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

blender_image.cpp « blender « cycles « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0fe42b2d47982f91d5b71bc262c3d3ea7109eade (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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
 * Copyright 2011-2013 Blender Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "render/image.h"

#include "blender/blender_sync.h"
#include "blender/blender_session.h"
#include "blender/blender_util.h"

CCL_NAMESPACE_BEGIN

/* builtin image file name is actually an image datablock name with
 * absolute sequence frame number concatenated via '@' character
 *
 * this function splits frame from builtin name
 */
int BlenderSession::builtin_image_frame(const string &builtin_name)
{
  int last = builtin_name.find_last_of('@');
  return atoi(builtin_name.substr(last + 1, builtin_name.size() - last - 1).c_str());
}

void BlenderSession::builtin_image_info(const string &builtin_name,
                                        void *builtin_data,
                                        ImageMetaData &metadata)
{
  /* empty image */
  metadata.width = 1;
  metadata.height = 1;

  if (!builtin_data)
    return;

  /* recover ID pointer */
  PointerRNA ptr;
  RNA_id_pointer_create((ID *)builtin_data, &ptr);
  BL::ID b_id(ptr);

  if (b_id.is_a(&RNA_Image)) {
    /* image data */
    BL::Image b_image(b_id);

    metadata.builtin_free_cache = !b_image.has_data();
    metadata.is_float = b_image.is_float();
    metadata.width = b_image.size()[0];
    metadata.height = b_image.size()[1];
    metadata.depth = 1;
    metadata.channels = b_image.channels();

    if (metadata.is_float) {
      /* Float images are already converted on the Blender side,
       * no need to do anything in Cycles. */
      metadata.colorspace = u_colorspace_raw;
    }
  }
  else if (b_id.is_a(&RNA_Object)) {
    /* smoke volume data */
    BL::Object b_ob(b_id);
    BL::FluidDomainSettings b_domain = object_fluid_gas_domain_find(b_ob);

    metadata.is_float = true;
    metadata.depth = 1;
    metadata.channels = 1;

    if (!b_domain)
      return;

    if (builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_DENSITY) ||
        builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_FLAME) ||
        builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_HEAT) ||
        builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_TEMPERATURE))
      metadata.channels = 1;
    else if (builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_COLOR))
      metadata.channels = 4;
    else if (builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_VELOCITY))
      metadata.channels = 3;
    else
      return;

    int3 resolution = get_int3(b_domain.domain_resolution());
    int amplify = (b_domain.use_noise()) ? b_domain.noise_scale() : 1;

    /* Velocity and heat data is always low-resolution. */
    if (builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_VELOCITY) ||
        builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_HEAT)) {
      amplify = 1;
    }

    metadata.width = resolution.x * amplify;
    metadata.height = resolution.y * amplify;
    metadata.depth = resolution.z * amplify;
  }
  else {
    /* TODO(sergey): Check we're indeed in shader node tree. */
    PointerRNA ptr;
    RNA_pointer_create(NULL, &RNA_Node, builtin_data, &ptr);
    BL::Node b_node(ptr);
    if (b_node.is_a(&RNA_ShaderNodeTexPointDensity)) {
      BL::ShaderNodeTexPointDensity b_point_density_node(b_node);
      metadata.channels = 4;
      metadata.width = b_point_density_node.resolution();
      metadata.height = metadata.width;
      metadata.depth = metadata.width;
      metadata.is_float = true;
    }
  }
}

bool BlenderSession::builtin_image_pixels(const string &builtin_name,
                                          void *builtin_data,
                                          int tile,
                                          unsigned char *pixels,
                                          const size_t pixels_size,
                                          const bool associate_alpha,
                                          const bool free_cache)
{
  if (!builtin_data) {
    return false;
  }

  const int frame = builtin_image_frame(builtin_name);

  PointerRNA ptr;
  RNA_id_pointer_create((ID *)builtin_data, &ptr);
  BL::Image b_image(ptr);

  const int width = b_image.size()[0];
  const int height = b_image.size()[1];
  const int channels = b_image.channels();

  unsigned char *image_pixels = image_get_pixels_for_frame(b_image, frame, tile);
  const size_t num_pixels = ((size_t)width) * height;

  if (image_pixels && num_pixels * channels == pixels_size) {
    memcpy(pixels, image_pixels, pixels_size * sizeof(unsigned char));
  }
  else {
    if (channels == 1) {
      memset(pixels, 0, pixels_size * sizeof(unsigned char));
    }
    else {
      const size_t num_pixels_safe = pixels_size / channels;
      unsigned char *cp = pixels;
      for (size_t i = 0; i < num_pixels_safe; i++, cp += channels) {
        cp[0] = 255;
        cp[1] = 0;
        cp[2] = 255;
        if (channels == 4) {
          cp[3] = 255;
        }
      }
    }
  }

  if (image_pixels) {
    MEM_freeN(image_pixels);
  }

  /* Free image buffers to save memory during render. */
  if (free_cache) {
    b_image.buffers_free();
  }

  if (associate_alpha) {
    /* Premultiply, byte images are always straight for Blender. */
    unsigned char *cp = pixels;
    for (size_t i = 0; i < num_pixels; i++, cp += channels) {
      cp[0] = (cp[0] * cp[3]) >> 8;
      cp[1] = (cp[1] * cp[3]) >> 8;
      cp[2] = (cp[2] * cp[3]) >> 8;
    }
  }
  return true;
}

