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