Welcome to mirror list, hosted at ThFree Co, Russian Federation.

services.h « osl « kernel « cycles « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 334b6682e34480d26b77725755f944fab065e7fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/* SPDX-License-Identifier: Apache-2.0
 * Copyright 2011-2022 Blender Foundation */

#ifndef __OSL_SERVICES_H__
#define __OSL_SERVICES_H__

/* OSL Render Services
 *
 * Implementation of OSL render services, to retriever matrices, attributes,
 * textures and point clouds. In principle this should only be accessing
 * kernel data, but currently we also reach back into the Scene to retrieve
 * attributes.
 */

#include <OSL/oslclosure.h>
#include <OSL/oslexec.h>
#include <OSL/rendererservices.h>

#ifdef WITH_PTEX
class PtexCache;
#endif

CCL_NAMESPACE_BEGIN

class Object;
class Scene;
class Shader;
struct ShaderData;
struct float3;
struct KernelGlobalsCPU;

/* OSL Texture Handle
 *
 * OSL texture lookups are string based. If those strings are known at compile
 * time, the OSL compiler can cache a texture handle to use instead of a string.
 *
 * By default it uses TextureSystem::TextureHandle. But since we want to support
 * different kinds of textures and color space conversions, this is our own handle
 * with additional data.
 *
 * These are stored in a concurrent hash map, because OSL can compile multiple
 * shaders in parallel.
 *
 * NOTE: The svm_slots array contains a compressed mapping of tile to svm_slot pairs
 * stored as follows: x:tile_a, y:svm_slot_a, z:tile_b, w:svm_slot_b etc. */

struct OSLTextureHandle : public OIIO::RefCnt {
  enum Type { OIIO, SVM, IES, BEVEL, AO };

  OSLTextureHandle(Type type, const vector<int4> &svm_slots)
      : type(type), svm_slots(svm_slots), oiio_handle(NULL), processor(NULL)
  {
  }

  OSLTextureHandle(Type type = OIIO, int svm_slot = -1)
      : OSLTextureHandle(type, {make_int4(0, svm_slot, -1, -1)})
  {
  }

  Type type;
  vector<int4> svm_slots;
  OSL::TextureSystem::TextureHandle *oiio_handle;
  ColorSpaceProcessor *processor;
};

typedef OIIO::intrusive_ptr<OSLTextureHandle> OSLTextureHandleRef;
typedef OIIO::unordered_map_concurrent<ustring, OSLTextureHandleRef, ustringHash>
    OSLTextureHandleMap;

/* OSL Render Services
 *
 * Interface for OSL to access attributes, textures and other scene data. */

class OSLRenderServices : public OSL::RendererServices {
 public:
  OSLRenderServices(OSL::TextureSystem *texture_system);
  ~OSLRenderServices();

  static void register_closures(OSL::ShadingSystem *ss);

  bool get_matrix(OSL::ShaderGlobals *sg,
                  OSL::Matrix44 &result,
                  OSL::TransformationPtr xform,
                  float time) override;
  bool get_inverse_matrix(OSL::ShaderGlobals *sg,
                          OSL::Matrix44 &result,
                          OSL::TransformationPtr xform,
                          float time) override;

  bool get_matrix(OSL::ShaderGlobals *sg,
                  OSL::Matrix44 &result,
                  ustring from,
                  float time) override;
  bool get_inverse_matrix(OSL::ShaderGlobals *sg,
                          OSL::Matrix44 &result,
                          ustring to,
                          float time) override;

  bool get_matrix(OSL::ShaderGlobals *sg,
                  OSL::Matrix44 &result,
                  OSL::TransformationPtr xform) override;
  bool get_inverse_matrix(OSL::ShaderGlobals *sg,
                          OSL::Matrix44 &result,
                          OSL::TransformationPtr xform) override;

  bool get_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, ustring from) override;
  bool get_inverse_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, ustring from) override;

  bool get_array_attribute(OSL::ShaderGlobals *sg,
                           bool derivatives,
                           ustring object,
                           TypeDesc type,
                           ustring name,
                           int index,
                           void *val) override;
  bool get_attribute(OSL::ShaderGlobals *sg,
                     bool derivatives,
                     ustring object,
                     TypeDesc type,
                     ustring name,
                     void *val) override;
  bool get_attribute(ShaderData *sd,
                     bool derivatives,
                     ustring object_name,
                     TypeDesc type,
                     ustring name,
                     void *val);

  bool get_userdata(
      bool derivatives, ustring name, TypeDesc type, OSL::ShaderGlobals *sg, void *val) override;

  int pointcloud_search(OSL::ShaderGlobals *sg,
                        ustring filename,
                        const OSL::Vec3 &center,
                        float radius,
                        int max_points,
                        bool sort,
                        size_t *out_indices,
                        float *out_distances,
                        int derivs_offset) override;

  int pointcloud_get(OSL::ShaderGlobals *sg,
                     ustring filename,
                     size_t *indices,
                     int count,
                     ustring attr_name,
                     TypeDesc attr_type,
                     void *out_data) override;

  bool pointcloud_write(OSL::ShaderGlobals *sg,
                        ustring filename,
                        const OSL::Vec3 &pos,
                        int nattribs,
                        const ustring *names,
                        const TypeDesc *types,
                        const void **data) override;

  bool trace(TraceOpt &options,
             OSL::ShaderGlobals *sg,
             const OSL::Vec3 &P,
             const OSL::Vec3 &dPdx,
             const OSL::Vec3 &dPdy,
             const OSL::Vec3 &R,
             const OSL::Vec3 &dRdx,
             const OSL::Vec3 &dRdy) override;

  bool getmessage(OSL::ShaderGlobals *sg,
                  ustring source,
                  ustring name,
                  TypeDesc type,
                  void *val,
                  bool derivatives) override;

