Mesa (master): r300g: use a dummy replacement fragment shader if the shader compilation fails

Marek Olšák mareko at kemper.freedesktop.org
Sun Apr 11 17:33:02 UTC 2010


Module: Mesa
Branch: master
Commit: 69019afa67d66cf3e5d2b4d5b286bf2ac1bd87af
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=69019afa67d66cf3e5d2b4d5b286bf2ac1bd87af

Author: Marek Olšák <maraeo at gmail.com>
Date:   Sun Apr 11 10:15:12 2010 +0200

r300g: use a dummy replacement fragment shader if the shader compilation fails

Better than killing an application.

---

 src/gallium/drivers/r300/r300_fs.c            |   86 ++++++++++++++++++------
 src/gallium/drivers/r300/r300_fs.h            |   16 ++++-
 src/gallium/drivers/r300/r300_state.c         |    5 +-
 src/gallium/drivers/r300/r300_state_derived.c |    4 +-
 src/gallium/drivers/r300/r300_vs.c            |    2 +-
 5 files changed, 80 insertions(+), 33 deletions(-)

diff --git a/src/gallium/drivers/r300/r300_fs.c b/src/gallium/drivers/r300/r300_fs.c
index d931952..c3f51ec 100644
--- a/src/gallium/drivers/r300/r300_fs.c
+++ b/src/gallium/drivers/r300/r300_fs.c
@@ -26,6 +26,7 @@
 #include "util/u_memory.h"
 
 #include "tgsi/tgsi_dump.h"
+#include "tgsi/tgsi_ureg.h"
 
 #include "r300_context.h"
 #include "r300_screen.h"
