radeonsi: Set PIPE_SHADER_CAP_MAX_SHADER_IMAGES
[mesa.git] / src / gallium / drivers / radeonsi / si_debug.c
index cce665e85fae554b7bc9e249abb2fdc0bc13b1cc..eb0cabb9f2f461bf85627277fcc0ff7e872ee939 100644 (file)
 #include "si_shader.h"
 #include "sid.h"
 #include "sid_tables.h"
+#include "radeon/radeon_elf_util.h"
 #include "ddebug/dd_util.h"
+#include "util/u_memory.h"
 
+DEBUG_GET_ONCE_OPTION(replace_shaders, "RADEON_REPLACE_SHADERS", NULL)
 
-static void si_dump_shader(struct si_shader_ctx_state *state, const char *name,
-                          FILE *f)
+static void si_dump_shader(struct si_screen *sscreen,
+                          struct si_shader_ctx_state *state, FILE *f)
 {
        if (!state->cso || !state->current)
                return;
 
-       fprintf(f, "%s shader disassembly:\n", name);
        si_dump_shader_key(state->cso->type, &state->current->key, f);
-       fprintf(f, "%s\n\n", state->current->binary.disasm_string);
+       si_shader_dump(sscreen, state->current, NULL,
+                      state->cso->info.processor, f);
+}
+
+/**
+ * Shader compiles can be overridden with arbitrary ELF objects by setting
+ * the environment variable RADEON_REPLACE_SHADERS=num1:filename1[;num2:filename2]
+ */
+bool si_replace_shader(unsigned num, struct radeon_shader_binary *binary)
+{
+       const char *p = debug_get_option_replace_shaders();
+       const char *semicolon;
+       char *copy = NULL;
+       FILE *f;
+       long filesize, nread;
+       char *buf = NULL;
+       bool replaced = false;
+
+       if (!p)
+               return false;
+
+       while (*p) {
+               unsigned long i;
+               char *endp;
+               i = strtoul(p, &endp, 0);
+
+               p = endp;
+               if (*p != ':') {
+                       fprintf(stderr, "RADEON_REPLACE_SHADERS formatted badly.\n");
+                       exit(1);
+               }
+               ++p;
+
+               if (i == num)
+                       break;
+
+               p = strchr(p, ';');
+               if (!p)
+                       return false;
+               ++p;
+       }
+       if (!*p)
+               return false;
+
+       semicolon = strchr(p, ';');
+       if (semicolon) {
+               p = copy = strndup(p, semicolon - p);
+               if (!copy) {
+                       fprintf(stderr, "out of memory\n");
+                       return false;
+               }
+       }
+
+       fprintf(stderr, "radeonsi: replace shader %u by %s\n", num, p);
+
+       f = fopen(p, "r");
+       if (!f) {
+               perror("radeonsi: failed to open file");
+               goto out_free;
+       }
+
+       if (fseek(f, 0, SEEK_END) != 0)
+               goto file_error;
+
+       filesize = ftell(f);
+       if (filesize < 0)
+               goto file_error;
+
+       if (fseek(f, 0, SEEK_SET) != 0)
+               goto file_error;
+
+       buf = MALLOC(filesize);
+       if (!buf) {
+               fprintf(stderr, "out of memory\n");
+               goto out_close;
+       }
+
+       nread = fread(buf, 1, filesize, f);
+       if (nread != filesize)
+               goto file_error;
+
+       radeon_elf_read(buf, filesize, binary);
+       replaced = true;
+
+out_close:
+       fclose(f);
+out_free:
+       FREE(buf);
+       free(copy);
+       return replaced;
+
+file_error:
+       perror("radeonsi: reading shader");
+       goto out_close;
 }
 
 /* Parsed IBs are difficult to read without colors. Use "less -R file" to
@@ -61,13 +156,16 @@ static void print_spaces(FILE *f, unsigned num)
 static void print_value(FILE *file, uint32_t value, int bits)
 {
        /* Guess if it's int or float */
-       if (value <= (1 << 15))
-               fprintf(file, "%u\n", value);
-       else {
+       if (value <= (1 << 15)) {
+               if (value <= 9)
+                       fprintf(file, "%u\n", value);
+               else
+                       fprintf(file, "%u (0x%0*x)\n", value, bits / 4, value);
+       } else {
                float f = uif(value);
 
                if (fabs(f) < 100000 && f*10 == floor(f*10))
-                       fprintf(file, "%.1ff\n", f);
+                       fprintf(file, "%.1ff (0x%0*x)\n", f, bits / 4, value);
                else
                        /* Don't print more leading zeros than there are bits. */
                        fprintf(file, "0x%0*x\n", bits / 4, value);
@@ -407,7 +505,7 @@ static void si_dump_last_ib(struct si_context *sctx, FILE *f)
                 * waited for the context, so this buffer should be idle.
                 * If the GPU is hung, there is no point in waiting for it.
                 */
-               uint32_t *map = sctx->b.ws->buffer_map(sctx->last_trace_buf->cs_buf,
+               uint32_t *map = sctx->b.ws->buffer_map(sctx->last_trace_buf->buf,
                                                       NULL,
                                                       PIPE_TRANSFER_UNSYNCHRONIZED |
                                                       PIPE_TRANSFER_READ);
@@ -572,11 +670,11 @@ static void si_dump_debug_state(struct pipe_context *ctx, FILE *f,
                si_dump_debug_registers(sctx, f);
 
        si_dump_framebuffer(sctx, f);
-       si_dump_shader(&sctx->vs_shader, "Vertex", f);
-       si_dump_shader(&sctx->tcs_shader, "Tessellation control", f);
-       si_dump_shader(&sctx->tes_shader, "Tessellation evaluation", f);
-       si_dump_shader(&sctx->gs_shader, "Geometry", f);
-       si_dump_shader(&sctx->ps_shader, "Fragment", f);
+       si_dump_shader(sctx->screen, &sctx->vs_shader, f);
+       si_dump_shader(sctx->screen, &sctx->tcs_shader, f);
+       si_dump_shader(sctx->screen, &sctx->tes_shader, f);
+       si_dump_shader(sctx->screen, &sctx->gs_shader, f);
+       si_dump_shader(sctx->screen, &sctx->ps_shader, f);
 
        si_dump_last_bo_list(sctx, f);
        si_dump_last_ib(sctx, f);
@@ -673,7 +771,7 @@ void si_check_vm_faults(struct si_context *sctx)
        if (!si_vm_fault_occured(sctx, &addr))
                return;
 
-       f = dd_get_debug_file();
+       f = dd_get_debug_file(false);
        if (!f)
                return;
 
@@ -683,8 +781,7 @@ void si_check_vm_faults(struct si_context *sctx)
        fprintf(f, "Device name: %s\n\n", screen->get_name(screen));
        fprintf(f, "Failing VM page: 0x%08x\n\n", addr);
 
-       si_dump_last_bo_list(sctx, f);
-       si_dump_last_ib(sctx, f);
+       si_dump_debug_state(&sctx->b.b, f, 0);
        fclose(f);
 
        fprintf(stderr, "Detected a VM fault, exiting...\n");