r300g: remove always_dirty flag and correct leftovers
[mesa.git] / src / gallium / drivers / r300 / r300_vs.c
index bd6b95dccbae0acdeccad5823edc140587801b4b..8137972f0188a989207fd9a029d809156732d731 100644 (file)
@@ -31,6 +31,7 @@
 
 #include "tgsi/tgsi_dump.h"
 #include "tgsi/tgsi_parse.h"
+#include "tgsi/tgsi_ureg.h"
 
 #include "radeon_compiler.h"
 
@@ -80,11 +81,12 @@ static void r300_shader_read_vs_outputs(
 
             case TGSI_SEMANTIC_EDGEFLAG:
                 assert(index == 0);
-                fprintf(stderr, "r300 VP: cannot handle edgeflag output\n");
-                assert(0);
+                fprintf(stderr, "r300 VP: cannot handle edgeflag output.\n");
                 break;
+
             default:
-                assert(0);
+                fprintf(stderr, "r300 VP: unknown vertex output semantic: %i.\n",
+                        info->output_semantic_name[i]);
         }
     }
 
@@ -148,35 +150,32 @@ static void r300_init_vs_output_mapping(struct r300_vertex_shader* vs)
 
     /* Texture coordinates. */
     gen_count = 0;
-    for (i = 0; i < ATTR_GENERIC_COUNT; i++) {
+    for (i = 0; i < ATTR_GENERIC_COUNT && gen_count < 8; i++) {
         if (vs_outputs->generic[i] != ATTR_UNUSED) {
             vap_out->vap_vsm_vtx_assm |= (R300_INPUT_CNTL_TC0 << gen_count);
             vap_out->vap_out_vtx_fmt[1] |= (4 << (3 * gen_count));
 
-            assert(tabi < 16);
             stream_loc[tabi++] = 6 + gen_count;
             gen_count++;
         }
     }
 
     /* Fog coordinates. */
-    if (vs_outputs->fog != ATTR_UNUSED) {
+    if (gen_count < 8 && vs_outputs->fog != ATTR_UNUSED) {
         vap_out->vap_vsm_vtx_assm |= (R300_INPUT_CNTL_TC0 << gen_count);
         vap_out->vap_out_vtx_fmt[1] |= (4 << (3 * gen_count));
 
-        assert(tabi < 16);
         stream_loc[tabi++] = 6 + gen_count;
         gen_count++;
     }
 
-    /* XXX magic */
-    assert(gen_count <= 8);
-
     /* WPOS. */
-    vs->wpos_tex_output = gen_count;
-
-    assert(tabi < 16);
-    stream_loc[tabi++] = 6 + gen_count;
+    if (gen_count < 8) {
+        vs->wpos_tex_output = gen_count;
+        stream_loc[tabi++] = 6 + gen_count;
+    } else {
+        vs_outputs->wpos = ATTR_UNUSED;
+    }
 
     for (; tabi < 16;) {
         stream_loc[tabi++] = -1;
@@ -252,24 +251,43 @@ static void set_vertex_inputs_outputs(struct r300_vertex_program_compiler * c)
     }
 }
 
-void r300_vertex_shader_common_init(struct r300_vertex_shader *vs,
-                                    const struct pipe_shader_state *shader)
+static void r300_dummy_vertex_shader(
+    struct r300_context* r300,
+    struct r300_vertex_shader* shader)
 {
-    /* Copy state directly into shader. */
-    vs->state = *shader;
-    vs->state.tokens = tgsi_dup_tokens(shader->tokens);
-    tgsi_scan_shader(shader->tokens, &vs->info);
+    struct pipe_shader_state state;
+    struct ureg_program *ureg;
+    struct ureg_dst dst;
+    struct ureg_src imm;
 
-    r300_shader_read_vs_outputs(&vs->info, &vs->outputs);
-    r300_init_vs_output_mapping(vs);
+    /* Make a simple vertex shader which outputs (0, 0, 0, 1),
+     * effectively rendering nothing. */
+    ureg = ureg_create(TGSI_PROCESSOR_VERTEX);
+    dst = ureg_DECL_output(ureg, TGSI_SEMANTIC_POSITION, 0);
+    imm = ureg_imm4f(ureg, 0, 0, 0, 1);
+
+    ureg_MOV(ureg, dst, imm);
+    ureg_END(ureg);
+
+    state.tokens = ureg_finalize(ureg);
+
+    shader->dummy = TRUE;
+    r300_translate_vertex_shader(r300, shader, state.tokens);
+
+    ureg_destroy(ureg);
 }
 
 void r300_translate_vertex_shader(struct r300_context* r300,
-                                  struct r300_vertex_shader* vs)
+                                  struct r300_vertex_shader* vs,
+                                  const struct tgsi_token *tokens)
 {
     struct r300_vertex_program_compiler compiler;
     struct tgsi_to_rc ttr;
 
+    tgsi_scan_shader(tokens, &vs->info);
+    r300_shader_read_vs_outputs(&vs->info, &vs->outputs);
+    r300_init_vs_output_mapping(vs);
+
     /* Setup the compiler */
     rc_init(&compiler.Base);
 
@@ -279,7 +297,7 @@ void r300_translate_vertex_shader(struct r300_context* r300,
 
     if (compiler.Base.Debug) {
         debug_printf("r300: Initial vertex program\n");
-        tgsi_dump(vs->state.tokens, 0);
+        tgsi_dump(tokens, 0);
     }
 
     /* Translate TGSI to our internal representation */
@@ -287,22 +305,38 @@ void r300_translate_vertex_shader(struct r300_context* r300,
     ttr.info = &vs->info;
     ttr.use_half_swizzles = FALSE;
 
-    r300_tgsi_to_rc(&ttr, vs->state.tokens);
+    r300_tgsi_to_rc(&ttr, tokens);
+
+    compiler.RequiredOutputs =
+        ~(~0 << (vs->info.num_outputs +
+                 (vs->outputs.wpos != ATTR_UNUSED ? 1 : 0)));
 
-    compiler.RequiredOutputs = ~(~0 << (vs->info.num_outputs+1));
     compiler.SetHwInputOutput = &set_vertex_inputs_outputs;
 
     /* Insert the WPOS output. */
-    rc_copy_output(&compiler.Base, 0, vs->outputs.wpos);
+    if (vs->outputs.wpos != ATTR_UNUSED) {
+        rc_copy_output(&compiler.Base, 0, vs->outputs.wpos);
+    }
 
     /* Invoke the compiler */
     r3xx_compile_vertex_program(&compiler);
     if (compiler.Base.Error) {
         /* XXX We should fallback using Draw. */
-        fprintf(stderr, "r300 VP: Compiler error\n");
-        abort();
+        fprintf(stderr, "r300 VP: Compiler error:\n%sUsing a dummy shader"
+                " instead.\n", compiler.Base.ErrorMsg);
+
+        if (vs->dummy) {
+            fprintf(stderr, "r300 VP: Cannot compile the dummy shader! "
+                    "Giving up...\n");
+            abort();
+        }
+        r300_dummy_vertex_shader(r300, vs);
     }
 
+    /* Initialize numbers of constants for each type. */
+    vs->externals_count = ttr.immediate_offset;
+    vs->immediates_count = vs->code.constants.Count - vs->externals_count;
+
     /* And, finally... */
     rc_destroy(&compiler.Base);
 }
@@ -314,13 +348,15 @@ boolean r300_vertex_shader_setup_wpos(struct r300_context* r300)
     int tex_output = vs->wpos_tex_output;
     uint32_t tex_fmt = R300_INPUT_CNTL_TC0 << tex_output;
 
-    if (r300->fs->inputs.wpos != ATTR_UNUSED) {
+    if (vs->outputs.wpos == ATTR_UNUSED) {
+        return FALSE;
+    }
+
+    if (r300_fs(r300)->shader->inputs.wpos != ATTR_UNUSED) {
         /* Enable WPOS in VAP. */
         if (!(vap_out->vap_vsm_vtx_assm & tex_fmt)) {
             vap_out->vap_vsm_vtx_assm |= tex_fmt;
             vap_out->vap_out_vtx_fmt[1] |= (4 << (3 * tex_output));
-
-            assert(tex_output < 8);
             return TRUE;
         }
     } else {