#if OSL_LIBRARY_VERSION_CODE >= 11100
  TextureSystem::TextureHandle *get_texture_handle(ustring filename,
                                                   OSL::ShadingContext *context) override;
#else
  TextureSystem::TextureHandle *get_texture_handle(ustring filename) override;
#endif

  bool good(TextureSystem::TextureHandle *texture_handle) override;

  bool texture(ustring filename,
               TextureSystem::TextureHandle *texture_handle,
               TexturePerthread *texture_thread_info,
               TextureOpt &options,
               OSL::ShaderGlobals *sg,
               float s,
               float t,
               float dsdx,
               float dtdx,
               float dsdy,
               float dtdy,
               int nchannels,
               float *result,
               float *dresultds,
               float *dresultdt,
               ustring *errormessage) override;

  bool texture3d(ustring filename,
                 TextureHandle *texture_handle,
                 TexturePerthread *texture_thread_info,
                 TextureOpt &options,
                 OSL::ShaderGlobals *sg,
                 const OSL::Vec3 &P,
                 const OSL::Vec3 &dPdx,
                 const OSL::Vec3 &dPdy,
                 const OSL::Vec3 &dPdz,
                 int nchannels,
                 float *result,
                 float *dresultds,
                 float *dresultdt,
                 float *dresultdr,
                 ustring *errormessage) override;

  bool environment(ustring filename,
                   TextureHandle *texture_handle,
                   TexturePerthread *texture_thread_info,
                   TextureOpt &options,
                   OSL::ShaderGlobals *sg,
                   const OSL::Vec3 &R,
                   const OSL::Vec3 &dRdx,
                   const OSL::Vec3 &dRdy,
                   int nchannels,
                   float *result,
                   float *dresultds,
                   float *dresultdt,
                   ustring *errormessage) override;

#if OSL_LIBRARY_VERSION_CODE >= 11100
  bool get_texture_info(ustring filename,
                        TextureHandle *texture_handle,
                        TexturePerthread *texture_thread_info,
                        OSL::ShadingContext *shading_context,
                        int subimage,
                        ustring dataname,
                        TypeDesc datatype,
                        void *data,
                        ustring *errormessage) override;
#else
  bool get_texture_info(OSL::ShaderGlobals *sg,
                        ustring filename,
                        TextureHandle *texture_handle,
                        int subimage,
                        ustring dataname,
                        TypeDesc datatype,
                        void *data) override;
#endif

  static bool get_background_attribute(const KernelGlobalsCPU *kg,
                                       ShaderData *sd,
                                       ustring name,
                                       TypeDesc type,
                                       bool derivatives,
                                       void *val);
  static bool get_object_standard_attribute(const KernelGlobalsCPU *kg,
                                            ShaderData *sd,
                                            ustring name,
                                            TypeDesc type,
                                            bool derivatives,
                                            void *val);

  static ustring u_distance;
  static ustring u_index;
  static ustring u_world;
  static ustring u_camera;
  static ustring u_screen;
  static ustring u_raster;
  static ustring u_ndc;
  static ustring u_object_location;
  static ustring u_object_color;
  static ustring u_object_alpha;
  static ustring u_object_index;
  static ustring u_geom_dupli_generated;
  static ustring u_geom_dupli_uv;
  static ustring u_material_index;
  static ustring u_object_random;
  static ustring u_particle_index;
  static ustring u_particle_random;
  static ustring u_particle_age;
  static ustring u_particle_lifetime;
  static ustring u_particle_location;
  static ustring u_particle_rotation;
  static ustring u_particle_size;
  static ustring u_particle_velocity;
  static ustring u_particle_angular_velocity;
  static ustring u_geom_numpolyvertices;
  static ustring u_geom_trianglevertices;
  static ustring u_geom_polyvertices;
  static ustring u_geom_name;
  static ustring u_geom_undisplaced;
  static ustring u_is_smooth;
  static ustring u_is_curve;
  static ustring u_curve_thickness;
  static ustring u_curve_length;
  static ustring u_curve_tangent_normal;
  static ustring u_curve_random;
  static ustring u_is_point;
  static ustring u_point_position;
  static ustring u_point_radius;
  static ustring u_point_random;
  static ustring u_normal_map_normal;
  static ustring u_path_ray_length;
  static ustring u_path_ray_depth;
  static ustring u_path_diffuse_depth;
  static ustring u_path_glossy_depth;
  static ustring u_path_transparent_depth;
  static ustring u_path_transmission_depth;
  static ustring u_trace;
  static ustring u_hit;
  static ustring u_hitdist;
  static ustring u_N;
  static ustring u_Ng;
  static ustring u_P;
  static ustring u_I;
  static ustring u_u;
  static ustring u_v;
  static ustring u_empty;
  static ustring u_at_bevel;
  static ustring u_at_ao;

  /* Texture system and texture handle map are part of the services instead of
   * globals to be shared between different render sessions. This saves memory,
   * and is required because texture handles are cached as part of the shared
   * shading system. */
  OSLTextureHandleMap textures;
};

CCL_NAMESPACE_END

#endif /* __OSL_SERVICES_H__ */