1a7e775113b4055ad6f7f3fcff5e09fac8afe7d7
[blender-staging.git] / source / blender / compositor / operations / COM_RenderLayersProg.cpp
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor: 
19  *              Jeroen Bakker 
20  *              Monique Dewanchand
21  */
22
23 #include "COM_RenderLayersProg.h"
24
25 #include "BLI_listbase.h"
26 #include "BKE_scene.h"
27 #include "DNA_scene_types.h"
28
29 extern "C" {
30 #  include "RE_pipeline.h"
31 #  include "RE_shader_ext.h"
32 #  include "RE_render_ext.h"
33 }
34
35 /* ******** Render Layers Base Prog ******** */
36
37 RenderLayersBaseProg::RenderLayersBaseProg(int renderpass, int elementsize) : NodeOperation()
38 {
39         this->m_renderpass = renderpass;
40         this->setScene(NULL);
41         this->m_inputBuffer = NULL;
42         this->m_elementsize = elementsize;
43         this->m_rd = NULL;
44 }
45
46
47 void RenderLayersBaseProg::initExecution()
48 {
49         Scene *scene = this->getScene();
50         Render *re = (scene) ? RE_GetRender(scene->id.name) : NULL;
51         RenderResult *rr = NULL;
52         
53         if (re)
54                 rr = RE_AcquireResultRead(re);
55         
56         if (rr) {
57                 SceneRenderLayer *srl = (SceneRenderLayer *)BLI_findlink(&scene->r.layers, getLayerId());
58                 if (srl) {
59
60                         RenderLayer *rl = RE_GetRenderLayer(rr, srl->name);
61                         if (rl) {
62                                 this->m_inputBuffer = RE_RenderLayerGetPass(rl, this->m_renderpass, this->m_viewName);
63                                 if (this->m_inputBuffer == NULL && this->m_renderpass == SCE_PASS_COMBINED) {
64                                         this->m_inputBuffer = RE_RenderLayerGetPass(rl, SCE_PASS_COMBINED, this->m_viewName);
65                                 }
66                         }
67                 }
68         }
69         if (re) {
70                 RE_ReleaseResult(re);
71                 re = NULL;
72         }
73 }
74
75 void RenderLayersBaseProg::doInterpolation(float output[4], float x, float y, PixelSampler sampler)
76 {
77         unsigned int offset;
78         int width = this->getWidth(), height = this->getHeight();
79
80         switch (sampler) {
81                 case COM_PS_NEAREST: {
82                         int ix = x;
83                         int iy = y;
84                         if (ix < 0 || iy < 0 || ix >= width || iy >= height) {
85                                 if (this->m_elementsize == 1)
86                                         output[0] = 0.0f;
87                                 else if (this->m_elementsize == 3)
88                                         zero_v3(output);
89                                 else
90                                         zero_v4(output);
91                                 break;
92                                 
93                         }
94
95                         offset = (iy * width + ix) * this->m_elementsize;
96
97                         if (this->m_elementsize == 1)
98                                 output[0] = this->m_inputBuffer[offset];
99                         else if (this->m_elementsize == 3)
100                                 copy_v3_v3(output, &this->m_inputBuffer[offset]);
101                         else
102                                 copy_v4_v4(output, &this->m_inputBuffer[offset]);
103                         break;
104                 }
105
106                 case COM_PS_BILINEAR:
107                         BLI_bilinear_interpolation_fl(this->m_inputBuffer, output, width, height, this->m_elementsize, x, y);
108                         break;
109
110                 case COM_PS_BICUBIC:
111                         BLI_bicubic_interpolation_fl(this->m_inputBuffer, output, width, height, this->m_elementsize, x, y);
112                         break;
113         }
114 }
115
116 void RenderLayersBaseProg::executePixelSampled(float output[4], float x, float y, PixelSampler sampler)
117 {
118 #if 0
119         const RenderData *rd = this->m_rd;
120
121         int dx = 0, dy = 0;
122
123         if (rd->mode & R_BORDER && rd->mode & R_CROP) {
124                 /* see comment in executeRegion describing coordinate mapping,
125                  * here it simply goes other way around
126                  */
127                 int full_width  = rd->xsch * rd->size / 100;
128                 int full_height = rd->ysch * rd->size / 100;
129
130                 dx = rd->border.xmin * full_width - (full_width - this->getWidth()) / 2.0f;
131                 dy = rd->border.ymin * full_height - (full_height - this->getHeight()) / 2.0f;
132         }
133
134         int ix = x - dx;
135         int iy = y - dy;
136 #endif
137
138 #ifndef NDEBUG
139         {
140                 const DataType data_type = this->getOutputSocket()->getDataType();
141                 int actual_element_size = this->m_elementsize;
142                 int expected_element_size;
143                 if (data_type == COM_DT_VALUE) {
144                         expected_element_size = 1;
145                 }
146                 else if (data_type == COM_DT_VECTOR) {
147                         expected_element_size = 3;
148                 }
149                 else if (data_type == COM_DT_COLOR) {
150                         expected_element_size = 4;
151                 }
152                 else {
153                         BLI_assert(!"Something horribly wrong just happened");
154                 }
155                 BLI_assert(expected_element_size == actual_element_size);
156         }
157 #endif
158
159         if (this->m_inputBuffer == NULL) {
160                 int elemsize = this->m_elementsize;
161                 if (elemsize == 1) {
162                         output[0] = 0.0f;
163                 }
164                 else if (elemsize == 3) {
165                         zero_v3(output);
166                 }
167                 else {
168                         BLI_assert(elemsize == 4);
169                         zero_v4(output);
170                 }
171         }
172         else {
173                 doInterpolation(output, x, y, sampler);
174         }
175 }
176
177 void RenderLayersBaseProg::deinitExecution()
178 {
179         this->m_inputBuffer = NULL;
180 }
181
182 void RenderLayersBaseProg::determineResolution(unsigned int resolution[2], unsigned int /*preferredResolution*/[2])
183 {
184         Scene *sce = this->getScene();
185         Render *re = (sce) ? RE_GetRender(sce->id.name) : NULL;
186         RenderResult *rr = NULL;
187         
188         resolution[0] = 0;
189         resolution[1] = 0;
190         
191         if (re)
192                 rr = RE_AcquireResultRead(re);
193         
194         if (rr) {
195                 SceneRenderLayer *srl   = (SceneRenderLayer *)BLI_findlink(&sce->r.layers, getLayerId());
196                 if (srl) {
197                         RenderLayer *rl = RE_GetRenderLayer(rr, srl->name);
198                         if (rl) {
199                                 resolution[0] = rl->rectx;
200                                 resolution[1] = rl->recty;
201                         }
202                 }
203         }
204         
205         if (re)
206                 RE_ReleaseResult(re);
207
208 }
209
210 /* ******** Render Layers AO Operation ******** */
211
212 RenderLayersAOOperation::RenderLayersAOOperation() : RenderLayersBaseProg(SCE_PASS_AO, 3)
213 {
214         this->addOutputSocket(COM_DT_COLOR);
215 }
216
217
218 void RenderLayersAOOperation::executePixelSampled(float output[4], float x, float y, PixelSampler sampler)
219 {
220         float *inputBuffer = this->getInputBuffer();
221         if (inputBuffer == NULL) {
222                 zero_v3(output);
223         }
224         else {
225                 doInterpolation(output, x, y, sampler);
226         }
227         output[3] = 1.0f;
228 }
229
230 /* ******** Render Layers Alpha Operation ******** */
231
232 RenderLayersAlphaProg::RenderLayersAlphaProg() : RenderLayersBaseProg(SCE_PASS_COMBINED, 4)
233 {
234         this->addOutputSocket(COM_DT_VALUE);
235 }
236
237 void RenderLayersAlphaProg::executePixelSampled(float output[4], float x, float y, PixelSampler sampler)
238 {
239         float *inputBuffer = this->getInputBuffer();
240
241         if (inputBuffer == NULL) {
242                 output[0] = 0.0f;
243         }
244         else {
245                 float temp[4];
246                 doInterpolation(temp, x, y, sampler);
247                 output[0] = temp[3];
248         }
249 }
250
251 /* ******** Render Layers Color Operation ******** */
252
253 RenderLayersColorOperation::RenderLayersColorOperation() : RenderLayersBaseProg(SCE_PASS_RGBA, 4)
254 {
255         this->addOutputSocket(COM_DT_COLOR);
256 }
257
258 /* ******** Render Layers Cycles Operation ******** */
259
260 RenderLayersCyclesOperation::RenderLayersCyclesOperation(int pass) : RenderLayersBaseProg(pass, 3)
261 {
262         this->addOutputSocket(COM_DT_VECTOR);
263 }
264
265 /* ******** Render Layers Depth Operation ******** */
266
267 RenderLayersDepthProg::RenderLayersDepthProg() : RenderLayersBaseProg(SCE_PASS_Z, 1)
268 {
269         this->addOutputSocket(COM_DT_VALUE);
270 }
271
272 void RenderLayersDepthProg::executePixelSampled(float output[4], float x, float y, PixelSampler /*sampler*/)
273 {
274         int ix = x;
275         int iy = y;
276         float *inputBuffer = this->getInputBuffer();
277
278         if (inputBuffer == NULL || ix < 0 || iy < 0 || ix >= (int)this->getWidth() || iy >= (int)this->getHeight() ) {
279                 output[0] = 0.0f;
280         }
281         else {
282                 unsigned int offset = (iy * this->getWidth() + ix);
283                 output[0] = inputBuffer[offset];
284         }
285 }
286
287 /* ******** Render Layers Diffuse Operation ******** */
288
289 RenderLayersDiffuseOperation::RenderLayersDiffuseOperation() : RenderLayersBaseProg(SCE_PASS_DIFFUSE, 3)
290 {
291         this->addOutputSocket(COM_DT_VECTOR);
292 }
293
294 /* ******** Render Layers Emit Operation ******** */
295
296 RenderLayersEmitOperation::RenderLayersEmitOperation() : RenderLayersBaseProg(SCE_PASS_EMIT, 3)
297 {
298         this->addOutputSocket(COM_DT_VECTOR);
299 }
300
301 /* ******** Render Layers Environment Operation ******** */
302
303 RenderLayersEnvironmentOperation::RenderLayersEnvironmentOperation() : RenderLayersBaseProg(SCE_PASS_ENVIRONMENT, 3)
304 {
305         this->addOutputSocket(COM_DT_VECTOR);
306 }
307
308 /* ******** Render Layers Image Operation ******** */
309
310 RenderLayersColorProg::RenderLayersColorProg() : RenderLayersBaseProg(SCE_PASS_COMBINED, 4)
311 {
312         this->addOutputSocket(COM_DT_COLOR);
313 }
314
315 /* ******** Render Layers Indirect Operation ******** */
316
317 RenderLayersIndirectOperation::RenderLayersIndirectOperation() : RenderLayersBaseProg(SCE_PASS_INDIRECT, 3)
318 {
319         this->addOutputSocket(COM_DT_VECTOR);
320 }
321
322 /* ******** Render Layers Material Index Operation ******** */
323
324 RenderLayersMaterialIndexOperation::RenderLayersMaterialIndexOperation() : RenderLayersBaseProg(SCE_PASS_INDEXMA, 1)
325 {
326         this->addOutputSocket(COM_DT_VALUE);
327 }
328
329 /* ******** Render Layers Mist Operation ******** */
330
331 RenderLayersMistOperation::RenderLayersMistOperation() : RenderLayersBaseProg(SCE_PASS_MIST, 1)
332 {
333         this->addOutputSocket(COM_DT_VALUE);
334 }
335
336 /* ******** Render Layers Normal Operation ******** */
337
338 RenderLayersNormalOperation::RenderLayersNormalOperation() : RenderLayersBaseProg(SCE_PASS_NORMAL, 3)
339 {
340         this->addOutputSocket(COM_DT_VECTOR);
341 }
342
343 /* ******** Render Layers Object Index Operation ******** */
344
345 RenderLayersObjectIndexOperation::RenderLayersObjectIndexOperation() : RenderLayersBaseProg(SCE_PASS_INDEXOB, 1)
346 {
347         this->addOutputSocket(COM_DT_VALUE);
348 }
349
350 /* ******** Render Layers Reflection Operation ******** */
351
352 RenderLayersReflectionOperation::RenderLayersReflectionOperation() : RenderLayersBaseProg(SCE_PASS_REFLECT, 3)
353 {
354         this->addOutputSocket(COM_DT_VECTOR);
355 }
356
357 /* ******** Render Layers Refraction Operation ******** */
358
359 RenderLayersRefractionOperation::RenderLayersRefractionOperation() : RenderLayersBaseProg(SCE_PASS_REFRACT, 3)
360 {
361         this->addOutputSocket(COM_DT_VECTOR);
362 }
363
364 /* ******** Render Layers Shadow Operation ******** */
365
366 RenderLayersShadowOperation::RenderLayersShadowOperation() : RenderLayersBaseProg(SCE_PASS_SHADOW, 3)
367 {
368         this->addOutputSocket(COM_DT_VECTOR);
369 }
370
371 /* ******** Render Layers Specular Operation ******** */
372
373 RenderLayersSpecularOperation::RenderLayersSpecularOperation() : RenderLayersBaseProg(SCE_PASS_SPEC, 3)
374 {
375         this->addOutputSocket(COM_DT_VECTOR);
376 }
377
378 /* ******** Render Layers Speed Operation ******** */
379
380 RenderLayersSpeedOperation::RenderLayersSpeedOperation() : RenderLayersBaseProg(SCE_PASS_VECTOR, 4)
381 {
382         this->addOutputSocket(COM_DT_COLOR);
383 }
384
385 /* ******** Render Layers UV Operation ******** */
386
387 RenderLayersUVOperation::RenderLayersUVOperation() : RenderLayersBaseProg(SCE_PASS_UV, 3)
388 {
389         this->addOutputSocket(COM_DT_VECTOR);
390 }
391
392 /* ******** Debug Render Layers Cycles Operation ******** */
393
394 #ifdef WITH_CYCLES_DEBUG
395
396 RenderLayersCyclesDebugOperation::RenderLayersCyclesDebugOperation(
397         int pass,
398         int debug_pass_type)
399         : RenderLayersBaseProg(pass, RE_debug_pass_num_channels_get(debug_pass_type))
400 {
401         switch (m_elementsize) {
402                 case 1:
403                         this->addOutputSocket(COM_DT_VALUE);
404                         break;
405                 case 3:
406                         this->addOutputSocket(COM_DT_VECTOR);
407                         break;
408                 case 4:
409                         this->addOutputSocket(COM_DT_COLOR);
410                         break;
411                 default:
412                         BLI_assert(!"Unkown debug pass type element size.");
413         }
414 }
415
416 #endif