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