Blender  V2.93
kernel_shader_sort.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2017 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
20 {
21 #ifndef __KERNEL_CUDA__
22  int tid = ccl_global_id(1) * ccl_global_size(0) + ccl_global_id(0);
24  if (tid == 0) {
25  kernel_split_params.queue_index[QUEUE_SHADER_SORTED_RAYS] = qsize;
26  }
27 
29  if (offset >= qsize) {
30  return;
31  }
32 
33  int lid = ccl_local_id(1) * ccl_local_size(0) + ccl_local_id(0);
36  ccl_local uint *local_value = &locals->local_value[0];
37  ccl_local ushort *local_index = &locals->local_index[0];
38 
39  /* copy to local memory */
40  for (uint i = 0; i < SHADER_SORT_BLOCK_SIZE; i += SHADER_SORT_LOCAL_SIZE) {
41  uint idx = offset + i + lid;
42  uint add = input + idx;
43  uint value = (~0);
44  if (idx < qsize) {
45  int ray_index = kernel_split_state.queue_data[add];
46  bool valid = (ray_index != QUEUE_EMPTY_SLOT) &&
47  IS_STATE(kernel_split_state.ray_state, ray_index, RAY_ACTIVE);
48  if (valid) {
49  value = kernel_split_sd(sd, ray_index)->shader & SHADER_MASK;
50  }
51  }
52  local_value[i + lid] = value;
53  local_index[i + lid] = i + lid;
54  }
56 
57  /* skip sorting for cpu split kernel */
58 # ifdef __KERNEL_OPENCL__
59 
60  /* bitonic sort */
61  for (uint length = 1; length < SHADER_SORT_BLOCK_SIZE; length <<= 1) {
62  for (uint inc = length; inc > 0; inc >>= 1) {
63  for (uint ii = 0; ii < SHADER_SORT_BLOCK_SIZE; ii += SHADER_SORT_LOCAL_SIZE) {
64  uint i = lid + ii;
65  bool direction = ((i & (length << 1)) != 0);
66  uint j = i ^ inc;
67  ushort ioff = local_index[i];
68  ushort joff = local_index[j];
69  uint iKey = local_value[ioff];
70  uint jKey = local_value[joff];
71  bool smaller = (jKey < iKey) || (jKey == iKey && j < i);
72  bool swap = smaller ^ (j < i) ^ direction;
74  local_index[i] = (swap) ? joff : ioff;
75  local_index[j] = (swap) ? ioff : joff;
77  }
78  }
79  }
80 # endif /* __KERNEL_OPENCL__ */
81 
82  /* copy to destination */
83  for (uint i = 0; i < SHADER_SORT_BLOCK_SIZE; i += SHADER_SORT_LOCAL_SIZE) {
84  uint idx = offset + i + lid;
85  uint lidx = local_index[i + lid];
86  uint outi = output + idx;
87  uint ini = input + offset + lidx;
88  uint value = local_value[lidx];
89  if (idx < qsize) {
90  kernel_split_state.queue_data[outi] = (value == (~0)) ? QUEUE_EMPTY_SLOT :
91  kernel_split_state.queue_data[ini];
92  }
93  }
94 #endif /* __KERNEL_CUDA__ */
95 }
96 
unsigned int uint
Definition: BLI_sys_types.h:83
unsigned short ushort
Definition: BLI_sys_types.h:84
void swap(T &a, T &b)
Definition: Common.h:33
#define output
SIMD_FORCE_INLINE btScalar length(const btQuaternion &q)
Return the length of a quaternion.
Definition: btQuaternion.h:895
#define ccl_global_id(d)
#define ccl_global_size(d)
#define ccl_local_param
#define ccl_device
ccl_device_inline uint ccl_local_id(uint d)
ccl_device_inline uint ccl_local_size(uint d)
#define ccl_local
#define CCL_NAMESPACE_END
CCL_NAMESPACE_BEGIN ccl_device void kernel_shader_sort(KernelGlobals *kg, ccl_local_param ShaderSortLocals *locals)
#define kernel_split_params
#define kernel_split_sd(sd, ray_index)
#define kernel_split_state
#define SHADER_SORT_BLOCK_SIZE
Definition: kernel_types.h:75
#define IS_STATE(ray_state, ray_index, state)
@ SHADER_MASK
Definition: kernel_types.h:593
#define SHADER_SORT_LOCAL_SIZE
Definition: kernel_types.h:82
#define QUEUE_EMPTY_SLOT
@ RAY_ACTIVE
@ QUEUE_SHADER_SORTED_RAYS
@ QUEUE_ACTIVE_AND_REGENERATED_RAYS
static void add(GHash *messages, MemArena *memarena, const Message *msg)
Definition: msgfmt.c:268
#define CCL_LOCAL_MEM_FENCE
Definition: util_atomic.h:32
#define ccl_barrier(flags)
Definition: util_atomic.h:33