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 */
280 /* facenormal copy, can get flipped */
282 RE_vlakren_get_normal(&R, obi, vlr, shi->facenor);
284 /* calculate vertexnormals */
285 if (vlr->flag & R_SMOOTH) {
286 copy_v3_v3(shi->n1, shi->v1->n);
287 copy_v3_v3(shi->n2, shi->v2->n);
288 copy_v3_v3(shi->n3, shi->v3->n);
290 if (obi->flag & R_TRANSFORMED) {
291 mul_m3_v3(obi->nmat, shi->n1); normalize_v3(shi->n1);
292 mul_m3_v3(obi->nmat, shi->n2); normalize_v3(shi->n2);
293 mul_m3_v3(obi->nmat, shi->n3); normalize_v3(shi->n3);
298 /* note, facenr declared volatile due to over-eager -O2 optimization's
299 * on cygwin (particularly -frerun-cse-after-loop)
302 /* copy data from face to ShadeInput, scanline case */
303 void shade_input_set_triangle(ShadeInput *shi, volatile int obi, volatile int facenr, int UNUSED(normal_flip))
306 shi->obi = &R.objectinstance[obi];
307 shi->obr = shi->obi->obr;
308 shi->facenr = (facenr - 1) & RE_QUAD_MASK;
309 if (shi->facenr < shi->obr->totvlak) {
310 VlakRen *vlr = RE_findOrAddVlak(shi->obr, shi->facenr);
312 if (facenr & RE_QUAD_OFFS)
313 shade_input_set_triangle_i(shi, shi->obi, vlr, 0, 2, 3);
315 shade_input_set_triangle_i(shi, shi->obi, vlr, 0, 1, 2);
318 shi->vlr = NULL; /* general signal we got sky */
321 shi->vlr = NULL; /* general signal we got sky */
324 /* full osa case: copy static info */
325 void shade_input_copy_triangle(ShadeInput *shi, ShadeInput *from)
327 /* not so nice, but works... warning is in RE_shader_ext.h */
328 memcpy(shi, from, sizeof(struct ShadeInputCopy));
331 /* copy data from strand to shadeinput */
332 void shade_input_set_strand(ShadeInput *shi, StrandRen *strand, StrandPoint *spoint)
334 /* note, shi->mat is set in node shaders */
335 shi->mat = shi->mat_override ? shi->mat_override : strand->buffer->ma;
337 shi->osatex = (shi->mat->texco & TEXCO_OSA);
338 shi->mode = shi->mat->mode_l; /* or-ed result for all nodes */
340 /* shade_input_set_viewco equivalent */
341 copy_v3_v3(shi->co, spoint->co);
342 copy_v3_v3(shi->view, shi->co);
343 normalize_v3(shi->view);
345 shi->xs = (int)spoint->x;
346 shi->ys = (int)spoint->y;
348 if (shi->osatex || (R.r.mode & R_SHADOW)) {
349 copy_v3_v3(shi->dxco, spoint->dtco);
350 copy_v3_v3(shi->dyco, spoint->dsco);
353 /* dxview, dyview, not supported */
355 /* facenormal, simply viewco flipped */
356 copy_v3_v3(shi->facenor, spoint->nor);
358 /* shade_input_set_normals equivalent */
359 if (shi->mat->mode & MA_TANGENT_STR) {
360 copy_v3_v3(shi->vn, spoint->tan);
365 cross_v3_v3v3(cross, spoint->co, spoint->tan);
366 cross_v3_v3v3(shi->vn, cross, spoint->tan);
367 normalize_v3(shi->vn);
369 if (dot_v3v3(shi->vn, shi->view) < 0.0f)
373 copy_v3_v3(shi->vno, shi->vn);
376 void shade_input_set_strand_texco(ShadeInput *shi, StrandRen *strand, StrandVert *svert, StrandPoint *spoint)
378 StrandBuffer *strandbuf = strand->buffer;
379 ObjectRen *obr = strandbuf->obr;
381 int mode = shi->mode; /* or-ed result for all nodes */
382 short texco = shi->mat->texco;
384 if ((shi->mat->texco & TEXCO_REFL)) {
385 /* shi->dxview, shi->dyview, not supported */
388 if (shi->osatex && (texco & (TEXCO_NORM | TEXCO_REFL))) {
392 if (mode & (MA_TANGENT_V | MA_NORMAP_TANG)) {
393 copy_v3_v3(shi->tang, spoint->tan);
394 copy_v3_v3(shi->nmaptang, spoint->tan);
397 if (mode & MA_STR_SURFDIFF) {
398 float *surfnor = RE_strandren_get_surfnor(obr, strand, 0);
401 copy_v3_v3(shi->surfnor, surfnor);
403 copy_v3_v3(shi->surfnor, shi->vn);
405 if (shi->mat->strand_surfnor > 0.0f) {
406 shi->surfdist = 0.0f;
407 for (sv = strand->vert; sv != svert; sv++)
408 shi->surfdist += len_v3v3(sv->co, (sv + 1)->co);
409 shi->surfdist += spoint->t * len_v3v3(sv->co, (sv + 1)->co);
413 if (R.r.mode & R_SPEED) {
416 speed = RE_strandren_get_winspeed(shi->obi, strand, 0);
418 copy_v4_v4(shi->winspeed, speed);
420 shi->winspeed[0] = shi->winspeed[1] = shi->winspeed[2] = shi->winspeed[3] = 0.0f;
423 /* shade_input_set_shade_texco equivalent */
424 if (texco & NEED_UV) {
425 if (texco & TEXCO_ORCO) {
426 copy_v3_v3(shi->lo, strand->orco);
427 /* no shi->osatex, orco derivatives are zero */
430 if (texco & TEXCO_GLOB) {
431 copy_v3_v3(shi->gl, shi->co);
432 mul_m4_v3(R.viewinv, shi->gl);
435 copy_v3_v3(shi->dxgl, shi->dxco);
436 mul_mat3_m4_v3(R.viewinv, shi->dxgl);
437 copy_v3_v3(shi->dygl, shi->dyco);
438 mul_mat3_m4_v3(R.viewinv, shi->dygl);
442 if (texco & TEXCO_STRAND) {
443 shi->strandco = spoint->strandco;
446 shi->dxstrand = spoint->dtstrandco;
447 shi->dystrand = 0.0f;
451 if ((texco & TEXCO_UV) || (mode & (MA_VERTEXCOL | MA_VERTEXCOLP | MA_FACETEXTURE))) {
459 shi->actuv = obr->actmtface;
460 shi->actcol = obr->actmcol;
462 if (mode & (MA_VERTEXCOL | MA_VERTEXCOLP)) {
463 for (i = 0; (mcol = RE_strandren_get_mcol(obr, strand, i, &name, 0)); i++) {
464 ShadeInputCol *scol = &shi->col[i];
465 char *cp = (char *)mcol;
470 scol->col[0] = cp[3] / 255.0f;
471 scol->col[1] = cp[2] / 255.0f;
472 scol->col[2] = cp[1] / 255.0f;
473 scol->col[3] = cp[0] / 255.0f;
477 shi->vcol[0] = shi->col[shi->actcol].col[0];
478 shi->vcol[1] = shi->col[shi->actcol].col[1];
479 shi->vcol[2] = shi->col[shi->actcol].col[2];
480 shi->vcol[3] = shi->col[shi->actcol].col[3];
490 for (i = 0; (uv = RE_strandren_get_uv(obr, strand, i, &name, 0)); i++) {
491 ShadeInputUV *suv = &shi->uv[i];
496 if (strandbuf->overrideuv == i) {
498 suv->uv[1] = spoint->strandco;
502 suv->uv[0] = -1.0f + 2.0f * uv[0];
503 suv->uv[1] = -1.0f + 2.0f * uv[1];
504 suv->uv[2] = 0.0f; /* texture.c assumes there are 3 coords */
514 if ((mode & MA_FACETEXTURE) && i == obr->actmtface) {
515 if ((mode & (MA_VERTEXCOL | MA_VERTEXCOLP)) == 0) {
524 if (shi->totuv == 0) {
525 ShadeInputUV *suv = &shi->uv[0];
528 suv->uv[1] = spoint->strandco;
529 suv->uv[2] = 0.0f; /* texture.c assumes there are 3 coords */
531 if (mode & MA_FACETEXTURE) {
532 /* no tface? set at 1.0f */
542 if (texco & TEXCO_NORM) {
543 shi->orn[0] = -shi->vn[0];
544 shi->orn[1] = -shi->vn[1];
545 shi->orn[2] = -shi->vn[2];
548 if (texco & TEXCO_STRESS) {
552 if (texco & TEXCO_TANGENT) {
553 if ((mode & MA_TANGENT_V) == 0) {
554 /* just prevent surprises */
555 shi->tang[0] = shi->tang[1] = shi->tang[2] = 0.0f;
556 shi->nmaptang[0] = shi->nmaptang[1] = shi->nmaptang[2] = 0.0f;
561 /* this only avalailable for scanline renders */
562 if (shi->depth == 0) {
563 if (texco & TEXCO_WINDOW) {
564 shi->winco[0] = -1.0f + 2.0f * spoint->x / (float)R.winx;
565 shi->winco[1] = -1.0f + 2.0f * spoint->y / (float)R.winy;
566 shi->winco[2] = 0.0f;
570 shi->dxwin[0] = 0.0f;
571 shi->dywin[1] = 0.0f;
572 shi->dxwin[0] = 0.0f;
573 shi->dywin[1] = 0.0f;
578 if (shi->do_manage) {
579 if (mode & (MA_VERTEXCOL | MA_VERTEXCOLP | MA_FACETEXTURE)) {
580 srgb_to_linearrgb_v3_v3(shi->vcol, shi->vcol);
586 /* from scanline pixel coordinates to 3d coordinates, requires set_triangle */
587 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])
589 /* returns not normalized, so is in viewplane coords */
590 calc_view_vector(view, x, y);
592 if (shi->mat->material_type == MA_TYPE_WIRE) {
593 /* wire cannot use normal for calculating shi->co, so
594 * we reconstruct the coordinate less accurate */
595 if (R.r.mode & R_ORTHO)
596 calc_renderco_ortho(co, x, y, z);
598 calc_renderco_zbuf(co, view, z);
601 /* for non-wire, intersect with the triangle to get the exact coord */
602 float fac, dface, v1[3];
604 copy_v3_v3(v1, shi->v1->co);
605 if (shi->obi->flag & R_TRANSFORMED)
606 mul_m4_v3(shi->obi->mat, v1);
608 dface = dot_v3v3(v1, shi->facenor);
610 /* ortho viewplane cannot intersect using view vector originating in (0,0,0) */
611 if (R.r.mode & R_ORTHO) {
612 /* x and y 3d coordinate can be derived from pixel coord and winmat */
613 float fx = 2.0f / (R.winx * R.winmat[0][0]);
614 float fy = 2.0f / (R.winy * R.winmat[1][1]);
616 co[0] = (x - 0.5f * R.winx) * fx - R.winmat[3][0] / R.winmat[0][0];
617 co[1] = (y - 0.5f * R.winy) * fy - R.winmat[3][1] / R.winmat[1][1];
619 /* using a*x + b*y + c*z = d equation, (a b c) is normal */
620 if (shi->facenor[2] != 0.0f)
621 co[2] = (dface - shi->facenor[0] * co[0] - shi->facenor[1] * co[1]) / shi->facenor[2];
628 if (shi->facenor[2] != 0.0f)
629 dxco[2] = -(shi->facenor[0] * fx) / shi->facenor[2];
635 if (shi->facenor[2] != 0.0f)
636 dyco[2] = -(shi->facenor[1] * fy) / shi->facenor[2];
641 if (co[2] != 0.0f) fac = 1.0f / co[2]; else fac = 0.0f;
642 dxyview[0] = -R.viewdx * fac;
643 dxyview[1] = -R.viewdy * fac;
650 div = dot_v3v3(shi->facenor, view);
651 if (div != 0.0f) fac = dface / div;
654 co[0] = fac * view[0];
655 co[1] = fac * view[1];
656 co[2] = fac * view[2];
658 /* pixel dx/dy for render coord */
660 float u = dface / (div - R.viewdx * shi->facenor[0]);
661 float v = dface / (div - R.viewdy * shi->facenor[1]);
663 dxco[0] = co[0] - (view[0] - R.viewdx) * u;
664 dxco[1] = co[1] - (view[1]) * u;
665 dxco[2] = co[2] - (view[2]) * u;
667 dyco[0] = co[0] - (view[0]) * v;
668 dyco[1] = co[1] - (view[1] - R.viewdy) * v;
669 dyco[2] = co[2] - (view[2]) * v;
672 if (fac != 0.0f) fac = 1.0f / fac;
673 dxyview[0] = -R.viewdx * fac;
674 dxyview[1] = -R.viewdy * fac;
680 /* set camera coords - for scanline, it's always 0.0,0.0,0.0 (render is in camera space)
681 * however for raytrace it can be different - the position of the last intersection */
682 shi->camera_co[0] = shi->camera_co[1] = shi->camera_co[2] = 0.0f;
684 /* cannot normalize earlier, code above needs it at viewplane level */
688 /* from scanline pixel coordinates to 3d coordinates, requires set_triangle */
689 void shade_input_set_viewco(ShadeInput *shi, float x, float y, float xs, float ys, float z)
691 float *dxyview = NULL, *dxco = NULL, *dyco = NULL;
693 /* currently in use for dithering (soft shadow), node preview, irregular shad */
697 /* original scanline coordinate without jitter */
702 /* check if we need derivatives */
703 if (shi->osatex || (R.r.mode & R_SHADOW)) {
707 if ((shi->mat->texco & TEXCO_REFL))
708 dxyview = &shi->dxview;
711 shade_input_calc_viewco(shi, xs, ys, z, shi->view, dxyview, shi->co, dxco, dyco);
714 /* calculate U and V, for scanline (silly render face u and v are in range -1 to 0) */
715 void shade_input_set_uv(ShadeInput *shi)
717 VlakRen *vlr = shi->vlr;
719 if ((vlr->flag & R_SMOOTH) || (shi->mat->texco & NEED_UV) || (shi->passflag & SCE_PASS_UV)) {
720 float v1[3], v2[3], v3[3];
722 copy_v3_v3(v1, shi->v1->co);
723 copy_v3_v3(v2, shi->v2->co);
724 copy_v3_v3(v3, shi->v3->co);
726 if (shi->obi->flag & R_TRANSFORMED) {
727 mul_m4_v3(shi->obi->mat, v1);
728 mul_m4_v3(shi->obi->mat, v2);
729 mul_m4_v3(shi->obi->mat, v3);
732 /* exception case for wire render of edge */
733 if (vlr->v2 == vlr->v3) {
736 lend = len_v3v3(v2, v1);
737 lenc = len_v3v3(shi->co, v1);
740 shi->u = shi->v = 0.0f;
743 shi->u = -(1.0f - lenc / lend);
755 /* most of this could become re-used for faces */
756 float detsh, t00, t10, t01, t11;
759 /* find most stable axis to project */
760 axis_dominant_v3(&axis1, &axis2, shi->facenor);
762 /* compute u,v and derivatives */
763 t00 = v3[axis1] - v1[axis1]; t01 = v3[axis2] - v1[axis2];
764 t10 = v3[axis1] - v2[axis1]; t11 = v3[axis2] - v2[axis2];
766 detsh = (t00 * t11 - t10 * t01);
767 detsh = (detsh != 0.0f) ? 1.0f / detsh : 0.0f;
768 t00 *= detsh; t01 *= detsh;
769 t10 *= detsh; t11 *= detsh;
771 shi->u = (shi->co[axis1] - v3[axis1]) * t11 - (shi->co[axis2] - v3[axis2]) * t10;
772 shi->v = (shi->co[axis2] - v3[axis2]) * t00 - (shi->co[axis1] - v3[axis1]) * t01;
774 shi->dx_u = shi->dxco[axis1] * t11 - shi->dxco[axis2] * t10;
775 shi->dx_v = shi->dxco[axis2] * t00 - shi->dxco[axis1] * t01;
776 shi->dy_u = shi->dyco[axis1] * t11 - shi->dyco[axis2] * t10;
777 shi->dy_v = shi->dyco[axis2] * t00 - shi->dyco[axis1] * t01;
780 /* u and v are in range -1 to 0, we allow a little bit extra but not too much, screws up speedvectors */
781 CLAMP(shi->u, -2.0f, 1.0f);
782 CLAMP(shi->v, -2.0f, 1.0f);
787 void shade_input_set_normals(ShadeInput *shi)
789 float u = shi->u, v = shi->v;
790 float l = 1.0f + u + v;
794 /* test flip normals to viewing direction */
795 if (!(shi->vlr->flag & R_TANGENT)) {
796 if (dot_v3v3(shi->facenor, shi->view) < 0.0f) {
797 negate_v3(shi->facenor);
802 /* calculate vertexnormals */
803 if (shi->vlr->flag & R_SMOOTH) {
804 float *n1 = shi->n1, *n2 = shi->n2, *n3 = shi->n3;
806 if (shi->flippednor) {
812 shi->vn[0] = l * n3[0] - u * n1[0] - v * n2[0];
813 shi->vn[1] = l * n3[1] - u * n1[1] - v * n2[1];
814 shi->vn[2] = l * n3[2] - u * n1[2] - v * n2[2];
816 /* use unnormalized normal (closer to games) */
817 copy_v3_v3(shi->nmapnorm, shi->vn);
819 normalize_v3(shi->vn);
822 copy_v3_v3(shi->vn, shi->facenor);
823 copy_v3_v3(shi->nmapnorm, shi->vn);
827 copy_v3_v3(shi->vno, shi->vn);
829 /* flip normals to viewing direction */
830 if (!(shi->vlr->flag & R_TANGENT))
831 if (dot_v3v3(shi->facenor, shi->view) < 0.0f)
832 shade_input_flip_normals(shi);
835 /* XXX shi->flippednor messes up otherwise */
836 void shade_input_set_vertex_normals(ShadeInput *shi)
838 float u = shi->u, v = shi->v;
839 float l = 1.0f + u + v;
841 /* calculate vertexnormals */
842 if (shi->vlr->flag & R_SMOOTH) {
843 const float *n1 = shi->n1, *n2 = shi->n2, *n3 = shi->n3;
845 shi->vn[0] = l * n3[0] - u * n1[0] - v * n2[0];
846 shi->vn[1] = l * n3[1] - u * n1[1] - v * n2[1];
847 shi->vn[2] = l * n3[2] - u * n1[2] - v * n2[2];
849 /* use unnormalized normal (closer to games) */
850 copy_v3_v3(shi->nmapnorm, shi->vn);
852 normalize_v3(shi->vn);
855 copy_v3_v3(shi->vn, shi->facenor);
856 copy_v3_v3(shi->nmapnorm, shi->vn);
860 copy_v3_v3(shi->vno, shi->vn);
864 /* use by raytrace, sss, bake to flip into the right direction */
865 void shade_input_flip_normals(ShadeInput *shi)
867 negate_v3(shi->facenor);
870 negate_v3(shi->nmapnorm);
871 shi->flippednor = !shi->flippednor;
874 void shade_input_set_shade_texco(ShadeInput *shi)
876 ObjectInstanceRen *obi = shi->obi;
877 ObjectRen *obr = shi->obr;
878 VertRen *v1 = shi->v1, *v2 = shi->v2, *v3 = shi->v3;
879 float u = shi->u, v = shi->v;
880 float l = 1.0f + u + v, dl;
881 int mode = shi->mode; /* or-ed result for all nodes */
882 short texco = shi->mat->texco;
885 if (shi->vlr->flag & R_SMOOTH) {
887 if (shi->osatex && (texco & (TEXCO_NORM | TEXCO_REFL)) ) {
888 float *n1 = shi->n1, *n2 = shi->n2, *n3 = shi->n3;
890 dl = shi->dx_u + shi->dx_v;
891 shi->dxno[0] = dl * n3[0] - shi->dx_u * n1[0] - shi->dx_v * n2[0];
892 shi->dxno[1] = dl * n3[1] - shi->dx_u * n1[1] - shi->dx_v * n2[1];
893 shi->dxno[2] = dl * n3[2] - shi->dx_u * n1[2] - shi->dx_v * n2[2];
894 dl = shi->dy_u + shi->dy_v;
895 shi->dyno[0] = dl * n3[0] - shi->dy_u * n1[0] - shi->dy_v * n2[0];
896 shi->dyno[1] = dl * n3[1] - shi->dy_u * n1[1] - shi->dy_v * n2[1];
897 shi->dyno[2] = dl * n3[2] - shi->dy_u * n1[2] - shi->dy_v * n2[2];
903 if (mode & (MA_TANGENT_V | MA_NORMAP_TANG) || R.flag & R_NEED_TANGENT) {
904 float *tangent, *s1, *s2, *s3;
907 if (shi->vlr->flag & R_SMOOTH) {
913 /* qdn: flat faces have tangents too,
914 * could pick either one, using average here */
920 shi->tang[0] = shi->tang[1] = shi->tang[2] = 0.0f;
921 shi->nmaptang[0] = shi->nmaptang[1] = shi->nmaptang[2] = 0.0f;
923 if (mode & MA_TANGENT_V) {
924 s1 = RE_vertren_get_tangent(obr, v1, 0);
925 s2 = RE_vertren_get_tangent(obr, v2, 0);
926 s3 = RE_vertren_get_tangent(obr, v3, 0);
928 if (s1 && s2 && s3) {
929 shi->tang[0] = (tl * s3[0] - tu * s1[0] - tv * s2[0]);
930 shi->tang[1] = (tl * s3[1] - tu * s1[1] - tv * s2[1]);
931 shi->tang[2] = (tl * s3[2] - tu * s1[2] - tv * s2[2]);
933 if (obi->flag & R_TRANSFORMED)
934 mul_m3_v3(obi->nmat, shi->tang);
936 normalize_v3(shi->tang);
937 copy_v3_v3(shi->nmaptang, shi->tang);
941 if (mode & MA_NORMAP_TANG || R.flag & R_NEED_TANGENT) {
942 tangent = RE_vlakren_get_nmap_tangent(obr, shi->vlr, 0);
945 int j1 = shi->i1, j2 = shi->i2, j3 = shi->i3;
946 float c0[3], c1[3], c2[3];
948 vlr_set_uv_indices(shi->vlr, &j1, &j2, &j3);
950 copy_v3_v3(c0, &tangent[j1 * 4]);
951 copy_v3_v3(c1, &tangent[j2 * 4]);
952 copy_v3_v3(c2, &tangent[j3 * 4]);
954 /* keeping tangents normalized at vertex level
955 * corresponds better to how it's done in game engines */
956 if (obi->flag & R_TRANSFORMED) {
957 mul_mat3_m4_v3(obi->mat, c0); normalize_v3(c0);
958 mul_mat3_m4_v3(obi->mat, c1); normalize_v3(c1);
959 mul_mat3_m4_v3(obi->mat, c2); normalize_v3(c2);
962 /* we don't normalize the interpolated TBN tangent
963 * corresponds better to how it's done in game engines */
964 shi->nmaptang[0] = (tl * c2[0] - tu * c0[0] - tv * c1[0]);
965 shi->nmaptang[1] = (tl * c2[1] - tu * c0[1] - tv * c1[1]);
966 shi->nmaptang[2] = (tl * c2[2] - tu * c0[2] - tv * c1[2]);
968 /* the sign is the same for all 3 vertices of any
969 * non degenerate triangle. */
970 shi->nmaptang[3] = tangent[j1 * 4 + 3];
975 if (mode & MA_STR_SURFDIFF) {
976 float *surfnor = RE_vlakren_get_surfnor(obr, shi->vlr, 0);
979 copy_v3_v3(shi->surfnor, surfnor);
980 if (obi->flag & R_TRANSFORMED)
981 mul_m3_v3(obi->nmat, shi->surfnor);
984 copy_v3_v3(shi->surfnor, shi->vn);
986 shi->surfdist = 0.0f;
989 if (R.r.mode & R_SPEED) {
992 s1 = RE_vertren_get_winspeed(obi, v1, 0);
993 s2 = RE_vertren_get_winspeed(obi, v2, 0);
994 s3 = RE_vertren_get_winspeed(obi, v3, 0);
995 if (s1 && s2 && s3) {
996 shi->winspeed[0] = (l * s3[0] - u * s1[0] - v * s2[0]);
997 shi->winspeed[1] = (l * s3[1] - u * s1[1] - v * s2[1]);
998 shi->winspeed[2] = (l * s3[2] - u * s1[2] - v * s2[2]);
999 shi->winspeed[3] = (l * s3[3] - u * s1[3] - v * s2[3]);
1002 shi->winspeed[0] = shi->winspeed[1] = shi->winspeed[2] = shi->winspeed[3] = 0.0f;
1006 /* pass option forces UV calc */
1007 if ((shi->passflag & SCE_PASS_UV) || (R.flag & R_NEED_VCOL))
1008 texco |= (NEED_UV | TEXCO_UV);
1010 /* texture coordinates. shi->dxuv shi->dyuv have been set */
1011 if (texco & NEED_UV) {
1013 if (texco & TEXCO_ORCO) {
1015 float *o1, *o2, *o3;
1021 shi->lo[0] = l * o3[0] - u * o1[0] - v * o2[0];
1022 shi->lo[1] = l * o3[1] - u * o1[1] - v * o2[1];
1023 shi->lo[2] = l * o3[2] - u * o1[2] - v * o2[2];
1026 dl = shi->dx_u + shi->dx_v;
1027 shi->dxlo[0] = dl * o3[0] - shi->dx_u * o1[0] - shi->dx_v * o2[0];
1028 shi->dxlo[1] = dl * o3[1] - shi->dx_u * o1[1] - shi->dx_v * o2[1];
1029 shi->dxlo[2] = dl * o3[2] - shi->dx_u * o1[2] - shi->dx_v * o2[2];
1030 dl = shi->dy_u + shi->dy_v;
1031 shi->dylo[0] = dl * o3[0] - shi->dy_u * o1[0] - shi->dy_v * o2[0];
1032 shi->dylo[1] = dl * o3[1] - shi->dy_u * o1[1] - shi->dy_v * o2[1];
1033 shi->dylo[2] = dl * o3[2] - shi->dy_u * o1[2] - shi->dy_v * o2[2];
1037 copy_v3_v3(shi->duplilo, obi->dupliorco);
1040 if (texco & TEXCO_GLOB) {
1041 copy_v3_v3(shi->gl, shi->co);
1042 mul_m4_v3(R.viewinv, shi->gl);
1044 copy_v3_v3(shi->dxgl, shi->dxco);
1045 mul_mat3_m4_v3(R.viewinv, shi->dxgl);
1046 copy_v3_v3(shi->dygl, shi->dyco);
1047 mul_mat3_m4_v3(R.viewinv, shi->dygl);
1051 if (texco & TEXCO_STRAND) {
1052 shi->strandco = (l * v3->accum - u * v1->accum - v * v2->accum);
1054 dl = shi->dx_u + shi->dx_v;
1055 shi->dxstrand = dl * v3->accum - shi->dx_u * v1->accum - shi->dx_v * v2->accum;
1056 dl = shi->dy_u + shi->dy_v;
1057 shi->dystrand = dl * v3->accum - shi->dy_u * v1->accum - shi->dy_v * v2->accum;
1061 if ((texco & TEXCO_UV) || (mode & (MA_VERTEXCOL | MA_VERTEXCOLP | MA_FACETEXTURE)) || (R.flag & R_NEED_VCOL)) {
1062 VlakRen *vlr = shi->vlr;
1066 int i, j1 = shi->i1, j2 = shi->i2, j3 = shi->i3;
1068 /* uv and vcols are not copied on split, so set them according vlr divide flag */
1069 vlr_set_uv_indices(vlr, &j1, &j2, &j3);
1073 shi->actuv = obr->actmtface;
1074 shi->actcol = obr->actmcol;
1076 if ((mode & (MA_VERTEXCOL | MA_VERTEXCOLP)) || (R.flag & R_NEED_VCOL)) {
1077 for (i = 0; (mcol = RE_vlakren_get_mcol(obr, vlr, i, &name, 0)); i++) {
1078 ShadeInputCol *scol = &shi->col[i];
1079 char *cp1, *cp2, *cp3;
1085 cp1 = (char *)(mcol + j1);
1086 cp2 = (char *)(mcol + j2);
1087 cp3 = (char *)(mcol + j3);
1090 a[0] = ((float)cp1[0]) / 255.f;
1091 a[1] = ((float)cp2[0]) / 255.f;
1092 a[2] = ((float)cp3[0]) / 255.f;
1093 scol->col[3] = l * a[2] - u * a[0] - v * a[1];
1095 /* sample premultiplied color value */
1096 scol->col[0] = (l * ((float)cp3[3]) * a[2] - u * ((float)cp1[3]) * a[0] - v * ((float)cp2[3]) * a[1]) / 255.f;
1097 scol->col[1] = (l * ((float)cp3[2]) * a[2] - u * ((float)cp1[2]) * a[0] - v * ((float)cp2[2]) * a[1]) / 255.f;
1098 scol->col[2] = (l * ((float)cp3[1]) * a[2] - u * ((float)cp1[1]) * a[0] - v * ((float)cp2[1]) * a[1]) / 255.f;
1100 /* if not zero alpha, restore non-multiplied color */
1102 mul_v3_fl(scol->col, 1.0f / scol->col[3]);
1107 shi->vcol[0] = shi->col[shi->actcol].col[0];
1108 shi->vcol[1] = shi->col[shi->actcol].col[1];
1109 shi->vcol[2] = shi->col[shi->actcol].col[2];
1110 shi->vcol[3] = shi->col[shi->actcol].col[3];
1113 shi->vcol[0] = 0.0f;
1114 shi->vcol[1] = 0.0f;
1115 shi->vcol[2] = 0.0f;
1116 shi->vcol[3] = 1.0f;
1120 for (i = 0; (tface = RE_vlakren_get_tface(obr, vlr, i, &name, 0)); i++) {
1121 ShadeInputUV *suv = &shi->uv[i];
1122 const float *uv1 = tface->uv[j1];
1123 const float *uv2 = tface->uv[j2];
1124 const float *uv3 = tface->uv[j3];
1129 if ((shi->mat->mapflag & MA_MAPFLAG_UVPROJECT) && (shi->depth == 0)) {
1133 float s1[2] = {-1.0f + 2.0f * uv1[0], -1.0f + 2.0f * uv1[1]};
1134 float s2[2] = {-1.0f + 2.0f * uv2[0], -1.0f + 2.0f * uv2[1]};
1135 float s3[2] = {-1.0f + 2.0f * uv3[0], -1.0f + 2.0f * uv3[1]};
1138 float obwinmat[4][4], winmat[4][4], ho1[4], ho2[4], ho3[4];
1140 float hox, hoy, l, dl, u, v;
1141 float s00, s01, s10, s11, detsh;
1143 /* old globals, localized now */
1144 Zmulx = ((float)R.winx) / 2.0f;
1145 Zmuly = ((float)R.winy) / 2.0f;
1147 zbuf_make_winmat(&R, winmat);
1148 if (shi->obi->flag & R_TRANSFORMED)
1149 mul_m4_m4m4(obwinmat, winmat, obi->mat);
1151 copy_m4_m4(obwinmat, winmat);
1153 zbuf_render_project(obwinmat, v1->co, ho1);
1154 zbuf_render_project(obwinmat, v2->co, ho2);
1155 zbuf_render_project(obwinmat, v3->co, ho3);
1157 s00 = ho3[0] / ho3[3] - ho1[0] / ho1[3];
1158 s01 = ho3[1] / ho3[3] - ho1[1] / ho1[3];
1159 s10 = ho3[0] / ho3[3] - ho2[0] / ho2[3];
1160 s11 = ho3[1] / ho3[3] - ho2[1] / ho2[3];
1162 detsh = s00 * s11 - s10 * s01;
1163 detsh = (detsh != 0.0f) ? 1.0f / detsh : 0.0f;
1164 s00 *= detsh; s01 *= detsh;
1165 s10 *= detsh; s11 *= detsh;
1167 /* recalc u and v again */
1168 hox = x / Zmulx - 1.0f;
1169 hoy = y / Zmuly - 1.0f;
1170 u = (hox - ho3[0] / ho3[3]) * s11 - (hoy - ho3[1] / ho3[3]) * s10;
1171 v = (hoy - ho3[1] / ho3[3]) * s00 - (hox - ho3[0] / ho3[3]) * s01;
1174 suv->uv[0] = l * s3[0] - u * s1[0] - v * s2[0];
1175 suv->uv[1] = l * s3[1] - u * s1[1] - v * s2[1];
1179 float dxuv[2], dyuv[2];
1180 dxuv[0] = s11 / Zmulx;
1181 dxuv[1] = -s01 / Zmulx;
1182 dyuv[0] = -s10 / Zmuly;
1183 dyuv[1] = s00 / Zmuly;
1185 dl = dxuv[0] + dxuv[1];
1186 suv->dxuv[0] = dl * s3[0] - dxuv[0] * s1[0] - dxuv[1] * s2[0];
1187 suv->dxuv[1] = dl * s3[1] - dxuv[0] * s1[1] - dxuv[1] * s2[1];
1188 dl = dyuv[0] + dyuv[1];
1189 suv->dyuv[0] = dl * s3[0] - dyuv[0] * s1[0] - dyuv[1] * s2[0];
1190 suv->dyuv[1] = dl * s3[1] - dyuv[0] * s1[1] - dyuv[1] * s2[1];
1195 suv->uv[0] = -1.0f + 2.0f * (l * uv3[0] - u * uv1[0] - v * uv2[0]);
1196 suv->uv[1] = -1.0f + 2.0f * (l * uv3[1] - u * uv1[1] - v * uv2[1]);
1197 suv->uv[2] = 0.0f; /* texture.c assumes there are 3 coords */
1202 dl = shi->dx_u + shi->dx_v;
1206 suv->dxuv[0] = 2.0f * (dl * uv3[0] - duv[0] * uv1[0] - duv[1] * uv2[0]);
1207 suv->dxuv[1] = 2.0f * (dl * uv3[1] - duv[0] * uv1[1] - duv[1] * uv2[1]);
1209 dl = shi->dy_u + shi->dy_v;
1213 suv->dyuv[0] = 2.0f * (dl * uv3[0] - duv[0] * uv1[0] - duv[1] * uv2[0]);
1214 suv->dyuv[1] = 2.0f * (dl * uv3[1] - duv[0] * uv1[1] - duv[1] * uv2[1]);
1217 if ((mode & MA_FACETEXTURE) && i == obr->actmtface) {
1218 if (((mode & (MA_VERTEXCOL | MA_VERTEXCOLP)) == 0) && ((R.flag & R_NEED_VCOL) == 0)) {
1219 shi->vcol[0] = 1.0f;
1220 shi->vcol[1] = 1.0f;
1221 shi->vcol[2] = 1.0f;
1222 shi->vcol[3] = 1.0f;
1225 render_realtime_texture(shi, tface->tpage);
1231 shi->dupliuv[0] = -1.0f + 2.0f * obi->dupliuv[0];
1232 shi->dupliuv[1] = -1.0f + 2.0f * obi->dupliuv[1];
1233 shi->dupliuv[2] = 0.0f;
1235 if (shi->totuv == 0) {
1236 ShadeInputUV *suv = &shi->uv[0];
1238 suv->uv[0] = 2.0f * (u + .5f);
1239 suv->uv[1] = 2.0f * (v + .5f);
1240 suv->uv[2] = 0.0f; /* texture.c assumes there are 3 coords */
1242 if (mode & MA_FACETEXTURE) {
1243 /* no tface? set at 1.0f */
1244 shi->vcol[0] = 1.0f;
1245 shi->vcol[1] = 1.0f;
1246 shi->vcol[2] = 1.0f;
1247 shi->vcol[3] = 1.0f;
1252 if (texco & TEXCO_NORM) {
1253 shi->orn[0] = -shi->vn[0];
1254 shi->orn[1] = -shi->vn[1];
1255 shi->orn[2] = -shi->vn[2];
1258 if (texco & TEXCO_STRESS) {
1259 float *s1, *s2, *s3;
1261 s1 = RE_vertren_get_stress(obr, v1, 0);
1262 s2 = RE_vertren_get_stress(obr, v2, 0);
1263 s3 = RE_vertren_get_stress(obr, v3, 0);
1264 if (s1 && s2 && s3) {
1265 shi->stress = l * s3[0] - u * s1[0] - v * s2[0];
1266 if (shi->stress < 1.0f) shi->stress -= 1.0f;
1267 else shi->stress = (shi->stress - 1.0f) / shi->stress;
1269 else shi->stress = 0.0f;
1272 if (texco & TEXCO_TANGENT) {
1273 if ((mode & MA_TANGENT_V) == 0) {
1274 /* just prevent surprises */
1275 shi->tang[0] = shi->tang[1] = shi->tang[2] = 0.0f;
1276 shi->nmaptang[0] = shi->nmaptang[1] = shi->nmaptang[2] = 0.0f;
1281 /* this only avalailable for scanline renders */
1282 if (shi->depth == 0) {
1286 if (texco & TEXCO_WINDOW) {
1287 shi->winco[0] = -1.0f + 2.0f * x / (float)R.winx;
1288 shi->winco[1] = -1.0f + 2.0f * y / (float)R.winy;
1289 shi->winco[2] = 0.0f;
1291 shi->dxwin[0] = 2.0f / (float)R.winx;
1292 shi->dywin[1] = 2.0f / (float)R.winy;
1293 shi->dxwin[1] = shi->dxwin[2] = 0.0f;
1294 shi->dywin[0] = shi->dywin[2] = 0.0f;
1299 * Note! For raytracing winco is not set,
1300 * important because thus means all shader input's need to have their variables set to zero
1301 * else un-initialized values are used
1303 if (shi->do_manage) {
1304 if ((mode & (MA_VERTEXCOL | MA_VERTEXCOLP | MA_FACETEXTURE)) || (R.flag & R_NEED_VCOL)) {
1305 srgb_to_linearrgb_v3_v3(shi->vcol, shi->vcol);
1311 /* ****************** ShadeSample ************************************** */
1313 /* initialize per part, not per pixel! */
1314 void shade_input_initialize(ShadeInput *shi, RenderPart *pa, RenderLayer *rl, int sample)
1317 memset(shi, 0, sizeof(ShadeInput));
1319 shi->sample = sample;
1320 shi->thread = pa->thread;
1321 shi->do_preview = (R.r.scemode & R_MATNODE_PREVIEW) != 0;
1323 shi->do_manage = BKE_scene_check_color_management_enabled(R.scene);
1326 shi->layflag = rl->layflag;
1327 shi->passflag = rl->passflag;
1328 shi->combinedflag = ~rl->pass_xor;
1329 shi->mat_override = rl->mat_override;
1330 shi->light_override = rl->light_override;
1332 /* note shi.depth==0 means first hit, not raytracing */
1336 /* initialize per part, not per pixel! */
1337 void shade_sample_initialize(ShadeSample *ssamp, RenderPart *pa, RenderLayer *rl)
1341 tot = R.osa == 0 ? 1 : R.osa;
1343 for (a = 0; a < tot; a++) {
1344 shade_input_initialize(&ssamp->shi[a], pa, rl, a);
1345 memset(&ssamp->shr[a], 0, sizeof(ShadeResult));
1348 get_sample_layers(pa, rl, ssamp->rlpp);
1351 /* Do AO or (future) GI */
1352 void shade_samples_do_AO(ShadeSample *ssamp)
1354 if (!(R.r.mode & R_SHADOW))
1356 if (!(R.r.mode & R_RAYTRACE) && !(R.wrld.ao_gather_method == WO_AOGATHER_APPROX))
1359 if (R.wrld.mode & (WO_AMB_OCC | WO_ENV_LIGHT | WO_INDIRECT_LIGHT)) {
1360 ShadeInput *shi = &ssamp->shi[0];
1363 if (((shi->passflag & SCE_PASS_COMBINED) && (shi->combinedflag & (SCE_PASS_AO | SCE_PASS_ENVIRONMENT | SCE_PASS_INDIRECT))) ||
1364 (shi->passflag & (SCE_PASS_AO | SCE_PASS_ENVIRONMENT | SCE_PASS_INDIRECT)))
1366 for (sample = 0; sample < ssamp->tot; shi++, sample++)
1367 if (!(shi->mode & MA_SHLESS))
1368 ambient_occlusion(shi); /* stores in shi->ao[] */
1374 void shade_samples_fill_with_ps(ShadeSample *ssamp, PixStr *ps, int x, int y)
1381 for (shi = ssamp->shi; ps; ps = ps->next) {
1382 shade_input_set_triangle(shi, ps->obi, ps->facenr, 1);
1384 if (shi->vlr) { /* NULL happens for env material or for 'all z' */
1385 unsigned short curmask = ps->mask;
1387 /* full osa is only set for OSA renders */
1388 if (shi->vlr->flag & R_FULL_OSA) {
1389 short shi_cp = 0, samp;
1391 for (samp = 0; samp < R.osa; samp++) {
1392 if (curmask & (1 << samp)) {
1393 /* zbuffer has this inverse corrected, ensures xs,ys are inside pixel */
1394 xs = (float)x + R.jit[samp][0] + 0.5f;
1395 ys = (float)y + R.jit[samp][1] + 0.5f;
1398 shade_input_copy_triangle(shi, shi - 1);
1400 shi->mask = (1 << samp);
1401 // shi->rl= ssamp->rlpp[samp];
1402 shi->samplenr = R.shadowsamplenr[shi->thread]++; /* this counter is not being reset per pixel */
1403 shade_input_set_viewco(shi, x, y, xs, ys, (float)ps->z);
1404 shade_input_set_uv(shi);
1406 shade_input_set_normals(shi);
1407 else /* XXX shi->flippednor messes up otherwise */
1408 shade_input_set_vertex_normals(shi);
1417 short b = R.samples->centmask[curmask];
1418 xs = (float)x + R.samples->centLut[b & 15] + 0.5f;
1419 ys = (float)y + R.samples->centLut[b >> 4] + 0.5f;
1421 else if (R.i.curblur) {
1422 xs= (float)x + R.mblur_jit[R.i.curblur-1][0] + 0.5f;
1423 ys= (float)y + R.mblur_jit[R.i.curblur-1][1] + 0.5f;
1426 xs = (float)x + 0.5f;
1427 ys = (float)y + 0.5f;
1430 shi->mask = curmask;
1431 shi->samplenr = R.shadowsamplenr[shi->thread]++;
1432 shade_input_set_viewco(shi, x, y, xs, ys, (float)ps->z);
1433 shade_input_set_uv(shi);
1434 shade_input_set_normals(shi);
1438 /* total sample amount, shi->sample is static set in initialize */
1439 if (shi != ssamp->shi)
1440 ssamp->tot = (shi - 1)->sample + 1;
1445 /* shades samples, returns true if anything happened */
1446 int shade_samples(ShadeSample *ssamp, PixStr *ps, int x, int y)
1448 shade_samples_fill_with_ps(ssamp, ps, x, y);
1451 ShadeInput *shi = ssamp->shi;
1452 ShadeResult *shr = ssamp->shr;
1455 /* if shadow or AO? */
1456 shade_samples_do_AO(ssamp);
1458 /* if shade (all shadepinputs have same passflag) */
1459 if (ssamp->shi[0].passflag & ~(SCE_PASS_Z | SCE_PASS_INDEXOB | SCE_PASS_INDEXMA)) {
1461 for (samp = 0; samp < ssamp->tot; samp++, shi++, shr++) {
1462 shade_input_set_shade_texco(shi);
1463 shade_input_do_shade(shi, shr);
1466 else if (shi->passflag & SCE_PASS_Z) {
1467 for (samp = 0; samp < ssamp->tot; samp++, shi++, shr++)
1468 shr->z = -shi->co[2];