bool BlenderSession::builtin_image_float_pixels(const string &builtin_name,
                                                void *builtin_data,
                                                int tile,
                                                float *pixels,
                                                const size_t pixels_size,
                                                const bool,
                                                const bool free_cache)
{
  if (!builtin_data) {
    return false;
  }

  PointerRNA ptr;
  RNA_id_pointer_create((ID *)builtin_data, &ptr);
  BL::ID b_id(ptr);

  if (b_id.is_a(&RNA_Image)) {
    /* image data */
    BL::Image b_image(b_id);
    int frame = builtin_image_frame(builtin_name);

    const int width = b_image.size()[0];
    const int height = b_image.size()[1];
    const int channels = b_image.channels();

    float *image_pixels;
    image_pixels = image_get_float_pixels_for_frame(b_image, frame, tile);
    const size_t num_pixels = ((size_t)width) * height;

    if (image_pixels && num_pixels * channels == pixels_size) {
      memcpy(pixels, image_pixels, pixels_size * sizeof(float));
    }
    else {
      if (channels == 1) {
        memset(pixels, 0, num_pixels * sizeof(float));
      }
      else {
        const size_t num_pixels_safe = pixels_size / channels;
        float *fp = pixels;
        for (int i = 0; i < num_pixels_safe; i++, fp += channels) {
          fp[0] = 1.0f;
          fp[1] = 0.0f;
          fp[2] = 1.0f;
          if (channels == 4) {
            fp[3] = 1.0f;
          }
        }
      }
    }

    if (image_pixels) {
      MEM_freeN(image_pixels);
    }

    /* Free image buffers to save memory during render. */
    if (free_cache) {
      b_image.buffers_free();
    }

    return true;
  }
  else if (b_id.is_a(&RNA_Object)) {
    /* smoke volume data */
    BL::Object b_ob(b_id);
    BL::FluidDomainSettings b_domain = object_fluid_gas_domain_find(b_ob);

    if (!b_domain) {
      return false;
    }
#if WITH_FLUID
    int3 resolution = get_int3(b_domain.domain_resolution());
    int length, amplify = (b_domain.use_noise()) ? b_domain.noise_scale() : 1;

    /* Velocity and heat data is always low-resolution. */
    if (builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_VELOCITY) ||
        builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_HEAT)) {
      amplify = 1;
    }

    const int width = resolution.x * amplify;
    const int height = resolution.y * amplify;
    const int depth = resolution.z * amplify;
    const size_t num_pixels = ((size_t)width) * height * depth;

    if (builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_DENSITY)) {
      FluidDomainSettings_density_grid_get_length(&b_domain.ptr, &length);
      if (length == num_pixels) {
        FluidDomainSettings_density_grid_get(&b_domain.ptr, pixels);
        return true;
      }
    }
    else if (builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_FLAME)) {
      /* this is in range 0..1, and interpreted by the OpenGL smoke viewer
       * as 1500..3000 K with the first part faded to zero density */
      FluidDomainSettings_flame_grid_get_length(&b_domain.ptr, &length);
      if (length == num_pixels) {
        FluidDomainSettings_flame_grid_get(&b_domain.ptr, pixels);
        return true;
      }
    }
    else if (builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_COLOR)) {
      /* the RGB is "premultiplied" by density for better interpolation results */
      FluidDomainSettings_color_grid_get_length(&b_domain.ptr, &length);
      if (length == num_pixels * 4) {
        FluidDomainSettings_color_grid_get(&b_domain.ptr, pixels);
        return true;
      }
    }
    else if (builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_VELOCITY)) {
      FluidDomainSettings_velocity_grid_get_length(&b_domain.ptr, &length);
      if (length == num_pixels * 3) {
        FluidDomainSettings_velocity_grid_get(&b_domain.ptr, pixels);
        return true;
      }
    }
    else if (builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_HEAT)) {
      FluidDomainSettings_heat_grid_get_length(&b_domain.ptr, &length);
      if (length == num_pixels) {
        FluidDomainSettings_heat_grid_get(&b_domain.ptr, pixels);
        return true;
      }
    }
    else if (builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_TEMPERATURE)) {
      FluidDomainSettings_temperature_grid_get_length(&b_domain.ptr, &length);
      if (length == num_pixels) {
        FluidDomainSettings_temperature_grid_get(&b_domain.ptr, pixels);
        return true;
      }
    }
    else {
      fprintf(
          stderr, "Cycles error: unknown volume attribute %s, skipping\n", builtin_name.c_str());
      pixels[0] = 0.0f;
      return false;
    }
#endif
    fprintf(stderr, "Cycles error: unexpected smoke volume resolution, skipping\n");
  }
  else {
    /* We originally were passing view_layer here but in reality we need a
     * a depsgraph to pass to the RE_point_density_minmax() function.
     */
    /* TODO(sergey): Check we're indeed in shader node tree. */
    PointerRNA ptr;
    RNA_pointer_create(NULL, &RNA_Node, builtin_data, &ptr);
    BL::Node b_node(ptr);
    if (b_node.is_a(&RNA_ShaderNodeTexPointDensity)) {
      BL::ShaderNodeTexPointDensity b_point_density_node(b_node);
      int length;
      b_point_density_node.calc_point_density(b_depsgraph, &length, &pixels);
    }
  }

  return false;
}

void BlenderSession::builtin_images_load()
{
  /* Force builtin images to be loaded along with Blender data sync. This
   * is needed because we may be reading from depsgraph evaluated data which
   * can be freed by Blender before Cycles reads it.
   *
   * TODO: the assumption that no further access to builtin image data will
   * happen is really weak, and likely to break in the future. We should find
   * a better solution to hand over the data directly to the image manager
   * instead of through callbacks whose timing is difficult to control. */
  ImageManager *manager = session->scene->image_manager;
  Device *device = session->device;
  manager->device_load_builtin(device, session->scene, session->progress);
}

CCL_NAMESPACE_END