i965: Use correct constant for max_variable_local_size
[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 uint32_t
36 brw_cs_group_size(const struct brw_context *brw)
37 {
38 assert(brw->cs.base.prog_data);
39 struct brw_cs_prog_data *cs_prog_data =
40 brw_cs_prog_data(brw->cs.base.prog_data);
41
42 if (brw->compute.group_size) {
43 /* With ARB_compute_variable_group_size the group size is set at
44 * dispatch time, so we can't use the one provided by the compiler.
45 */
46 return brw->compute.group_size[0] *
47 brw->compute.group_size[1] *
48 brw->compute.group_size[2];
49 } else {
50 return cs_prog_data->local_size[0] *
51 cs_prog_data->local_size[1] *
52 cs_prog_data->local_size[2];
53 }
54 }
55
56 static void
57 assign_cs_binding_table_offsets(const struct gen_device_info *devinfo,
58 const struct gl_program *prog,
59 struct brw_cs_prog_data *prog_data)
60 {
61 uint32_t next_binding_table_offset = 0;
62
63 /* May not be used if the gl_NumWorkGroups variable is not accessed. */
64 prog_data->binding_table.work_groups_start = next_binding_table_offset;
65 next_binding_table_offset++;
66
67 brw_assign_common_binding_table_offsets(devinfo, prog, &prog_data->base,
68 next_binding_table_offset);
69 }
70
71 static bool
72 brw_codegen_cs_prog(struct brw_context *brw,
73 struct brw_program *cp,
74 struct brw_cs_prog_key *key)
75 {
76 const struct gen_device_info *devinfo = &brw->screen->devinfo;
77 const GLuint *program;
78 void *mem_ctx = ralloc_context(NULL);
79 struct brw_cs_prog_data prog_data;
80 bool start_busy = false;
81 double start_time = 0;
82 struct gl_context *gl_ctx = &brw->ctx;
83 nir_shader *nir = nir_shader_clone(mem_ctx, cp->program.nir);
84
85 memset(&prog_data, 0, sizeof(prog_data));
86
87 if (cp->program.info.cs.shared_size > 64 * 1024) {
88 cp->program.sh.data->LinkStatus = LINKING_FAILURE;
89 const char *error_str =
90 "Compute shader used more than 64KB of shared variables";
91 ralloc_strcat(&cp->program.sh.data->InfoLog, error_str);
92 _mesa_problem(NULL, "Failed to link compute shader: %s\n", error_str);
93
94 ralloc_free(mem_ctx);
95 return false;
96 }
97
98 assign_cs_binding_table_offsets(devinfo, &cp->program, &prog_data);
99
100 brw_nir_setup_glsl_uniforms(mem_ctx, nir,
101 &cp->program, &prog_data.base, 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 /* If the work group size is variable we set it to the maximum here since
114 * the actual size is not known until the dispatch command is issued.
115 */
116 if (nir->info.cs.local_size_variable) {
117 prog_data.uses_variable_group_size = true;
118 nir->info.cs.max_variable_local_size =
119 gl_ctx->Const.MaxComputeVariableGroupInvocations;
120 } else {
121 prog_data.uses_variable_group_size = false;
122 }
123
124 char *error_str;
125 program = brw_compile_cs(brw->screen->compiler, brw, mem_ctx, key,
126 &prog_data, nir, st_index, NULL, &error_str);
127 if (program == NULL) {
128 cp->program.sh.data->LinkStatus = LINKING_FAILURE;
129 ralloc_strcat(&cp->program.sh.data->InfoLog, error_str);
130 _mesa_problem(NULL, "Failed to compile compute shader: %s\n", error_str);
131
132 ralloc_free(mem_ctx);
133 return false;
134 }
135
136 if (unlikely(brw->perf_debug)) {
137 if (cp->compiled_once) {
138 brw_debug_recompile(brw, MESA_SHADER_COMPUTE, cp->program.Id,
139 &key->base);
140 }
141 cp->compiled_once = true;
142
143 if (start_busy && !brw_bo_busy(brw->batch.last_bo)) {
144 perf_debug("CS compile took %.03f ms and stalled the GPU\n",
145 (get_time() - start_time) * 1000);
146 }
147 }
148
149 brw_alloc_stage_scratch(brw, &brw->cs.base, prog_data.base.total_scratch);
150
151 /* The param and pull_param arrays will be freed by the shader cache. */
152 ralloc_steal(NULL, prog_data.base.param);
153 ralloc_steal(NULL, prog_data.base.pull_param);
154 brw_upload_cache(&brw->cache, BRW_CACHE_CS_PROG,
155 key, sizeof(*key),
156 program, prog_data.base.program_size,
157 &prog_data, sizeof(prog_data),
158 &brw->cs.base.prog_offset, &brw->cs.base.prog_data);
159 ralloc_free(mem_ctx);
160
161 return true;
162 }
163
164
165 void
166 brw_cs_populate_key(struct brw_context *brw, struct brw_cs_prog_key *key)
167 {
168 struct gl_context *ctx = &brw->ctx;
169 /* BRW_NEW_COMPUTE_PROGRAM */
170 const struct brw_program *cp =
171 (struct brw_program *) brw->programs[MESA_SHADER_COMPUTE];
172
173 memset(key, 0, sizeof(*key));
174
175 /* _NEW_TEXTURE */
176 brw_populate_base_prog_key(ctx, cp, &key->base);
177 }
178
179
180 void
181 brw_upload_cs_prog(struct brw_context *brw)
182 {
183 struct gl_context *ctx = &brw->ctx;
184 struct brw_cs_prog_key key;
185 struct brw_program *cp =
186 (struct brw_program *) brw->programs[MESA_SHADER_COMPUTE];
187
188 if (!cp)
189 return;
190
191 if (!brw_state_dirty(brw, _NEW_TEXTURE, BRW_NEW_COMPUTE_PROGRAM))
192 return;
193
194 brw->cs.base.sampler_count =
195 util_last_bit(ctx->ComputeProgram._Current->SamplersUsed);
196
197 brw_cs_populate_key(brw, &key);
198
199 if (brw_search_cache(&brw->cache, BRW_CACHE_CS_PROG, &key, sizeof(key),
200 &brw->cs.base.prog_offset, &brw->cs.base.prog_data,
201 true))
202 return;
203
204 if (brw_disk_cache_upload_program(brw, MESA_SHADER_COMPUTE))
205 return;
206
207 cp = (struct brw_program *) brw->programs[MESA_SHADER_COMPUTE];
208 cp->id = key.base.program_string_id;
209
210 ASSERTED bool success = brw_codegen_cs_prog(brw, cp, &key);
211 assert(success);
212 }
213
214 void
215 brw_cs_populate_default_key(const struct brw_compiler *compiler,
216 struct brw_cs_prog_key *key,
217 struct gl_program *prog)
218 {
219 const struct gen_device_info *devinfo = compiler->devinfo;
220 memset(key, 0, sizeof(*key));
221 brw_populate_default_base_prog_key(devinfo, brw_program(prog), &key->base);
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 brw_cs_populate_default_key(brw->screen->compiler, &key, prog);
233
234 uint32_t old_prog_offset = brw->cs.base.prog_offset;
235 struct brw_stage_prog_data *old_prog_data = brw->cs.base.prog_data;
236
237 bool success = brw_codegen_cs_prog(brw, bcp, &key);
238
239 brw->cs.base.prog_offset = old_prog_offset;
240 brw->cs.base.prog_data = old_prog_data;
241
242 return success;
243 }