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_eu.h" 28 #include "brw_wm.h" 29 #include "brw_shader.h" 30 #include "intel_mipmap_tree.h" 31 #include "brw_state.h" 32 #include "intel_batchbuffer.h" 33 #include "brw_nir.h" 34 #include "brw_program.h" 35 #include "compiler/glsl/ir_uniform.h" 36 37 static void 38 assign_cs_binding_table_offsets(const struct gen_device_info *devinfo, 39 const struct gl_program *prog, 40 struct brw_cs_prog_data *prog_data) 41 { 42 uint32_t next_binding_table_offset = 0; 43 44 /* May not be used if the gl_NumWorkGroups variable is not accessed. */ 45 prog_data->binding_table.work_groups_start = next_binding_table_offset; 46 next_binding_table_offset++; 47 48 brw_assign_common_binding_table_offsets(devinfo, prog, &prog_data->base, 49 next_binding_table_offset); 50 } 51 52 static bool 53 brw_codegen_cs_prog(struct brw_context *brw, 54 struct brw_program *cp, 55 struct brw_cs_prog_key *key) 56 { 57 const struct gen_device_info *devinfo = &brw->screen->devinfo; 58 struct gl_context *ctx = &brw->ctx; 59 const GLuint *program; 60 void *mem_ctx = ralloc_context(NULL); 61 GLuint program_size; 62 struct brw_cs_prog_data prog_data; 63 bool start_busy = false; 64 double start_time = 0; 65 66 memset(&prog_data, 0, sizeof(prog_data)); 67 68 if (cp->program.info.cs.shared_size > 64 * 1024) { 69 cp->program.sh.data->LinkStatus = false; 70 const char *error_str = 71 "Compute shader used more than 64KB of shared variables"; 72 ralloc_strcat(&cp->program.sh.data->InfoLog, error_str); 73 _mesa_problem(NULL, "Failed to link compute shader: %s\n", error_str); 74 75 ralloc_free(mem_ctx); 76 return false; 77 } else { 78 prog_data.base.total_shared = cp->program.info.cs.shared_size; 79 } 80 81 assign_cs_binding_table_offsets(devinfo, &cp->program, &prog_data); 82 83 /* Allocate the references to the uniforms that will end up in the 84 * prog_data associated with the compiled program, and which will be freed 85 * by the state cache. 86 */ 87 int param_count = cp->program.nir->num_uniforms / 4; 88 89 /* The backend also sometimes add a param for the thread local id. */ 90 prog_data.thread_local_id_index = param_count++; 91 92 /* The backend also sometimes adds params for texture size. */ 93 param_count += 2 * ctx->Const.Program[MESA_SHADER_COMPUTE].MaxTextureImageUnits; 94 prog_data.base.param = 95 rzalloc_array(NULL, const gl_constant_value *, param_count); 96 prog_data.base.pull_param = 97 rzalloc_array(NULL, const gl_constant_value *, param_count); 98 prog_data.base.image_param = 99 rzalloc_array(NULL, struct brw_image_param, 100 cp->program.info.num_images); 101 prog_data.base.nr_params = param_count; 102 prog_data.base.nr_image_params = cp->program.info.num_images; 103 104 brw_nir_setup_glsl_uniforms(cp->program.nir, &cp->program,&prog_data.base, 105 true); 106 107 if (unlikely(brw->perf_debug)) { 108 start_busy = (brw->batch.last_bo && 109 drm_intel_bo_busy(brw->batch.last_bo)); 110 start_time = get_time(); 111 } 112 113 int st_index = -1; 114 if (INTEL_DEBUG & DEBUG_SHADER_TIME) 115 st_index = brw_get_shader_time_index(brw, &cp->program, ST_CS, true); 116 117 char *error_str; 118 program = brw_compile_cs(brw->screen->compiler, brw, mem_ctx, key, 119 &prog_data, cp->program.nir, st_index, 120 &program_size, &error_str); 121 if (program == NULL) { 122 cp->program.sh.data->LinkStatus = false; 123 ralloc_strcat(&cp->program.sh.data->InfoLog, error_str); 124 _mesa_problem(NULL, "Failed to compile compute shader: %s\n", error_str); 125 126 ralloc_free(mem_ctx); 127 return false; 128 } 129 130 if (unlikely(brw->perf_debug)) { 131 if (cp->compiled_once) { 132 _mesa_problem(&brw->ctx, "CS programs shouldn't need recompiles"); 133 } 134 cp->compiled_once = true; 135 136 if (start_busy && !drm_intel_bo_busy(brw->batch.last_bo)) { 137 perf_debug("CS compile took %.03f ms and stalled the GPU\n", 138 (get_time() - start_time) * 1000); 139 } 140 } 141 142 const unsigned subslices = MAX2(brw->screen->subslice_total, 1); 143 144 /* WaCSScratchSize:hsw 145 * 146 * Haswell's scratch space address calculation appears to be sparse 147 * rather than tightly packed. The Thread ID has bits indicating 148 * which subslice, EU within a subslice, and thread within an EU 149 * it is. There's a maximum of two slices and two subslices, so these 150 * can be stored with a single bit. Even though there are only 10 EUs 151 * per subslice, this is stored in 4 bits, so there's an effective 152 * maximum value of 16 EUs. Similarly, although there are only 7 153 * threads per EU, this is stored in a 3 bit number, giving an effective 154 * maximum value of 8 threads per EU. 155 * 156 * This means that we need to use 16 * 8 instead of 10 * 7 for the 157 * number of threads per subslice. 158 */ 159 const unsigned scratch_ids_per_subslice = 160 brw->is_haswell ? 16 * 8 : devinfo->max_cs_threads; 161 162 brw_alloc_stage_scratch(brw, &brw->cs.base, 163 prog_data.base.total_scratch, 164 scratch_ids_per_subslice * subslices); 165 166 brw_upload_cache(&brw->cache, BRW_CACHE_CS_PROG, 167 key, sizeof(*key), 168 program, program_size, 169 &prog_data, sizeof(prog_data), 170 &brw->cs.base.prog_offset, &brw->cs.base.prog_data); 171 ralloc_free(mem_ctx); 172 173 return true; 174 } 175 176 177 static void 178 brw_cs_populate_key(struct brw_context *brw, struct brw_cs_prog_key *key) 179 { 180 struct gl_context *ctx = &brw->ctx; 181 /* BRW_NEW_COMPUTE_PROGRAM */ 182 const struct brw_program *cp = (struct brw_program *) brw->compute_program; 183 const struct gl_program *prog = (struct gl_program *) cp; 184 185 memset(key, 0, sizeof(*key)); 186 187 /* _NEW_TEXTURE */ 188 brw_populate_sampler_prog_key_data(ctx, prog, &key->tex); 189 190 /* The unique compute program ID */ 191 key->program_string_id = cp->id; 192 } 193 194 195 void 196 brw_upload_cs_prog(struct brw_context *brw) 197 { 198 struct gl_context *ctx = &brw->ctx; 199 struct brw_cs_prog_key key; 200 struct brw_program *cp = (struct brw_program *) brw->compute_program; 201 202 if (!cp) 203 return; 204 205 if (!brw_state_dirty(brw, _NEW_TEXTURE, BRW_NEW_COMPUTE_PROGRAM)) 206 return; 207 208 brw->cs.base.sampler_count = 209 util_last_bit(ctx->ComputeProgram._Current->SamplersUsed); 210 211 brw_cs_populate_key(brw, &key); 212 213 if (!brw_search_cache(&brw->cache, BRW_CACHE_CS_PROG, 214 &key, sizeof(key), 215 &brw->cs.base.prog_offset, 216 &brw->cs.base.prog_data)) { 217 bool success = brw_codegen_cs_prog(brw, cp, &key); 218 (void) success; 219 assert(success); 220 } 221 } 222 223 224 bool 225 brw_cs_precompile(struct gl_context *ctx, struct gl_program *prog) 226 { 227 struct brw_context *brw = brw_context(ctx); 228 struct brw_cs_prog_key key; 229 230 struct brw_program *bcp = brw_program(prog); 231 232 memset(&key, 0, sizeof(key)); 233 key.program_string_id = bcp->id; 234 235 brw_setup_tex_for_precompile(brw, &key.tex, prog); 236 237 uint32_t old_prog_offset = brw->cs.base.prog_offset; 238 struct brw_stage_prog_data *old_prog_data = brw->cs.base.prog_data; 239 240 bool success = brw_codegen_cs_prog(brw, bcp, &key); 241 242 brw->cs.base.prog_offset = old_prog_offset; 243 brw->cs.base.prog_data = old_prog_data; 244 245 return success; 246 } 247