2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * The Original Code is Copyright (C) 2006 Blender Foundation
19 * All rights reserved.
21 * Contributors: Hos, Robert Wenzlaff.
23 * ***** END GPL LICENSE BLOCK *****
26 /** \file blender/render/intern/source/shadeinput.c
37 #include "BLI_blenlib.h"
38 #include "BLI_utildefines.h"
40 #include "DNA_curve_types.h"
41 #include "DNA_group_types.h"
42 #include "DNA_lamp_types.h"
43 #include "DNA_meshdata_types.h"
44 #include "DNA_material_types.h"
46 #include "BKE_colortools.h"
47 #include "BKE_scene.h"
52 #include "raycounter.h"
53 #include "renderpipeline.h"
54 #include "render_types.h"
55 #include "renderdatabase.h"
56 #include "rendercore.h"
61 #include "volumetric.h"
64 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
65 /* defined in pipeline.c, is hardcopy of active dynamic allocated Render */
66 /* only to be used here in this file, it's for speed */
67 extern struct Render R;
68 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
70 /* Shade Sample order:
72 * - shade_samples_fill_with_ps()
74 * - shade_input_set_triangle() <- if prev sample-face is same, use shade_input_copy_triangle()
76 * - shade_input_set_viewco() <- not for ray or bake
77 * - shade_input_set_uv() <- not for ray or bake
78 * - shade_input_set_normals()
81 * - shade_samples_do_AO()
82 * - if shading happens
84 * - shade_input_set_shade_texco()
85 * - shade_samples_do_shade()
86 * - OSA: distribute sample result with filter masking
90 /* initialize material variables in shadeinput,
91 * doing inverse gamma correction where applicable */
92 void shade_input_init_material(ShadeInput *shi)
94 /* note, keep this synced with render_types.h */
95 memcpy(&shi->r, &shi->mat->r, 23 * sizeof(float));
96 shi->har = shi->mat->har;
99 /* also used as callback for nodes */
100 /* delivers a fully filled in ShadeResult, for all passes */
101 void shade_material_loop(ShadeInput *shi, ShadeResult *shr)
104 shade_lamp_loop(shi, shr); /* clears shr */
106 if (shi->translucency != 0.0f) {
108 float fac = shi->translucency;
110 shade_input_init_material(shi);
111 negate_v3_v3(shi->vn, shi->vno);
112 negate_v3(shi->facenor);
113 shi->depth++; /* hack to get real shadow now */
114 shade_lamp_loop(shi, &shr_t);
117 /* a couple of passes */
118 madd_v3_v3fl(shr->combined, shr_t.combined, fac);
119 if (shi->passflag & SCE_PASS_SPEC)
120 madd_v3_v3fl(shr->spec, shr_t.spec, fac);
121 if (shi->passflag & SCE_PASS_DIFFUSE) {
122 madd_v3_v3fl(shr->diff, shr_t.diff, fac);
123 madd_v3_v3fl(shr->diffshad, shr_t.diffshad, fac);
125 if (shi->passflag & SCE_PASS_SHADOW)
126 madd_v3_v3fl(shr->shad, shr_t.shad, fac);
129 negate_v3(shi->facenor);
132 /* depth >= 1 when ray-shading */
133 if (shi->depth == 0 || shi->volume_depth > 0) {
134 if (R.r.mode & R_RAYTRACE) {
135 if (shi->ray_mirror != 0.0f || ((shi->mode & MA_TRANSP) && (shi->mode & MA_RAYTRANSP) && shr->alpha != 1.0f)) {
136 /* ray trace works on combined, but gives pass info */
140 /* disable adding of sky for raytransp */
141 if ((shi->mode & MA_TRANSP) && (shi->mode & MA_RAYTRANSP))
142 if ((shi->layflag & SCE_LAY_SKY) && (R.r.alphamode == R_ADDSKY))
146 if (R.r.mode & R_RAYTRACE) {
147 if (R.render_volumes_inside.first)
148 shade_volume_inside(shi, shr);
153 /* do a shade, finish up some passes, apply mist */
154 void shade_input_do_shade(ShadeInput *shi, ShadeResult *shr)
159 /* ------ main shading loop -------- */
161 memset(&shi->raycounter, 0, sizeof(shi->raycounter));
164 if (shi->mat->nodetree && shi->mat->use_nodes)
165 compat = ntreeShaderExecTree(shi->mat->nodetree, shi, shr);
167 /* also run this when node shaders fail, due to incompatible shader nodes */
168 if (compat == false) {
169 /* copy all relevant material vars, note, keep this synced with render_types.h */
170 shade_input_init_material(shi);
172 if (shi->mat->material_type == MA_TYPE_VOLUME) {
173 if (R.r.mode & R_RAYTRACE) {
174 shade_volume_outside(shi, shr);
177 else { /* MA_TYPE_SURFACE, MA_TYPE_WIRE */
178 shade_material_loop(shi, shr);
182 /* copy additional passes */
183 if (shi->passflag & (SCE_PASS_VECTOR | SCE_PASS_NORMAL)) {
184 copy_v4_v4(shr->winspeed, shi->winspeed);
185 copy_v3_v3(shr->nor, shi->vn);
189 if ((shi->passflag & SCE_PASS_MIST) || ((R.wrld.mode & WO_MIST) && (shi->mat->mode & MA_NOMIST) == 0)) {
190 if (R.r.mode & R_ORTHO)
191 shr->mist = mistfactor(-shi->co[2], shi->co);
193 shr->mist = mistfactor(len_v3(shi->co), shi->co);
195 else shr->mist = 0.0f;
197 if ((R.wrld.mode & WO_MIST) && (shi->mat->mode & MA_NOMIST) == 0) {
202 /* add mist and premul color */
203 if (shr->alpha != 1.0f || alpha != 1.0f) {
204 float fac = alpha * (shr->alpha);
205 shr->combined[3] = fac;
207 if (shi->mat->material_type != MA_TYPE_VOLUME)
208 mul_v3_fl(shr->combined, fac);
211 shr->combined[3] = 1.0f;
214 shr->z = -shi->co[2];
218 if (1 || shi->passflag & SCE_PASS_RAYHITS) {
219 shr->rayhits[0] = (float)shi->raycounter.faces.test;
220 shr->rayhits[1] = (float)shi->raycounter.bb.hit;
221 shr->rayhits[2] = 0.0;
222 shr->rayhits[3] = 1.0;
226 RE_RC_MERGE(&re_rc_counter[shi->thread], &shi->raycounter);
229 /* **************************************************************************** */
231 /* **************************************************************************** */
234 void vlr_set_uv_indices(VlakRen *vlr, int *i1, int *i2, int *i3)
236 /* to prevent storing new tfaces or vcols, we check a split runtime */
240 /* 1---2 1---2 0 = orig face, 1 = new face */
242 /* Update vert nums to point to correct verts of original face */
243 if (vlr->flag & R_DIVIDE_24) {
244 if (vlr->flag & R_FACE_SPLIT) {
245 (*i1)++; (*i2)++; (*i3)++;
251 else if (vlr->flag & R_FACE_SPLIT) {
256 /* copy data from face to ShadeInput, general case */
257 /* indices 0 1 2 3 only */
258 void shade_input_set_triangle_i(ShadeInput *shi, ObjectInstanceRen *obi, VlakRen *vlr, short i1, short i2, short i3)
260 VertRen **vpp = &vlr->v1;
274 /* note, shi->mat is set in node shaders */
275 shi->mat = shi->mat_override ? shi->mat_override : vlr->mat;
277 shi->osatex = (shi->mat->texco & TEXCO_OSA);
278 shi->mode = shi->mat->mode_l; /* or-ed result for all nodes */
279 shi->mode2 = shi->mat->mode2_l;
281 /* facenormal copy, can get flipped */
283 RE_vlakren_get_normal(&R, obi, vlr, shi->facenor);
285 /* calculate vertexnormals */
286 if (vlr->flag & R_SMOOTH) {
287 copy_v3_v3(shi->n1, shi->v1->n);
288 copy_v3_v3(shi->n2, shi->v2->n);
289 copy_v3_v3(shi->n3, shi->v3->n);
291 if (obi->flag & R_TRANSFORMED) {
292 mul_m3_v3(obi->nmat, shi->n1); normalize_v3(shi->n1);
293 mul_m3_v3(obi->nmat, shi->n2); normalize_v3(shi->n2);
294 mul_m3_v3(obi->nmat, shi->n3); normalize_v3(shi->n3);
299 /* note, facenr declared volatile due to over-eager -O2 optimization's
300 * on cygwin (particularly -frerun-cse-after-loop)
303 /* copy data from face to ShadeInput, scanline case */
304 void shade_input_set_triangle(ShadeInput *shi, volatile int obi, volatile int facenr, int UNUSED(normal_flip))
307 shi->obi = &R.objectinstance[obi];
308 shi->obr = shi->obi->obr;
309 shi->facenr = (facenr - 1) & RE_QUAD_MASK;
310 if (shi->facenr < shi->obr->totvlak) {
311 VlakRen *vlr = RE_findOrAddVlak(shi->obr, shi->facenr);
313 if (facenr & RE_QUAD_OFFS)
314 shade_input_set_triangle_i(shi, shi->obi, vlr, 0, 2, 3);
316 shade_input_set_triangle_i(shi, shi->obi, vlr, 0, 1, 2);
319 shi->vlr = NULL; /* general signal we got sky */
322 shi->vlr = NULL; /* general signal we got sky */
325 /* full osa case: copy static info */
326 void shade_input_copy_triangle(ShadeInput *shi, ShadeInput *from)
328 /* not so nice, but works... warning is in RE_shader_ext.h */
329 memcpy(shi, from, sizeof(struct ShadeInputCopy));
332 /* copy data from strand to shadeinput */
333 void shade_input_set_strand(ShadeInput *shi, StrandRen *strand, StrandPoint *spoint)
335 /* note, shi->mat is set in node shaders */
336 shi->mat = shi->mat_override ? shi->mat_override : strand->buffer->ma;
338 shi->osatex = (shi->mat->texco & TEXCO_OSA);
339 shi->mode = shi->mat->mode_l; /* or-ed result for all nodes */
341 /* shade_input_set_viewco equivalent */
342 copy_v3_v3(shi->co, spoint->co);
343 copy_v3_v3(shi->view, shi->co);
344 normalize_v3(shi->view);
346 shi->xs = (int)spoint->x;
347 shi->ys = (int)spoint->y;
349 if (shi->osatex || (R.r.mode & R_SHADOW)) {
350 copy_v3_v3(shi->dxco, spoint->dtco);
351 copy_v3_v3(shi->dyco, spoint->dsco);
354 /* dxview, dyview, not supported */
356 /* facenormal, simply viewco flipped */
357 copy_v3_v3(shi->facenor, spoint->nor);
359 /* shade_input_set_normals equivalent */
360 if (shi->mat->mode & MA_TANGENT_STR) {
361 copy_v3_v3(shi->vn, spoint->tan);
366 cross_v3_v3v3(cross, spoint->co, spoint->tan);
367 cross_v3_v3v3(shi->vn, cross, spoint->tan);
368 normalize_v3(shi->vn);
370 if (dot_v3v3(shi->vn, shi->view) < 0.0f)
374 copy_v3_v3(shi->vno, shi->vn);
377 void shade_input_set_strand_texco(ShadeInput *shi, StrandRen *strand, StrandVert *svert, StrandPoint *spoint)
379 StrandBuffer *strandbuf = strand->buffer;
380 ObjectRen *obr = strandbuf->obr;
382 int mode = shi->mode; /* or-ed result for all nodes */
383 short texco = shi->mat->texco;
385 if ((shi->mat->texco & TEXCO_REFL)) {
386 /* shi->dxview, shi->dyview, not supported */
389 if (shi->osatex && (texco & (TEXCO_NORM | TEXCO_REFL))) {
393 if (mode & (MA_TANGENT_V | MA_NORMAP_TANG)) {
394 copy_v3_v3(shi->tang, spoint->tan);
395 copy_v3_v3(shi->nmaptang, spoint->tan);
398 if (mode & MA_STR_SURFDIFF) {
399 float *surfnor = RE_strandren_get_surfnor(obr, strand, 0);
402 copy_v3_v3(shi->surfnor, surfnor);
404 copy_v3_v3(shi->surfnor, shi->vn);
406 if (shi->mat->strand_surfnor > 0.0f) {
407 shi->surfdist = 0.0f;
408 for (sv = strand->vert; sv != svert; sv++)
409 shi->surfdist += len_v3v3(sv->co, (sv + 1)->co);
410 shi->surfdist += spoint->t * len_v3v3(sv->co, (sv + 1)->co);
414 if (R.r.mode & R_SPEED) {
417 speed = RE_strandren_get_winspeed(shi->obi, strand, 0);
419 copy_v4_v4(shi->winspeed, speed);
421 shi->winspeed[0] = shi->winspeed[1] = shi->winspeed[2] = shi->winspeed[3] = 0.0f;
424 /* shade_input_set_shade_texco equivalent */
425 if (texco & NEED_UV) {
426 if (texco & TEXCO_ORCO) {
427 copy_v3_v3(shi->lo, strand->orco);
428 /* no shi->osatex, orco derivatives are zero */
431 if (texco & TEXCO_GLOB) {
432 copy_v3_v3(shi->gl, shi->co);
433 mul_m4_v3(R.viewinv, shi->gl);
436 copy_v3_v3(shi->dxgl, shi->dxco);
437 mul_mat3_m4_v3(R.viewinv, shi->dxgl);
438 copy_v3_v3(shi->dygl, shi->dyco);
439 mul_mat3_m4_v3(R.viewinv, shi->dygl);
443 if (texco & TEXCO_STRAND) {
444 shi->strandco = spoint->strandco;
447 shi->dxstrand = spoint->dtstrandco;
448 shi->dystrand = 0.0f;
452 if ((texco & TEXCO_UV) || (mode & (MA_VERTEXCOL | MA_VERTEXCOLP | MA_FACETEXTURE))) {
460 shi->actuv = obr->actmtface;
461 shi->actcol = obr->actmcol;
463 if (mode & (MA_VERTEXCOL | MA_VERTEXCOLP)) {
464 for (i = 0; (mcol = RE_strandren_get_mcol(obr, strand, i, &name, 0)); i++) {
465 ShadeInputCol *scol = &shi->col[i];
466 char *cp = (char *)mcol;
471 scol->col[0] = cp[3] / 255.0f;
472 scol->col[1] = cp[2] / 255.0f;
473 scol->col[2] = cp[1] / 255.0f;
474 scol->col[3] = cp[0] / 255.0f;
478 shi->vcol[0] = shi->col[shi->actcol].col[0];
479 shi->vcol[1] = shi->col[shi->actcol].col[1];
480 shi->vcol[2] = shi->col[shi->actcol].col[2];
481 shi->vcol[3] = shi->col[shi->actcol].col[3];
491 for (i = 0; (uv = RE_strandren_get_uv(obr, strand, i, &name, 0)); i++) {
492 ShadeInputUV *suv = &shi->uv[i];
497 if (strandbuf->overrideuv == i) {
499 suv->uv[1] = spoint->strandco;
503 suv->uv[0] = -1.0f + 2.0f * uv[0];
504 suv->uv[1] = -1.0f + 2.0f * uv[1];
505 suv->uv[2] = 0.0f; /* texture.c assumes there are 3 coords */
515 if ((mode & MA_FACETEXTURE) && i == obr->actmtface) {
516 if ((mode & (MA_VERTEXCOL | MA_VERTEXCOLP)) == 0) {
525 if (shi->totuv == 0) {
526 ShadeInputUV *suv = &shi->uv[0];
529 suv->uv[1] = spoint->strandco;
530 suv->uv[2] = 0.0f; /* texture.c assumes there are 3 coords */
532 if (mode & MA_FACETEXTURE) {
533 /* no tface? set at 1.0f */
543 if (texco & TEXCO_NORM) {
544 shi->orn[0] = -shi->vn[0];
545 shi->orn[1] = -shi->vn[1];
546 shi->orn[2] = -shi->vn[2];
549 if (texco & TEXCO_STRESS) {
553 if (texco & TEXCO_TANGENT) {
554 if ((mode & MA_TANGENT_V) == 0) {
555 /* just prevent surprises */
556 shi->tang[0] = shi->tang[1] = shi->tang[2] = 0.0f;
557 shi->nmaptang[0] = shi->nmaptang[1] = shi->nmaptang[2] = 0.0f;
562 /* this only avalailable for scanline renders */
563 if (shi->depth == 0) {
564 if (texco & TEXCO_WINDOW) {
565 shi->winco[0] = -1.0f + 2.0f * spoint->x / (float)R.winx;
566 shi->winco[1] = -1.0f + 2.0f * spoint->y / (float)R.winy;
567 shi->winco[2] = 0.0f;
571 shi->dxwin[0] = 0.0f;
572 shi->dywin[1] = 0.0f;
573 shi->dxwin[0] = 0.0f;
574 shi->dywin[1] = 0.0f;
579 if (shi->do_manage) {
580 if (mode & (MA_VERTEXCOL | MA_VERTEXCOLP | MA_FACETEXTURE)) {
581 srgb_to_linearrgb_v3_v3(shi->vcol, shi->vcol);
587 /* from scanline pixel coordinates to 3d coordinates, requires set_triangle */
588 void shade_input_calc_viewco(ShadeInput *shi, float x, float y, float z, float view[3], float dxyview[2], float co[3], float dxco[3], float dyco[3])
590 /* returns not normalized, so is in viewplane coords */
591 calc_view_vector(view, x, y);
593 if (shi->mat->material_type == MA_TYPE_WIRE) {
594 /* wire cannot use normal for calculating shi->co, so
595 * we reconstruct the coordinate less accurate */
596 if (R.r.mode & R_ORTHO)
597 calc_renderco_ortho(co, x, y, z);
599 calc_renderco_zbuf(co, view, z);
602 /* for non-wire, intersect with the triangle to get the exact coord */
603 float fac, dface, v1[3];
605 copy_v3_v3(v1, shi->v1->co);
606 if (shi->obi->flag & R_TRANSFORMED)
607 mul_m4_v3(shi->obi->mat, v1);
609 dface = dot_v3v3(v1, shi->facenor);
611 /* ortho viewplane cannot intersect using view vector originating in (0,0,0) */
612 if (R.r.mode & R_ORTHO) {
613 /* x and y 3d coordinate can be derived from pixel coord and winmat */
614 float fx = 2.0f / (R.winx * R.winmat[0][0]);
615 float fy = 2.0f / (R.winy * R.winmat[1][1]);
617 co[0] = (x - 0.5f * R.winx) * fx - R.winmat[3][0] / R.winmat[0][0];
618 co[1] = (y - 0.5f * R.winy) * fy - R.winmat[3][1] / R.winmat[1][1];
620 /* using a*x + b*y + c*z = d equation, (a b c) is normal */
621 if (shi->facenor[2] != 0.0f)
622 co[2] = (dface - shi->facenor[0] * co[0] - shi->facenor[1] * co[1]) / shi->facenor[2];
629 if (shi->facenor[2] != 0.0f)
630 dxco[2] = -(shi->facenor[0] * fx) / shi->facenor[2];
636 if (shi->facenor[2] != 0.0f)
637 dyco[2] = -(shi->facenor[1] * fy) / shi->facenor[2];
642 if (co[2] != 0.0f) fac = 1.0f / co[2]; else fac = 0.0f;
643 dxyview[0] = -R.viewdx * fac;
644 dxyview[1] = -R.viewdy * fac;
651 div = dot_v3v3(shi->facenor, view);
652 if (div != 0.0f) fac = dface / div;
655 co[0] = fac * view[0];
656 co[1] = fac * view[1];
657 co[2] = fac * view[2];
659 /* pixel dx/dy for render coord */
661 float u = dface / (div - R.viewdx * shi->facenor[0]);
662 float v = dface / (div - R.viewdy * shi->facenor[1]);
664 dxco[0] = co[0] - (view[0] - R.viewdx) * u;
665 dxco[1] = co[1] - (view[1]) * u;
666 dxco[2] = co[2] - (view[2]) * u;
668 dyco[0] = co[0] - (view[0]) * v;
669 dyco[1] = co[1] - (view[1] - R.viewdy) * v;
670 dyco[2] = co[2] - (view[2]) * v;
673 if (fac != 0.0f) fac = 1.0f / fac;
674 dxyview[0] = -R.viewdx * fac;
675 dxyview[1] = -R.viewdy * fac;
681 /* set camera coords - for scanline, it's always 0.0,0.0,0.0 (render is in camera space)
682 * however for raytrace it can be different - the position of the last intersection */
683 shi->camera_co[0] = shi->camera_co[1] = shi->camera_co[2] = 0.0f;
685 /* cannot normalize earlier, code above needs it at viewplane level */
689 /* from scanline pixel coordinates to 3d coordinates, requires set_triangle */
690 void shade_input_set_viewco(ShadeInput *shi, float x, float y, float xs, float ys, float z)
692 float *dxyview = NULL, *dxco = NULL, *dyco = NULL;
694 /* currently in use for dithering (soft shadow), node preview, irregular shad */
698 /* original scanline coordinate without jitter */
703 /* check if we need derivatives */
704 if (shi->osatex || (R.r.mode & R_SHADOW)) {
708 if ((shi->mat->texco & TEXCO_REFL))
709 dxyview = &shi->dxview;
712 shade_input_calc_viewco(shi, xs, ys, z, shi->view, dxyview, shi->co, dxco, dyco);
715 /* calculate U and V, for scanline (silly render face u and v are in range -1 to 0) */
716 void shade_input_set_uv(ShadeInput *shi)
718 VlakRen *vlr = shi->vlr;
720 if ((vlr->flag & R_SMOOTH) || (shi->mat->texco & NEED_UV) || (shi->passflag & SCE_PASS_UV)) {
721 float v1[3], v2[3], v3[3];
723 copy_v3_v3(v1, shi->v1->co);
724 copy_v3_v3(v2, shi->v2->co);
725 copy_v3_v3(v3, shi->v3->co);
727 if (shi->obi->flag & R_TRANSFORMED) {
728 mul_m4_v3(shi->obi->mat, v1);
729 mul_m4_v3(shi->obi->mat, v2);
730 mul_m4_v3(shi->obi->mat, v3);
733 /* exception case for wire render of edge */
734 if (vlr->v2 == vlr->v3) {
737 lend = len_v3v3(v2, v1);
738 lenc = len_v3v3(shi->co, v1);
741 shi->u = shi->v = 0.0f;
744 shi->u = -(1.0f - lenc / lend);
756 /* most of this could become re-used for faces */
757 float detsh, t00, t10, t01, t11;
760 /* find most stable axis to project */
761 axis_dominant_v3(&axis1, &axis2, shi->facenor);
763 /* compute u,v and derivatives */
764 t00 = v3[axis1] - v1[axis1]; t01 = v3[axis2] - v1[axis2];
765 t10 = v3[axis1] - v2[axis1]; t11 = v3[axis2] - v2[axis2];
767 detsh = (t00 * t11 - t10 * t01);
768 detsh = (detsh != 0.0f) ? 1.0f / detsh : 0.0f;
769 t00 *= detsh; t01 *= detsh;
770 t10 *= detsh; t11 *= detsh;
772 shi->u = (shi->co[axis1] - v3[axis1]) * t11 - (shi->co[axis2] - v3[axis2]) * t10;
773 shi->v = (shi->co[axis2] - v3[axis2]) * t00 - (shi->co[axis1] - v3[axis1]) * t01;
775 shi->dx_u = shi->dxco[axis1] * t11 - shi->dxco[axis2] * t10;
776 shi->dx_v = shi->dxco[axis2] * t00 - shi->dxco[axis1] * t01;
777 shi->dy_u = shi->dyco[axis1] * t11 - shi->dyco[axis2] * t10;
778 shi->dy_v = shi->dyco[axis2] * t00 - shi->dyco[axis1] * t01;
781 /* u and v are in range -1 to 0, we allow a little bit extra but not too much, screws up speedvectors */
782 CLAMP(shi->u, -2.0f, 1.0f);
783 CLAMP(shi->v, -2.0f, 1.0f);
788 void shade_input_set_normals(ShadeInput *shi)
790 float u = shi->u, v = shi->v;
791 float l = 1.0f + u + v;
795 /* test flip normals to viewing direction */
796 if (!(shi->vlr->flag & R_TANGENT)) {
797 if (dot_v3v3(shi->facenor, shi->view) < 0.0f) {
798 negate_v3(shi->facenor);
803 /* calculate vertexnormals */
804 if (shi->vlr->flag & R_SMOOTH) {
805 float *n1 = shi->n1, *n2 = shi->n2, *n3 = shi->n3;
807 if (shi->flippednor) {
813 shi->vn[0] = l * n3[0] - u * n1[0] - v * n2[0];
814 shi->vn[1] = l * n3[1] - u * n1[1] - v * n2[1];
815 shi->vn[2] = l * n3[2] - u * n1[2] - v * n2[2];
817 /* use unnormalized normal (closer to games) */
818 copy_v3_v3(shi->nmapnorm, shi->vn);
820 normalize_v3(shi->vn);
823 copy_v3_v3(shi->vn, shi->facenor);
824 copy_v3_v3(shi->nmapnorm, shi->vn);
828 copy_v3_v3(shi->vno, shi->vn);
830 /* flip normals to viewing direction */
831 if (!(shi->vlr->flag & R_TANGENT))
832 if (dot_v3v3(shi->facenor, shi->view) < 0.0f)
833 shade_input_flip_normals(shi);
836 /* XXX shi->flippednor messes up otherwise */
837 void shade_input_set_vertex_normals(ShadeInput *shi)
839 float u = shi->u, v = shi->v;
840 float l = 1.0f + u + v;
842 /* calculate vertexnormals */
843 if (shi->vlr->flag & R_SMOOTH) {
844 const float *n1 = shi->n1, *n2 = shi->n2, *n3 = shi->n3;
846 shi->vn[0] = l * n3[0] - u * n1[0] - v * n2[0];
847 shi->vn[1] = l * n3[1] - u * n1[1] - v * n2[1];
848 shi->vn[2] = l * n3[2] - u * n1[2] - v * n2[2];
850 /* use unnormalized normal (closer to games) */
851 copy_v3_v3(shi->nmapnorm, shi->vn);
853 normalize_v3(shi->vn);
856 copy_v3_v3(shi->vn, shi->facenor);
857 copy_v3_v3(shi->nmapnorm, shi->vn);
861 copy_v3_v3(shi->vno, shi->vn);
865 /* use by raytrace, sss, bake to flip into the right direction */
866 void shade_input_flip_normals(ShadeInput *shi)
868 negate_v3(shi->facenor);
871 negate_v3(shi->nmapnorm);
872 shi->flippednor = !shi->flippednor;
875 void shade_input_set_shade_texco(ShadeInput *shi)
877 ObjectInstanceRen *obi = shi->obi;
878 ObjectRen *obr = shi->obr;
879 VertRen *v1 = shi->v1, *v2 = shi->v2, *v3 = shi->v3;
880 float u = shi->u, v = shi->v;
881 float l = 1.0f + u + v, dl;
882 int mode = shi->mode; /* or-ed result for all nodes */
883 short texco = shi->mat->texco;
886 if (shi->vlr->flag & R_SMOOTH) {
888 if (shi->osatex && (texco & (TEXCO_NORM | TEXCO_REFL)) ) {
889 float *n1 = shi->n1, *n2 = shi->n2, *n3 = shi->n3;
891 dl = shi->dx_u + shi->dx_v;
892 shi->dxno[0] = dl * n3[0] - shi->dx_u * n1[0] - shi->dx_v * n2[0];
893 shi->dxno[1] = dl * n3[1] - shi->dx_u * n1[1] - shi->dx_v * n2[1];
894 shi->dxno[2] = dl * n3[2] - shi->dx_u * n1[2] - shi->dx_v * n2[2];
895 dl = shi->dy_u + shi->dy_v;
896 shi->dyno[0] = dl * n3[0] - shi->dy_u * n1[0] - shi->dy_v * n2[0];
897 shi->dyno[1] = dl * n3[1] - shi->dy_u * n1[1] - shi->dy_v * n2[1];
898 shi->dyno[2] = dl * n3[2] - shi->dy_u * n1[2] - shi->dy_v * n2[2];
904 if (mode & (MA_TANGENT_V | MA_NORMAP_TANG) || R.flag & R_NEED_TANGENT) {
905 float *tangent, *s1, *s2, *s3;
908 if (shi->vlr->flag & R_SMOOTH) {
914 /* qdn: flat faces have tangents too,
915 * could pick either one, using average here */
921 shi->tang[0] = shi->tang[1] = shi->tang[2] = 0.0f;
922 shi->nmaptang[0] = shi->nmaptang[1] = shi->nmaptang[2] = 0.0f;
924 if (mode & MA_TANGENT_V) {
925 s1 = RE_vertren_get_tangent(obr, v1, 0);
926 s2 = RE_vertren_get_tangent(obr, v2, 0);
927 s3 = RE_vertren_get_tangent(obr, v3, 0);
929 if (s1 && s2 && s3) {
930 shi->tang[0] = (tl * s3[0] - tu * s1[0] - tv * s2[0]);
931 shi->tang[1] = (tl * s3[1] - tu * s1[1] - tv * s2[1]);
932 shi->tang[2] = (tl * s3[2] - tu * s1[2] - tv * s2[2]);
934 if (obi->flag & R_TRANSFORMED)
935 mul_m3_v3(obi->nmat, shi->tang);
937 normalize_v3(shi->tang);
938 copy_v3_v3(shi->nmaptang, shi->tang);
942 if (mode & MA_NORMAP_TANG || R.flag & R_NEED_TANGENT) {
943 tangent = RE_vlakren_get_nmap_tangent(obr, shi->vlr, 0);
946 int j1 = shi->i1, j2 = shi->i2, j3 = shi->i3;
947 float c0[3], c1[3], c2[3];
949 vlr_set_uv_indices(shi->vlr, &j1, &j2, &j3);
951 copy_v3_v3(c0, &tangent[j1 * 4]);
952 copy_v3_v3(c1, &tangent[j2 * 4]);
953 copy_v3_v3(c2, &tangent[j3 * 4]);
955 /* keeping tangents normalized at vertex level
956 * corresponds better to how it's done in game engines */
957 if (obi->flag & R_TRANSFORMED) {
958 mul_mat3_m4_v3(obi->mat, c0); normalize_v3(c0);
959 mul_mat3_m4_v3(obi->mat, c1); normalize_v3(c1);
960 mul_mat3_m4_v3(obi->mat, c2); normalize_v3(c2);
963 /* we don't normalize the interpolated TBN tangent
964 * corresponds better to how it's done in game engines */
965 shi->nmaptang[0] = (tl * c2[0] - tu * c0[0] - tv * c1[0]);
966 shi->nmaptang[1] = (tl * c2[1] - tu * c0[1] - tv * c1[1]);
967 shi->nmaptang[2] = (tl * c2[2] - tu * c0[2] - tv * c1[2]);
969 /* the sign is the same for all 3 vertices of any
970 * non degenerate triangle. */
971 shi->nmaptang[3] = tangent[j1 * 4 + 3];
976 if (mode & MA_STR_SURFDIFF) {
977 float *surfnor = RE_vlakren_get_surfnor(obr, shi->vlr, 0);
980 copy_v3_v3(shi->surfnor, surfnor);
981 if (obi->flag & R_TRANSFORMED)
982 mul_m3_v3(obi->nmat, shi->surfnor);
985 copy_v3_v3(shi->surfnor, shi->vn);
987 shi->surfdist = 0.0f;
990 if (R.r.mode & R_SPEED) {
993 s1 = RE_vertren_get_winspeed(obi, v1, 0);
994 s2 = RE_vertren_get_winspeed(obi, v2, 0);
995 s3 = RE_vertren_get_winspeed(obi, v3, 0);
996 if (s1 && s2 && s3) {
997 shi->winspeed[0] = (l * s3[0] - u * s1[0] - v * s2[0]);
998 shi->winspeed[1] = (l * s3[1] - u * s1[1] - v * s2[1]);
999 shi->winspeed[2] = (l * s3[2] - u * s1[2] - v * s2[2]);
1000 shi->winspeed[3] = (l * s3[3] - u * s1[3] - v * s2[3]);
1003 shi->winspeed[0] = shi->winspeed[1] = shi->winspeed[2] = shi->winspeed[3] = 0.0f;
1007 /* pass option forces UV calc */
1008 if ((shi->passflag & SCE_PASS_UV) || (R.flag & R_NEED_VCOL))
1009 texco |= (NEED_UV | TEXCO_UV);
1011 /* texture coordinates. shi->dxuv shi->dyuv have been set */
1012 if (texco & NEED_UV) {
1014 if (texco & TEXCO_ORCO) {
1016 float *o1, *o2, *o3;
1022 shi->lo[0] = l * o3[0] - u * o1[0] - v * o2[0];
1023 shi->lo[1] = l * o3[1] - u * o1[1] - v * o2[1];
1024 shi->lo[2] = l * o3[2] - u * o1[2] - v * o2[2];
1027 dl = shi->dx_u + shi->dx_v;
1028 shi->dxlo[0] = dl * o3[0] - shi->dx_u * o1[0] - shi->dx_v * o2[0];
1029 shi->dxlo[1] = dl * o3[1] - shi->dx_u * o1[1] - shi->dx_v * o2[1];
1030 shi->dxlo[2] = dl * o3[2] - shi->dx_u * o1[2] - shi->dx_v * o2[2];
1031 dl = shi->dy_u + shi->dy_v;
1032 shi->dylo[0] = dl * o3[0] - shi->dy_u * o1[0] - shi->dy_v * o2[0];
1033 shi->dylo[1] = dl * o3[1] - shi->dy_u * o1[1] - shi->dy_v * o2[1];
1034 shi->dylo[2] = dl * o3[2] - shi->dy_u * o1[2] - shi->dy_v * o2[2];
1038 copy_v3_v3(shi->duplilo, obi->dupliorco);
1041 if (texco & TEXCO_GLOB) {
1042 copy_v3_v3(shi->gl, shi->co);
1043 mul_m4_v3(R.viewinv, shi->gl);
1045 copy_v3_v3(shi->dxgl, shi->dxco);
1046 mul_mat3_m4_v3(R.viewinv, shi->dxgl);
1047 copy_v3_v3(shi->dygl, shi->dyco);
1048 mul_mat3_m4_v3(R.viewinv, shi->dygl);
1052 if (texco & TEXCO_STRAND) {
1053 shi->strandco = (l * v3->accum - u * v1->accum - v * v2->accum);
1055 dl = shi->dx_u + shi->dx_v;
1056 shi->dxstrand = dl * v3->accum - shi->dx_u * v1->accum - shi->dx_v * v2->accum;
1057 dl = shi->dy_u + shi->dy_v;
1058 shi->dystrand = dl * v3->accum - shi->dy_u * v1->accum - shi->dy_v * v2->accum;
1062 if ((texco & TEXCO_UV) || (mode & (MA_VERTEXCOL | MA_VERTEXCOLP | MA_FACETEXTURE)) || (R.flag & R_NEED_VCOL)) {
1063 VlakRen *vlr = shi->vlr;
1067 int i, j1 = shi->i1, j2 = shi->i2, j3 = shi->i3;
1069 /* uv and vcols are not copied on split, so set them according vlr divide flag */
1070 vlr_set_uv_indices(vlr, &j1, &j2, &j3);
1074 shi->actuv = obr->actmtface;
1075 shi->actcol = obr->actmcol;
1077 if ((mode & (MA_VERTEXCOL | MA_VERTEXCOLP)) || (R.flag & R_NEED_VCOL)) {
1078 for (i = 0; (mcol = RE_vlakren_get_mcol(obr, vlr, i, &name, 0)); i++) {
1079 ShadeInputCol *scol = &shi->col[i];
1080 char *cp1, *cp2, *cp3;
1086 cp1 = (char *)(mcol + j1);
1087 cp2 = (char *)(mcol + j2);
1088 cp3 = (char *)(mcol + j3);
1091 a[0] = ((float)cp1[0]) / 255.f;
1092 a[1] = ((float)cp2[0]) / 255.f;
1093 a[2] = ((float)cp3[0]) / 255.f;
1094 scol->col[3] = l * a[2] - u * a[0] - v * a[1];
1096 /* sample premultiplied color value */
1097 scol->col[0] = (l * ((float)cp3[3]) * a[2] - u * ((float)cp1[3]) * a[0] - v * ((float)cp2[3]) * a[1]) / 255.f;
1098 scol->col[1] = (l * ((float)cp3[2]) * a[2] - u * ((float)cp1[2]) * a[0] - v * ((float)cp2[2]) * a[1]) / 255.f;
1099 scol->col[2] = (l * ((float)cp3[1]) * a[2] - u * ((float)cp1[1]) * a[0] - v * ((float)cp2[1]) * a[1]) / 255.f;
1101 /* if not zero alpha, restore non-multiplied color */
1103 mul_v3_fl(scol->col, 1.0f / scol->col[3]);
1108 shi->vcol[0] = shi->col[shi->actcol].col[0];
1109 shi->vcol[1] = shi->col[shi->actcol].col[1];
1110 shi->vcol[2] = shi->col[shi->actcol].col[2];
1111 shi->vcol[3] = shi->col[shi->actcol].col[3];
1114 shi->vcol[0] = 0.0f;
1115 shi->vcol[1] = 0.0f;
1116 shi->vcol[2] = 0.0f;
1117 shi->vcol[3] = 1.0f;
1121 for (i = 0; (tface = RE_vlakren_get_tface(obr, vlr, i, &name, 0)); i++) {
1122 ShadeInputUV *suv = &shi->uv[i];
1123 const float *uv1 = tface->uv[j1];
1124 const float *uv2 = tface->uv[j2];
1125 const float *uv3 = tface->uv[j3];
1130 if ((shi->mat->mapflag & MA_MAPFLAG_UVPROJECT) && (shi->depth == 0)) {
1134 float s1[2] = {-1.0f + 2.0f * uv1[0], -1.0f + 2.0f * uv1[1]};
1135 float s2[2] = {-1.0f + 2.0f * uv2[0], -1.0f + 2.0f * uv2[1]};
1136 float s3[2] = {-1.0f + 2.0f * uv3[0], -1.0f + 2.0f * uv3[1]};
1139 float obwinmat[4][4], winmat[4][4], ho1[4], ho2[4], ho3[4];
1141 float hox, hoy, l, dl, u, v;
1142 float s00, s01, s10, s11, detsh;
1144 /* old globals, localized now */
1145 Zmulx = ((float)R.winx) / 2.0f;
1146 Zmuly = ((float)R.winy) / 2.0f;
1148 zbuf_make_winmat(&R, winmat);
1149 if (shi->obi->flag & R_TRANSFORMED)
1150 mul_m4_m4m4(obwinmat, winmat, obi->mat);
1152 copy_m4_m4(obwinmat, winmat);
1154 zbuf_render_project(obwinmat, v1->co, ho1);
1155 zbuf_render_project(obwinmat, v2->co, ho2);
1156 zbuf_render_project(obwinmat, v3->co, ho3);
1158 s00 = ho3[0] / ho3[3] - ho1[0] / ho1[3];
1159 s01 = ho3[1] / ho3[3] - ho1[1] / ho1[3];
1160 s10 = ho3[0] / ho3[3] - ho2[0] / ho2[3];
1161 s11 = ho3[1] / ho3[3] - ho2[1] / ho2[3];
1163 detsh = s00 * s11 - s10 * s01;
1164 detsh = (detsh != 0.0f) ? 1.0f / detsh : 0.0f;
1165 s00 *= detsh; s01 *= detsh;
1166 s10 *= detsh; s11 *= detsh;
1168 /* recalc u and v again */
1169 hox = x / Zmulx - 1.0f;
1170 hoy = y / Zmuly - 1.0f;
1171 u = (hox - ho3[0] / ho3[3]) * s11 - (hoy - ho3[1] / ho3[3]) * s10;
1172 v = (hoy - ho3[1] / ho3[3]) * s00 - (hox - ho3[0] / ho3[3]) * s01;
1175 suv->uv[0] = l * s3[0] - u * s1[0] - v * s2[0];
1176 suv->uv[1] = l * s3[1] - u * s1[1] - v * s2[1];
1180 float dxuv[2], dyuv[2];
1181 dxuv[0] = s11 / Zmulx;
1182 dxuv[1] = -s01 / Zmulx;
1183 dyuv[0] = -s10 / Zmuly;
1184 dyuv[1] = s00 / Zmuly;
1186 dl = dxuv[0] + dxuv[1];
1187 suv->dxuv[0] = dl * s3[0] - dxuv[0] * s1[0] - dxuv[1] * s2[0];
1188 suv->dxuv[1] = dl * s3[1] - dxuv[0] * s1[1] - dxuv[1] * s2[1];
1189 dl = dyuv[0] + dyuv[1];
1190 suv->dyuv[0] = dl * s3[0] - dyuv[0] * s1[0] - dyuv[1] * s2[0];
1191 suv->dyuv[1] = dl * s3[1] - dyuv[0] * s1[1] - dyuv[1] * s2[1];
1196 suv->uv[0] = -1.0f + 2.0f * (l * uv3[0] - u * uv1[0] - v * uv2[0]);
1197 suv->uv[1] = -1.0f + 2.0f * (l * uv3[1] - u * uv1[1] - v * uv2[1]);
1198 suv->uv[2] = 0.0f; /* texture.c assumes there are 3 coords */
1203 dl = shi->dx_u + shi->dx_v;
1207 suv->dxuv[0] = 2.0f * (dl * uv3[0] - duv[0] * uv1[0] - duv[1] * uv2[0]);
1208 suv->dxuv[1] = 2.0f * (dl * uv3[1] - duv[0] * uv1[1] - duv[1] * uv2[1]);
1210 dl = shi->dy_u + shi->dy_v;
1214 suv->dyuv[0] = 2.0f * (dl * uv3[0] - duv[0] * uv1[0] - duv[1] * uv2[0]);
1215 suv->dyuv[1] = 2.0f * (dl * uv3[1] - duv[0] * uv1[1] - duv[1] * uv2[1]);
1218 if ((mode & MA_FACETEXTURE) && i == obr->actmtface) {
1219 if (((mode & (MA_VERTEXCOL | MA_VERTEXCOLP)) == 0) && ((R.flag & R_NEED_VCOL) == 0)) {
1220 shi->vcol[0] = 1.0f;
1221 shi->vcol[1] = 1.0f;
1222 shi->vcol[2] = 1.0f;
1223 shi->vcol[3] = 1.0f;
1226 render_realtime_texture(shi, tface->tpage);
1232 shi->dupliuv[0] = -1.0f + 2.0f * obi->dupliuv[0];
1233 shi->dupliuv[1] = -1.0f + 2.0f * obi->dupliuv[1];
1234 shi->dupliuv[2] = 0.0f;
1236 if (shi->totuv == 0) {
1237 ShadeInputUV *suv = &shi->uv[0];
1239 suv->uv[0] = 2.0f * (u + .5f);
1240 suv->uv[1] = 2.0f * (v + .5f);
1241 suv->uv[2] = 0.0f; /* texture.c assumes there are 3 coords */
1243 if (mode & MA_FACETEXTURE) {
1244 /* no tface? set at 1.0f */
1245 shi->vcol[0] = 1.0f;
1246 shi->vcol[1] = 1.0f;
1247 shi->vcol[2] = 1.0f;
1248 shi->vcol[3] = 1.0f;
1253 if (texco & TEXCO_NORM) {
1254 shi->orn[0] = -shi->vn[0];
1255 shi->orn[1] = -shi->vn[1];
1256 shi->orn[2] = -shi->vn[2];
1259 if (texco & TEXCO_STRESS) {
1260 float *s1, *s2, *s3;
1262 s1 = RE_vertren_get_stress(obr, v1, 0);
1263 s2 = RE_vertren_get_stress(obr, v2, 0);
1264 s3 = RE_vertren_get_stress(obr, v3, 0);
1265 if (s1 && s2 && s3) {
1266 shi->stress = l * s3[0] - u * s1[0] - v * s2[0];
1267 if (shi->stress < 1.0f) shi->stress -= 1.0f;
1268 else shi->stress = (shi->stress - 1.0f) / shi->stress;
1270 else shi->stress = 0.0f;
1273 if (texco & TEXCO_TANGENT) {
1274 if ((mode & MA_TANGENT_V) == 0) {
1275 /* just prevent surprises */
1276 shi->tang[0] = shi->tang[1] = shi->tang[2] = 0.0f;
1277 shi->nmaptang[0] = shi->nmaptang[1] = shi->nmaptang[2] = 0.0f;
1282 /* this only avalailable for scanline renders */
1283 if (shi->depth == 0) {
1287 if (texco & TEXCO_WINDOW) {
1288 shi->winco[0] = -1.0f + 2.0f * x / (float)R.winx;
1289 shi->winco[1] = -1.0f + 2.0f * y / (float)R.winy;
1290 shi->winco[2] = 0.0f;
1292 shi->dxwin[0] = 2.0f / (float)R.winx;
1293 shi->dywin[1] = 2.0f / (float)R.winy;
1294 shi->dxwin[1] = shi->dxwin[2] = 0.0f;
1295 shi->dywin[0] = shi->dywin[2] = 0.0f;
1300 * Note! For raytracing winco is not set,
1301 * important because thus means all shader input's need to have their variables set to zero
1302 * else un-initialized values are used
1304 if (shi->do_manage) {
1305 if ((mode & (MA_VERTEXCOL | MA_VERTEXCOLP | MA_FACETEXTURE)) || (R.flag & R_NEED_VCOL)) {
1306 srgb_to_linearrgb_v3_v3(shi->vcol, shi->vcol);
1312 /* ****************** ShadeSample ************************************** */
1314 /* initialize per part, not per pixel! */
1315 void shade_input_initialize(ShadeInput *shi, RenderPart *pa, RenderLayer *rl, int sample)
1318 memset(shi, 0, sizeof(ShadeInput));
1320 shi->sample = sample;
1321 shi->thread = pa->thread;
1322 shi->do_preview = (R.r.scemode & R_MATNODE_PREVIEW) != 0;
1324 shi->do_manage = BKE_scene_check_color_management_enabled(R.scene);
1327 shi->layflag = rl->layflag;
1328 shi->passflag = rl->passflag;
1329 shi->combinedflag = ~rl->pass_xor;
1330 shi->mat_override = rl->mat_override;
1331 shi->light_override = rl->light_override;
1333 /* note shi.depth==0 means first hit, not raytracing */
1337 /* initialize per part, not per pixel! */
1338 void shade_sample_initialize(ShadeSample *ssamp, RenderPart *pa, RenderLayer *rl)
1342 tot = R.osa == 0 ? 1 : R.osa;
1344 for (a = 0; a < tot; a++) {
1345 shade_input_initialize(&ssamp->shi[a], pa, rl, a);
1346 memset(&ssamp->shr[a], 0, sizeof(ShadeResult));
1349 get_sample_layers(pa, rl, ssamp->rlpp);
1352 /* Do AO or (future) GI */
1353 void shade_samples_do_AO(ShadeSample *ssamp)
1355 if (!(R.r.mode & R_SHADOW))
1357 if (!(R.r.mode & R_RAYTRACE) && !(R.wrld.ao_gather_method == WO_AOGATHER_APPROX))
1360 if (R.wrld.mode & (WO_AMB_OCC | WO_ENV_LIGHT | WO_INDIRECT_LIGHT)) {
1361 ShadeInput *shi = &ssamp->shi[0];
1364 if (((shi->passflag & SCE_PASS_COMBINED) && (shi->combinedflag & (SCE_PASS_AO | SCE_PASS_ENVIRONMENT | SCE_PASS_INDIRECT))) ||
1365 (shi->passflag & (SCE_PASS_AO | SCE_PASS_ENVIRONMENT | SCE_PASS_INDIRECT)))
1367 for (sample = 0; sample < ssamp->tot; shi++, sample++)
1368 if (!(shi->mode & MA_SHLESS))
1369 ambient_occlusion(shi); /* stores in shi->ao[] */
1375 void shade_samples_fill_with_ps(ShadeSample *ssamp, PixStr *ps, int x, int y)
1382 for (shi = ssamp->shi; ps; ps = ps->next) {
1383 shade_input_set_triangle(shi, ps->obi, ps->facenr, 1);
1385 if (shi->vlr) { /* NULL happens for env material or for 'all z' */
1386 unsigned short curmask = ps->mask;
1388 /* full osa is only set for OSA renders */
1389 if (shi->vlr->flag & R_FULL_OSA) {
1390 short shi_cp = 0, samp;
1392 for (samp = 0; samp < R.osa; samp++) {
1393 if (curmask & (1 << samp)) {
1394 /* zbuffer has this inverse corrected, ensures xs,ys are inside pixel */
1395 xs = (float)x + R.jit[samp][0] + 0.5f;
1396 ys = (float)y + R.jit[samp][1] + 0.5f;
1399 shade_input_copy_triangle(shi, shi - 1);
1401 shi->mask = (1 << samp);
1402 // shi->rl= ssamp->rlpp[samp];
1403 shi->samplenr = R.shadowsamplenr[shi->thread]++; /* this counter is not being reset per pixel */
1404 shade_input_set_viewco(shi, x, y, xs, ys, (float)ps->z);
1405 shade_input_set_uv(shi);
1407 shade_input_set_normals(shi);
1408 else /* XXX shi->flippednor messes up otherwise */
1409 shade_input_set_vertex_normals(shi);
1418 short b = R.samples->centmask[curmask];
1419 xs = (float)x + R.samples->centLut[b & 15] + 0.5f;
1420 ys = (float)y + R.samples->centLut[b >> 4] + 0.5f;
1422 else if (R.i.curblur) {
1423 xs= (float)x + R.mblur_jit[R.i.curblur-1][0] + 0.5f;
1424 ys= (float)y + R.mblur_jit[R.i.curblur-1][1] + 0.5f;
1427 xs = (float)x + 0.5f;
1428 ys = (float)y + 0.5f;
1431 shi->mask = curmask;
1432 shi->samplenr = R.shadowsamplenr[shi->thread]++;
1433 shade_input_set_viewco(shi, x, y, xs, ys, (float)ps->z);
1434 shade_input_set_uv(shi);
1435 shade_input_set_normals(shi);
1439 /* total sample amount, shi->sample is static set in initialize */
1440 if (shi != ssamp->shi)
1441 ssamp->tot = (shi - 1)->sample + 1;
1446 /* shades samples, returns true if anything happened */
1447 int shade_samples(ShadeSample *ssamp, PixStr *ps, int x, int y)
1449 shade_samples_fill_with_ps(ssamp, ps, x, y);
1452 ShadeInput *shi = ssamp->shi;
1453 ShadeResult *shr = ssamp->shr;
1456 /* if shadow or AO? */
1457 shade_samples_do_AO(ssamp);
1459 /* if shade (all shadepinputs have same passflag) */
1460 if (ssamp->shi[0].passflag & ~(SCE_PASS_Z | SCE_PASS_INDEXOB | SCE_PASS_INDEXMA)) {
1462 for (samp = 0; samp < ssamp->tot; samp++, shi++, shr++) {
1463 shade_input_set_shade_texco(shi);
1464 shade_input_do_shade(shi, shr);
1467 else if (shi->passflag & SCE_PASS_Z) {
1468 for (samp = 0; samp < ssamp->tot; samp++, shi++, shr++)
1469 shr->z = -shi->co[2];