1 /* 2 * Copyright (c) 2014 - 2015 Intel Corporation 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 21 * DEALINGS IN THE SOFTWARE. 22 */ 23 24 #include "util/ralloc.h" 25 #include "brw_context.h" 26 #include "brw_cs.h" 27 #include "brw_wm.h" 28 #include "intel_mipmap_tree.h" 29 #include "brw_state.h" 30 #include "intel_batchbuffer.h" 31 #include "compiler/brw_nir.h" 32 #include "brw_program.h" 33 #include "compiler/glsl/ir_uniform.h" 34 35 static void 36 assign_cs_binding_table_offsets(const struct gen_device_info *devinfo, 37 const struct gl_program *prog, 38 struct brw_cs_prog_data *prog_data) 39 { 40 uint32_t next_binding_table_offset = 0; 41 42 /* May not be used if the gl_NumWorkGroups variable is not accessed. */ 43 prog_data->binding_table.work_groups_start = next_binding_table_offset; 44 next_binding_table_offset++; 45 46 brw_assign_common_binding_table_offsets(devinfo, prog, &prog_data->base, 47 next_binding_table_offset); 48 } 49 50 static bool 51 brw_codegen_cs_prog(struct brw_context *brw, 52 struct brw_program *cp, 53 struct brw_cs_prog_key *key) 54 { 55 const struct gen_device_info *devinfo = &brw->screen->devinfo; 56 const GLuint *program; 57 void *mem_ctx = ralloc_context(NULL); 58 struct brw_cs_prog_data prog_data; 59 bool start_busy = false; 60 double start_time = 0; 61 62 memset(&prog_data, 0, sizeof(prog_data)); 63 64 if (cp->program.info.cs.shared_size > 64 * 1024) { 65 cp->program.sh.data->LinkStatus = linking_failure; 66 const char *error_str = 67 "Compute shader used more than 64KB of shared variables"; 68 ralloc_strcat(&cp->program.sh.data->InfoLog, error_str); 69 _mesa_problem(NULL, "Failed to link compute shader: %s\n", error_str); 70 71 ralloc_free(mem_ctx); 72 return false; 73 } else { 74 prog_data.base.total_shared = cp->program.info.cs.shared_size; 75 } 76 77 assign_cs_binding_table_offsets(devinfo, &cp->program, &prog_data); 78 79 brw_nir_setup_glsl_uniforms(mem_ctx, cp->program.nir, 80 &cp->program, &prog_data.base, true); 81 82 if (unlikely(brw->perf_debug)) { 83 start_busy = (brw->batch.last_bo && 84 brw_bo_busy(brw->batch.last_bo)); 85 start_time = get_time(); 86 } 87 88 int st_index = -1; 89 if (INTEL_DEBUG & DEBUG_SHADER_TIME) 90 st_index = brw_get_shader_time_index(brw, &cp->program, ST_CS, true); 91 92 char *error_str; 93 program = brw_compile_cs(brw->screen->compiler, brw, mem_ctx, key, 94 &prog_data, cp->program.nir, st_index, 95 &error_str); 96 if (program == NULL) { 97 cp->program.sh.data->LinkStatus = linking_failure; 98 ralloc_strcat(&cp->program.sh.data->InfoLog, error_str); 99 _mesa_problem(NULL, "Failed to compile compute shader: %s\n", error_str); 100 101 ralloc_free(mem_ctx); 102 return false; 103 } 104 105 if (unlikely(brw->perf_debug)) { 106 if (cp->compiled_once) { 107 _mesa_problem(&brw->ctx, "CS programs shouldn't need recompiles"); 108 } 109 cp->compiled_once = true; 110 111 if (start_busy && !brw_bo_busy(brw->batch.last_bo)) { 112 perf_debug("CS compile took %.03f ms and stalled the GPU\n", 113 (get_time() - start_time) * 1000); 114 } 115 } 116 117 brw_alloc_stage_scratch(brw, &brw->cs.base, prog_data.base.total_scratch); 118 119 /* The param and pull_param arrays will be freed by the shader cache. */ 120 ralloc_steal(NULL, prog_data.base.param); 121 ralloc_steal(NULL, prog_data.base.pull_param); 122 brw_upload_cache(&brw->cache, BRW_CACHE_CS_PROG, 123 key, sizeof(*key), 124 program, prog_data.base.program_size, 125 &prog_data, sizeof(prog_data), 126 &brw->cs.base.prog_offset, &brw->cs.base.prog_data); 127 ralloc_free(mem_ctx); 128 129 return true; 130 } 131 132 133 void 134 brw_cs_populate_key(struct brw_context *brw, struct brw_cs_prog_key *key) 135 { 136 struct gl_context *ctx = &brw->ctx; 137 /* BRW_NEW_COMPUTE_PROGRAM */ 138 const struct brw_program *cp = 139 (struct brw_program *) brw->programs[MESA_SHADER_COMPUTE]; 140 const struct gl_program *prog = (struct gl_program *) cp; 141 142 memset(key, 0, sizeof(*key)); 143 144 /* _NEW_TEXTURE */ 145 brw_populate_sampler_prog_key_data(ctx, prog, &key->tex); 146 147 /* The unique compute program ID */ 148 key->program_string_id = cp->id; 149 } 150 151 152 void 153 brw_upload_cs_prog(struct brw_context *brw) 154 { 155 struct gl_context *ctx = &brw->ctx; 156 struct brw_cs_prog_key key; 157 struct brw_program *cp = 158 (struct brw_program *) brw->programs[MESA_SHADER_COMPUTE]; 159 160 if (!cp) 161 return; 162 163 if (!brw_state_dirty(brw, _NEW_TEXTURE, BRW_NEW_COMPUTE_PROGRAM)) 164 return; 165 166 brw->cs.base.sampler_count = 167 util_last_bit(ctx->ComputeProgram._Current->SamplersUsed); 168 169 brw_cs_populate_key(brw, &key); 170 171 if (brw_search_cache(&brw->cache, BRW_CACHE_CS_PROG, 172 &key, sizeof(key), 173 &brw->cs.base.prog_offset, 174 &brw->cs.base.prog_data)) 175 return; 176 177 if (brw_disk_cache_upload_program(brw, MESA_SHADER_COMPUTE)) 178 return; 179 180 cp = (struct brw_program *) brw->programs[MESA_SHADER_COMPUTE]; 181 cp->id = key.program_string_id; 182 183 MAYBE_UNUSED bool success = brw_codegen_cs_prog(brw, cp, &key); 184 assert(success); 185 } 186 187 188 bool 189 brw_cs_precompile(struct gl_context *ctx, struct gl_program *prog) 190 { 191 struct brw_context *brw = brw_context(ctx); 192 struct brw_cs_prog_key key; 193 194 struct brw_program *bcp = brw_program(prog); 195 196 memset(&key, 0, sizeof(key)); 197 key.program_string_id = bcp->id; 198 199 brw_setup_tex_for_precompile(brw, &key.tex, prog); 200 201 uint32_t old_prog_offset = brw->cs.base.prog_offset; 202 struct brw_stage_prog_data *old_prog_data = brw->cs.base.prog_data; 203 204 bool success = brw_codegen_cs_prog(brw, bcp, &key); 205 206 brw->cs.base.prog_offset = old_prog_offset; 207 brw->cs.base.prog_data = old_prog_data; 208 209 return success; 210 } 211