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