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