@@ -76,20 +77,20 @@ void r300_shader_read_fs_inputs(struct tgsi_shader_info* info,
 }
 
 static void find_output_registers(struct r300_fragment_program_compiler * compiler,
-                                  struct r300_fragment_shader * fs)
+                                  struct r300_fragment_shader_code *shader)
 {
     unsigned i, colorbuf_count = 0;
 
     /* Mark the outputs as not present initially */
-    compiler->OutputColor[0] = fs->info.num_outputs;
-    compiler->OutputColor[1] = fs->info.num_outputs;
-    compiler->OutputColor[2] = fs->info.num_outputs;
-    compiler->OutputColor[3] = fs->info.num_outputs;
-    compiler->OutputDepth = fs->info.num_outputs;
+    compiler->OutputColor[0] = shader->info.num_outputs;
+    compiler->OutputColor[1] = shader->info.num_outputs;
+    compiler->OutputColor[2] = shader->info.num_outputs;
+    compiler->OutputColor[3] = shader->info.num_outputs;
+    compiler->OutputDepth = shader->info.num_outputs;
 
     /* Now see where they really are. */
-    for(i = 0; i < fs->info.num_outputs; ++i) {
-        switch(fs->info.output_semantic_name[i]) {
+    for(i = 0; i < shader->info.num_outputs; ++i) {
+        switch(shader->info.output_semantic_name[i]) {
             case TGSI_SEMANTIC_COLOR:
                 compiler->OutputColor[colorbuf_count] = i;
                 colorbuf_count++;
@@ -155,12 +156,47 @@ static void get_compare_state(
 
 static void r300_translate_fragment_shader(
     struct r300_context* r300,
+    struct r300_fragment_shader_code* shader,
+    const struct tgsi_token *tokens);
+
+static void r300_dummy_fragment_shader(
+    struct r300_context* r300,
     struct r300_fragment_shader_code* shader)
 {
-    struct r300_fragment_shader* fs = r300->fs;
+    struct pipe_shader_state state;
+    struct ureg_program *ureg;
+    struct ureg_dst out;
+    struct ureg_src imm;
+
+    /* Make a simple fragment shader which outputs (0, 0, 0, 1) */
+    ureg = ureg_create(TGSI_PROCESSOR_FRAGMENT);
+    out = ureg_DECL_output(ureg, TGSI_SEMANTIC_COLOR, 0);
+    imm = ureg_imm4f(ureg, 0, 0, 0, 1);
+
+    ureg_MOV(ureg, out, imm);
+    ureg_END(ureg);
+
+    state.tokens = ureg_finalize(ureg);
+
+    shader->dummy = TRUE;
+    r300_translate_fragment_shader(r300, shader, state.tokens);
+
+    ureg_destroy(ureg);
+}
+
+static void r300_translate_fragment_shader(
+    struct r300_context* r300,
+    struct r300_fragment_shader_code* shader,
+    const struct tgsi_token *tokens)
+{
     struct r300_fragment_program_compiler compiler;
     struct tgsi_to_rc ttr;
-    int wpos = fs->inputs.wpos;
+    int wpos;
+
+    tgsi_scan_shader(tokens, &shader->info);
+    r300_shader_read_fs_inputs(&shader->info, &shader->inputs);
+
+    wpos = shader->inputs.wpos;
 
     /* Setup the compiler. */
     memset(&compiler, 0, sizeof(compiler));
@@ -172,23 +208,23 @@ static void r300_translate_fragment_shader(
     compiler.is_r500 = r300->screen->caps.is_r500;
     compiler.max_temp_regs = compiler.is_r500 ? 128 : 32;
     compiler.AllocateHwInputs = &allocate_hardware_inputs;
-    compiler.UserData = &fs->inputs;
+    compiler.UserData = &shader->inputs;
 
-    find_output_registers(&compiler, fs);
+    find_output_registers(&compiler, shader);
 
     if (compiler.Base.Debug) {
         debug_printf("r300: Initial fragment program\n");
-        tgsi_dump(fs->state.tokens, 0);
+        tgsi_dump(tokens, 0);
     }
 
     /* Translate TGSI to our internal representation */
     ttr.compiler = &compiler.Base;
-    ttr.info = &fs->info;
+    ttr.info = &shader->info;
     ttr.use_half_swizzles = TRUE;
 
-    r300_tgsi_to_rc(&ttr, fs->state.tokens);
+    r300_tgsi_to_rc(&ttr, tokens);
 
-    fs->shadow_samplers = compiler.Base.Program.ShadowSamplers;
+    shader->shadow_samplers = compiler.Base.Program.ShadowSamplers;
 
     /**
      * Transform the program to support WPOS.
@@ -205,10 +241,15 @@ static void r300_translate_fragment_shader(
     /* Invoke the compiler */
     r3xx_compile_fragment_program(&compiler);
     if (compiler.Base.Error) {
-        /* XXX failover maybe? */
-        fprintf(stderr, "r300 FP: Compiler Error:\n%s",
-                compiler.Base.ErrorMsg);
-        abort();
+        fprintf(stderr, "r300 FP: Compiler Error:\n%sUsing a dummy shader"
+                " instead.\n", compiler.Base.ErrorMsg);
+
+        if (shader->dummy) {
+            fprintf(stderr, "r300 FP: Cannot compile the dummy shader! "
+                    "Giving up...\n");
+            abort();
+        }
+        r300_dummy_fragment_shader(r300, shader);
     }
 
     /* And, finally... */
@@ -229,7 +270,8 @@ boolean r300_pick_fragment_shader(struct r300_context* r300)
          * therefore we set ~0, which means it should look at all sampler
          * states. This choice doesn't have any impact on the correctness. */
         get_compare_state(r300, &fs->shader->compare_state, ~0);
-        r300_translate_fragment_shader(r300, fs->shader);
+        r300_translate_fragment_shader(r300, fs->shader, fs->state.tokens);
+        fs->shadow_samplers = fs->shader->shadow_samplers;
         return TRUE;
 
     } else if (fs->shadow_samplers) {
@@ -254,7 +296,7 @@ boolean r300_pick_fragment_shader(struct r300_context* r300)
             fs->first = fs->shader = ptr;
 
             ptr->compare_state = state;
-            r300_translate_fragment_shader(r300, ptr);
+            r300_translate_fragment_shader(r300, ptr, fs->state.tokens);
             return TRUE;
         }
     }
diff --git a/src/gallium/drivers/r300/r300_fs.h b/src/gallium/drivers/r300/r300_fs.h
index 40ce874..0feba15 100644
--- a/src/gallium/drivers/r300/r300_fs.h
+++ b/src/gallium/drivers/r300/r300_fs.h
@@ -31,6 +31,16 @@
 #include "r300_shader_semantics.h"
 
 struct r300_fragment_shader_code {
+    struct tgsi_shader_info info;
+    struct r300_shader_semantics inputs;
+
+    /* Whether the shader was replaced by a dummy one due to a shader
+     * compilation failure. */
+    boolean dummy;
+
+    /* Bits 0-15: TRUE if it's a shadow sampler, FALSE otherwise. */
+    unsigned shadow_samplers;
+
     struct r300_fragment_program_external_state compare_state;
     struct rX00_fragment_program_code code;
 
@@ -41,10 +51,8 @@ struct r300_fragment_shader {
     /* Parent class */
     struct pipe_shader_state state;
 
-    struct tgsi_shader_info info;
-    struct r300_shader_semantics inputs;
-
-    /* Bits 0-15: TRUE if it's a shadow sampler, FALSE otherwise. */
+    /* Bits 0-15: TRUE if it's a shadow sampler, FALSE otherwise.
+     * Initialized from the first compiled FS. */
     unsigned shadow_samplers;
 
     /* Currently-bound fragment shader. */
diff --git a/src/gallium/drivers/r300/r300_state.c b/src/gallium/drivers/r300/r300_state.c
index 6e69155..7a5db5a 100644
--- a/src/gallium/drivers/r300/r300_state.c
+++ b/src/gallium/drivers/r300/r300_state.c
@@ -679,9 +679,6 @@ static void* r300_create_fs_state(struct pipe_context* pipe,
     fs->state = *shader;
     fs->state.tokens = tgsi_dup_tokens(shader->tokens);
 
-    tgsi_scan_shader(shader->tokens, &fs->info);
-    r300_shader_read_fs_inputs(&fs->info, &fs->inputs);
-
     return (void*)fs;
 }
 
@@ -1103,7 +1100,7 @@ static void r300_set_viewport_state(struct pipe_context* pipe,
     }
 
     r300->viewport_state.dirty = TRUE;
-    if (r300->fs && r300->fs->inputs.wpos != ATTR_UNUSED) {
+    if (r300->fs && r300->fs->shader->inputs.wpos != ATTR_UNUSED) {
         r300->dirty_state |= R300_NEW_FRAGMENT_SHADER_CONSTANTS;
     }
 }
diff --git a/src/gallium/drivers/r300/r300_state_derived.c b/src/gallium/drivers/r300/r300_state_derived.c
index 10e854d..1b4c245 100644
--- a/src/gallium/drivers/r300/r300_state_derived.c
+++ b/src/gallium/drivers/r300/r300_state_derived.c
@@ -376,7 +376,7 @@ static void r300_update_derived_shader_state(struct r300_context* r300)
 {
     struct r300_vertex_shader* vs = r300->vs_state.state;
 
-    r300_update_rs_block(r300, &vs->outputs, &r300->fs->inputs);
+    r300_update_rs_block(r300, &vs->outputs, &r300->fs->shader->inputs);
 }
 
 static boolean r300_dsa_writes_depth_stencil(struct r300_dsa_state* dsa)
@@ -437,7 +437,7 @@ static void r300_update_ztop(struct r300_context* r300)
     /* ZS writes */
     if (r300_dsa_writes_depth_stencil(r300->dsa_state.state) &&
            (r300_dsa_alpha_test_enabled(r300->dsa_state.state) ||/* (1) */
-            r300->fs->info.uses_kill)) {                         /* (2) */
+            r300->fs->shader->info.uses_kill)) {                 /* (2) */
         ztop_state->z_buffer_top = R300_ZTOP_DISABLE;
     } else if (r300_fragment_shader_writes_depth(r300->fs)) {    /* (5) */
         ztop_state->z_buffer_top = R300_ZTOP_DISABLE;
diff --git a/src/gallium/drivers/r300/r300_vs.c b/src/gallium/drivers/r300/r300_vs.c
index d8900f7..af5a4d7 100644
--- a/src/gallium/drivers/r300/r300_vs.c
+++ b/src/gallium/drivers/r300/r300_vs.c
@@ -318,7 +318,7 @@ boolean r300_vertex_shader_setup_wpos(struct r300_context* r300)
         return FALSE;
     }
 
-    if (r300->fs->inputs.wpos != ATTR_UNUSED) {
+    if (r300->fs->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;




More information about the mesa-commit mailing list