Blender V5.3
kernel/osl/types.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
7#if !defined(__KERNEL_GPU__)
8# include <OSL/oslversion.h>
9# include <OpenImageIO/oiioversion.h>
10#endif
11
12#include "kernel/types.h"
13
14#include "util/defines.h"
15#include "util/types_float3.h"
16
18
19#if defined(__KERNEL_GPU__)
20/* Strings are represented by their hashes on the GPU. */
21using DeviceString = size_t;
22#elif defined(OPENIMAGEIO_USTRING_H)
23# if OSL_LIBRARY_VERSION_CODE >= 11400
24using DeviceString = ustringhash;
25# else
26using DeviceString = ustring;
27# endif
28#else
29using DeviceString = const char *;
30#endif
31
35struct OSLTraceData;
36
38{
39#if defined(__KERNEL_GPU__)
40 (void)str;
41 return hash;
42#elif defined(OPENIMAGEIO_USTRING_H)
43 (void)hash; /* Ignored in release builds. */
44 const DeviceString result = ustring(str);
45 kernel_assert(result.hash() == hash);
46 return result;
47#else
48 (void)hash;
49 return str;
50#endif
51}
52
53/* Closure */
54
58
60
61#define OSL_CLOSURE_STRUCT_BEGIN(Upper, lower) OSL_CLOSURE_##Upper##_ID,
62#include "closures_template.h"
63
64 OSL_CLOSURE_LAYER_ID,
65};
66
70
71struct ccl_align(8) OSLClosureMul : public OSLClosure {
72 packed_float3 weight;
73 const ccl_private OSLClosure *closure;
74};
75
76struct ccl_align(8) OSLClosureAdd : public OSLClosure {
77 const ccl_private OSLClosure *closureA;
78 const ccl_private OSLClosure *closureB;
79};
80
81struct ccl_align(8) OSLClosureComponent : public OSLClosure {
82 packed_float3 weight;
83};
84
85/* Globals */
86
87/* This structure is essentially a copy of OSL::ShaderGlobals, but with some of the
88 * opaque pointers replaced with the types that we use for them and additional members
89 * at the end.
90 * As long as the layout matches (which is must in any case, in order for the OptiX OSL
91 * code to work), this works fine since OSL doesn't do pointer arithmetic etc. on the
92 * ShaderGlobals pointer that we give it. */
94 /* This part of ShaderGlobals is shared with OSL's own struct, so the layout must match! */
100 float u, dudx, dudy;
101 float v, dvdx, dvdy;
103 float time;
104 float dtime;
107
108 /* In OSL this is an opaque pointer named render-state. */
109 ccl_private ShaderData *sd;
110
111 /* In OSL this is an opaque pointer */
112 ccl_private OSLTraceData *tracedata;
113
114 /* In OSL this is an opaque pointer named `objdata`. */
115#ifdef __KERNEL_GPU__
116 ccl_private uint8_t *closure_pool;
117#else
119#endif
120
121 void *context;
124
125 /* We use this to encode the path state on GPUs.
126 * Zero means no state, positive means path_state, negative means shadow_path_state.
127 * On CPU, we use pointers in the Cycles-specific section below. */
129
130 void *renderer;
138
139 /* This part is Cycles-specific and ignored by OSL itself. */
140#ifdef __KERNEL_GPU__
141 ccl_private uint8_t *closure_pool_end;
142#else
145#endif
146};
147
149
150/* This is copied out of OSL's TextureOpt struct, and it differs between versions.
151 * Currently only missingcolor, sblur and tblur are used in Cycles code. */
152
153#ifndef OIIO_VERSION
154# error "OIIO_VERSION must be defined to select the matching OSL TextureOpt layout"
155#endif
156
157#if OIIO_VERSION >= 30000
158struct OSLTextureOptions {
159 int firstchannel = 0;
160 int subimage = 0;
161 DeviceString /* ustring */ subimagename;
162 uint8_t /* Wrap */ swrap = 0; /* Wrap::Default; */
163 uint8_t /* Wrap */ twrap = 0; /* Wrap::Default; */
164 uint8_t /* Wrap */ rwrap = 0; /* Wrap::Default; */
165 uint8_t /* MipMode */ mipmode = 0; /* MipMode::Default; */
166 uint8_t /* InterpMode */ interpmode = 3; /* InterpMode::SmartBicubic; */
167 bool conservative_filter = true;
168 uint16_t anisotropic = 32;
169 float sblur = 0, tblur = 0, rblur = 0;
170 float swidth = 1, twidth = 1;
171 float rwidth = 1;
172 float fill = 0;
173 const float *missingcolor = nullptr;
174 float rnd = -1;
175 int colortransformid = 0;
176 int envlayout = 0; /* private */
177};
178#else
183 int /* Wrap */ swrap;
184 int /* Wrap */ twrap;
185 int /* MipMode */ mipmode;
186 int /* InterpMode */ interpmode;
189 float sblur, tblur;
191 float fill;
192 const float *missingcolor;
193 float time;
194 union {
195 float bias;
196 float rnd;
197 };
199 int /* Wrap */ rwrap;
200 float rblur;
201 float rwidth;
203};
204#endif
205#ifndef __KERNEL_GPU__
206static_assert(sizeof(OSLTextureOptions) == sizeof(OSL::TextureOpt),
207 "OSLTextureOptions size mismatch.");
208static_assert(sizeof(OSLTextureOptions::sblur) == sizeof(OSL::TextureOpt::sblur),
209 "OSLTextureOptions::sblur size mismatch.");
210static_assert(sizeof(OSLTextureOptions::tblur) == sizeof(OSL::TextureOpt::tblur),
211 "OSLTextureOptions::tblur size mismatch.");
212static_assert(sizeof(OSLTextureOptions::missingcolor) == sizeof(OSL::TextureOpt::missingcolor),
213 "OSLTextureOptions::missingcolor size mismatch.");
214static_assert(offsetof(OSLTextureOptions, sblur) == offsetof(OSL::TextureOpt, sblur),
215 "OSLTextureOptions::sblur offsetof mismatch.");
216static_assert(offsetof(OSLTextureOptions, tblur) == offsetof(OSL::TextureOpt, tblur),
217 "OSLTextureOptions::tblur offsetof mismatch.");
218static_assert(offsetof(OSLTextureOptions, missingcolor) == offsetof(OSL::TextureOpt, missingcolor),
219 "OSLTextureOptions::missingcolor offsetof mismatch.");
220#endif
221
222/* Note: starting from 1 instead of 0 so that the encoded handle is never a null pointer
223 * the OSL will interpret as an invalid handle. */
224enum class OSLTextureHandleType : unsigned int { IMAGE = 1, IES = 2, BEVEL = 3, AO = 4 };
225
226#define OSL_TEXTURE_HANDLE_ENCODE(type, id) ((uintptr_t(type) << 32) | uintptr_t(uint(id)))
227#define OSL_TEXTURE_HANDLE_TYPE(handle) OSLTextureHandleType(uintptr_t(handle) >> 32)
228#define OSL_TEXTURE_HANDLE_ID(handle) int(uint(uintptr_t(handle) & uintptr_t(0xFFFFFFFF)))
229
blender::float3 packed_float3
#define kernel_assert(cond)
#define ccl_private
#define ccl_device_inline
#define ccl_align(...)
#define CCL_NAMESPACE_END
#define offsetof(t, d)
#define str(s)
#define IMAGE(slot, format, qualifiers, type, name)
OSLTextureHandleType
ccl_device_inline DeviceString make_string(const char *str, const size_t hash)
OSLClosureType
@ OSL_CLOSURE_NONE_ID
@ OSL_CLOSURE_MUL_ID
@ OSL_CLOSURE_ADD_ID
const char * DeviceString
#define hash
Definition noise_c.cc:156
OSLClosureType id
DeviceString subimagename
const float * missingcolor
packed_float3 dIdx
packed_float3 dPdu
packed_float3 dIdy
packed_float3 Ng
ccl_private void * object2common
packed_float3 dPsdx
packed_float3 dPdv
ccl_private ShaderData * sd
const ThreadKernelGlobalsCPU * kg
packed_float3 dPdz
ccl_private OSLClosure * Ci
ccl_private void * shader2common
packed_float3 dPdtime
packed_float3 dPsdy
ccl_private OSLTraceData * tracedata
const struct IntegratorStateCPU * path_state
packed_float3 dPdx
packed_float3 I
packed_float3 Ps
const struct IntegratorShadowStateCPU * shadow_path_state
packed_float3 N
packed_float3 dPdy
packed_float3 P