swr: rework vert <-> frag shader linkage logic
authorIlia Mirkin <imirkin@alum.mit.edu>
Thu, 10 Nov 2016 02:04:01 +0000 (21:04 -0500)
committerIlia Mirkin <imirkin@alum.mit.edu>
Wed, 23 Nov 2016 01:27:20 +0000 (20:27 -0500)
Fixes a few things:
 - sprite coords only apply to generic varyings, and are a bitmask
 - back color only applies in 2-sided lighting mode
 - handle some odd situations between only some front/back colors being
   there. This is only semi-legal in GL, but we shouldn't start
   crashing.

Signed-off-by: Ilia Mirkin <imirkin@alum.mit.edu>
Reviewed-by: Tim Rowley <timothy.o.rowley@intel.com>
src/gallium/drivers/swr/swr_shader.cpp

index 2f7223900d3fd28723146e015c3c7e68a7336136..d29f635144348503f4829ba48afc9e8440700a15 100644 (file)
@@ -372,15 +372,6 @@ locate_linkage(ubyte name, ubyte index, struct tgsi_shader_info *info)
       }
    }
 
-   if (name == TGSI_SEMANTIC_COLOR) { // BCOLOR fallback
-      for (int i = 0; i < PIPE_MAX_SHADER_OUTPUTS; i++) {
-         if ((info->output_semantic_name[i] == TGSI_SEMANTIC_BCOLOR)
-             && (info->output_semantic_index[i] == index)) {
-            return i - 1; // position is not part of the linkage
-         }
-      }
-   }
-
    return 0xFFFFFFFF;
 }
 
@@ -523,54 +514,70 @@ BuilderSWR::CompileFS(struct swr_context *ctx, swr_jit_fs_key &key)
 
       unsigned linkedAttrib =
          locate_linkage(semantic_name, semantic_idx, &ctx->vs->info.base);
-      if (linkedAttrib == 0xFFFFFFFF) {
-         // not found - check for point sprite
-         if (ctx->rasterizer->sprite_coord_enable) {
-            linkedAttrib = ctx->vs->info.base.num_outputs - 1;
-            swr_fs->pointSpriteMask |= (1 << linkedAttrib);
-         } else {
-            fprintf(stderr,
-                    "Missing %s[%d]\n",
-                    tgsi_semantic_names[semantic_name],
-                    semantic_idx);
-            assert(0 && "attribute linkage not found");
+      if (semantic_name == TGSI_SEMANTIC_GENERIC &&
+          ctx->rasterizer->sprite_coord_enable & (1 << semantic_idx)) {
+         /* we add an extra attrib to the backendState in swr_update_derived. */
+         linkedAttrib = ctx->vs->info.base.num_outputs - 1;
+         swr_fs->pointSpriteMask |= (1 << linkedAttrib);
+      } else if (linkedAttrib == 0xFFFFFFFF) {
+         inputs[attrib][0] = wrap(VIMMED1(0.0f));
+         inputs[attrib][1] = wrap(VIMMED1(0.0f));
+         inputs[attrib][2] = wrap(VIMMED1(0.0f));
+         inputs[attrib][3] = wrap(VIMMED1(1.0f));
+         /* If we're reading in color and 2-sided lighting is enabled, we have
+          * to keep going.
+          */
+         if (semantic_name != TGSI_SEMANTIC_COLOR || !key.light_twoside)
+            continue;
+      } else {
+         if (interpMode == TGSI_INTERPOLATE_CONSTANT) {
+            swr_fs->constantMask |= 1 << linkedAttrib;
+         } else if (interpMode == TGSI_INTERPOLATE_COLOR) {
+            swr_fs->flatConstantMask |= 1 << linkedAttrib;
          }
       }
 
-      if (interpMode == TGSI_INTERPOLATE_CONSTANT) {
-         swr_fs->constantMask |= 1 << linkedAttrib;
-      } else if (interpMode == TGSI_INTERPOLATE_COLOR) {
-         swr_fs->flatConstantMask |= 1 << linkedAttrib;
-      }
-
-      for (int channel = 0; channel < TGSI_NUM_CHANNELS; channel++) {
-         if (mask & (1 << channel)) {
-            Value *indexA = C(linkedAttrib * 12 + channel);
-            Value *indexB = C(linkedAttrib * 12 + channel + 4);
-            Value *indexC = C(linkedAttrib * 12 + channel + 8);
+      unsigned bcolorAttrib = 0xFFFFFFFF;
+      Value *offset = NULL;
+      if (semantic_name == TGSI_SEMANTIC_COLOR && key.light_twoside) {
+         bcolorAttrib = locate_linkage(
+               TGSI_SEMANTIC_BCOLOR, semantic_idx, &ctx->vs->info.base);
+         /* Neither front nor back colors were available. Nothing to load. */
+         if (bcolorAttrib == 0xFFFFFFFF && linkedAttrib == 0xFFFFFFFF)
+            continue;
+         /* If there is no front color, just always use the back color. */
+         if (linkedAttrib == 0xFFFFFFFF)
+            linkedAttrib = bcolorAttrib;
 
-            if ((semantic_name == TGSI_SEMANTIC_COLOR)
-                && ctx->rasterizer->light_twoside) {
-               unsigned bcolorAttrib = locate_linkage(
-                  TGSI_SEMANTIC_BCOLOR, semantic_idx, &ctx->vs->info.base);
+         if (bcolorAttrib != 0xFFFFFFFF) {
+            if (interpMode == TGSI_INTERPOLATE_CONSTANT) {
+               swr_fs->constantMask |= 1 << bcolorAttrib;
+            } else if (interpMode == TGSI_INTERPOLATE_COLOR) {
+               swr_fs->flatConstantMask |= 1 << bcolorAttrib;
+            }
 
-               unsigned diff = 12 * (bcolorAttrib - linkedAttrib);
+            unsigned diff = 12 * (bcolorAttrib - linkedAttrib);
 
+            if (diff) {
                Value *back =
                   XOR(C(1), LOAD(pPS, {0, SWR_PS_CONTEXT_frontFace}), "backFace");
 
-               Value *offset = MUL(back, C(diff));
+               offset = MUL(back, C(diff));
                offset->setName("offset");
+            }
+         }
+      }
 
+      for (int channel = 0; channel < TGSI_NUM_CHANNELS; channel++) {
+         if (mask & (1 << channel)) {
+            Value *indexA = C(linkedAttrib * 12 + channel);
+            Value *indexB = C(linkedAttrib * 12 + channel + 4);
+            Value *indexC = C(linkedAttrib * 12 + channel + 8);
+
+            if (offset) {
                indexA = ADD(indexA, offset);
                indexB = ADD(indexB, offset);
                indexC = ADD(indexC, offset);
-
-               if (interpMode == TGSI_INTERPOLATE_CONSTANT) {
-                  swr_fs->constantMask |= 1 << bcolorAttrib;
-               } else if (interpMode == TGSI_INTERPOLATE_COLOR) {
-                  swr_fs->flatConstantMask |= 1 << bcolorAttrib;
-               }
             }
 
             Value *va = VBROADCAST(LOAD(GEP(pAttribs, indexA)));