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 #define MAX_BYTE_IMAGES 1024
35 #define MAX_FLOAT_IMAGES 1024
37 typedef struct KernelGlobals {
38 texture_image_uchar4 texture_byte_images[MAX_BYTE_IMAGES];
39 texture_image_float4 texture_float_images[MAX_FLOAT_IMAGES];
41 #define KERNEL_TEX(type, ttype, name) ttype name;
42 #define KERNEL_IMAGE_TEX(type, ttype, name)
43 #include "kernel_textures.h"
48 /* On the CPU, we also have the OSL globals here. Most data structures are shared
49 * with SVM, the difference is in the shaders and object/mesh attributes. */
51 OSLShadingSystem *osl_ss;
52 OSLThreadData *osl_tdata;
59 /* For CUDA, constant memory textures must be globals, so we can't put them
60 * into a struct. As a result we don't actually use this struct and use actual
61 * globals and simply pass along a NULL pointer everywhere, which we hope gets
64 #ifdef __KERNEL_CUDA__
66 __constant__ KernelData __data;
67 typedef struct KernelGlobals {} KernelGlobals;
69 #ifdef __KERNEL_CUDA_TEX_STORAGE__
70 #define KERNEL_TEX(type, ttype, name) ttype name;
72 #define KERNEL_TEX(type, ttype, name) const __constant__ __device__ type *name;
74 #define KERNEL_IMAGE_TEX(type, ttype, name) ttype name;
75 #include "kernel_textures.h"
81 #ifdef __KERNEL_OPENCL__
83 typedef ccl_addr_space struct KernelGlobals {
84 ccl_constant KernelData *data;
86 #define KERNEL_TEX(type, ttype, name) \
87 ccl_global type *name;
88 #include "kernel_textures.h"
90 #ifdef __SPLIT_KERNEL__
92 Intersection *isect_shadow;
98 /* Interpolated lookup table access */
100 ccl_device float lookup_table_read(KernelGlobals *kg, float x, int offset, int size)
102 x = saturate(x)*(size-1);
104 int index = min(float_to_int(x), size-1);
105 int nindex = min(index+1, size-1);
108 float data0 = kernel_tex_fetch(__lookup_table, index + offset);
112 float data1 = kernel_tex_fetch(__lookup_table, nindex + offset);
113 return (1.0f - t)*data0 + t*data1;
116 ccl_device float lookup_table_read_2D(KernelGlobals *kg, float x, float y, int offset, int xsize, int ysize)
118 y = saturate(y)*(ysize-1);
120 int index = min(float_to_int(y), ysize-1);
121 int nindex = min(index+1, ysize-1);
124 float data0 = lookup_table_read(kg, x, offset + xsize*index, xsize);
128 float data1 = lookup_table_read(kg, x, offset + xsize*nindex, xsize);
129 return (1.0f - t)*data0 + t*data1;