2 * Copyright 2011-2013 Blender Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 /* Constant Globals */
21 /* On the CPU, we pass along the struct KernelGlobals to nearly everywhere in
22 * the kernel, to access constant data. These are all stored as "textures", but
23 * these are really just standard arrays. We can't use actually globals because
24 * multiple renders may be running inside the same process. */
31 struct OSLShadingSystem;
34 typedef struct KernelGlobals {
35 texture_image_uchar4 texture_byte4_images[TEX_NUM_BYTE4_IMAGES_CPU];
36 texture_image_float4 texture_float4_images[TEX_NUM_FLOAT4_IMAGES_CPU];
37 texture_image_float texture_float_images[TEX_NUM_FLOAT_IMAGES_CPU];
38 texture_image_uchar texture_byte_images[TEX_NUM_BYTE_IMAGES_CPU];
40 # define KERNEL_TEX(type, ttype, name) ttype name;
41 # define KERNEL_IMAGE_TEX(type, ttype, name)
42 # include "kernel_textures.h"
47 /* On the CPU, we also have the OSL globals here. Most data structures are shared
48 * with SVM, the difference is in the shaders and object/mesh attributes. */
50 OSLShadingSystem *osl_ss;
51 OSLThreadData *osl_tdata;
56 #endif /* __KERNEL_CPU__ */
58 /* For CUDA, constant memory textures must be globals, so we can't put them
59 * into a struct. As a result we don't actually use this struct and use actual
60 * globals and simply pass along a NULL pointer everywhere, which we hope gets
63 #ifdef __KERNEL_CUDA__
65 __constant__ KernelData __data;
66 typedef struct KernelGlobals {} KernelGlobals;
68 # ifdef __KERNEL_CUDA_TEX_STORAGE__
69 # define KERNEL_TEX(type, ttype, name) ttype name;
71 # define KERNEL_TEX(type, ttype, name) const __constant__ __device__ type *name;
73 # define KERNEL_IMAGE_TEX(type, ttype, name) ttype name;
74 # include "kernel_textures.h"
76 #endif /* __KERNEL_CUDA__ */
80 #ifdef __KERNEL_OPENCL__
82 typedef ccl_addr_space struct KernelGlobals {
83 ccl_constant KernelData *data;
85 # define KERNEL_TEX(type, ttype, name) \
86 ccl_global type *name;
87 # include "kernel_textures.h"
89 # ifdef __SPLIT_KERNEL__
91 Intersection *isect_shadow;
95 #endif /* __KERNEL_OPENCL__ */
97 /* Interpolated lookup table access */
99 ccl_device float lookup_table_read(KernelGlobals *kg, float x, int offset, int size)
101 x = saturate(x)*(size-1);
103 int index = min(float_to_int(x), size-1);
104 int nindex = min(index+1, size-1);
107 float data0 = kernel_tex_fetch(__lookup_table, index + offset);
111 float data1 = kernel_tex_fetch(__lookup_table, nindex + offset);
112 return (1.0f - t)*data0 + t*data1;
115 ccl_device float lookup_table_read_2D(KernelGlobals *kg, float x, float y, int offset, int xsize, int ysize)
117 y = saturate(y)*(ysize-1);
119 int index = min(float_to_int(y), ysize-1);
120 int nindex = min(index+1, ysize-1);
123 float data0 = lookup_table_read(kg, x, offset + xsize*index, xsize);
127 float data1 = lookup_table_read(kg, x, offset + xsize*nindex, xsize);
128 return (1.0f - t)*data0 + t*data1;