i965: Push down inclusion of brw_program.h.
[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 static void
37 assign_cs_binding_table_offsets(const struct brw_device_info *devinfo,
38 const struct gl_shader_program *shader_prog,
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(MESA_SHADER_COMPUTE, devinfo,
49 shader_prog, prog, &prog_data->base,
50 next_binding_table_offset);
51 }
52
53 static bool
54 brw_codegen_cs_prog(struct brw_context *brw,
55 struct gl_shader_program *prog,
56 struct brw_compute_program *cp,
57 struct brw_cs_prog_key *key)
58 {
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 struct brw_shader *cs =
68 (struct brw_shader *) prog->_LinkedShaders[MESA_SHADER_COMPUTE];
69 assert (cs);
70
71 memset(&prog_data, 0, sizeof(prog_data));
72
73 assign_cs_binding_table_offsets(brw->intelScreen->devinfo, prog,
74 &cp->program.Base, &prog_data);
75
76 /* Allocate the references to the uniforms that will end up in the
77 * prog_data associated with the compiled program, and which will be freed
78 * by the state cache.
79 */
80 int param_count = cp->program.Base.nir->num_uniforms;
81
82 /* The backend also sometimes adds params for texture size. */
83 param_count += 2 * ctx->Const.Program[MESA_SHADER_COMPUTE].MaxTextureImageUnits;
84 prog_data.base.param =
85 rzalloc_array(NULL, const gl_constant_value *, param_count);
86 prog_data.base.pull_param =
87 rzalloc_array(NULL, const gl_constant_value *, param_count);
88 prog_data.base.image_param =
89 rzalloc_array(NULL, struct brw_image_param, cs->base.NumImages);
90 prog_data.base.nr_params = param_count;
91 prog_data.base.nr_image_params = cs->base.NumImages;
92
93 brw_nir_setup_glsl_uniforms(cp->program.Base.nir, prog, &cp->program.Base,
94 &prog_data.base, true);
95
96 if (unlikely(brw->perf_debug)) {
97 start_busy = (brw->batch.last_bo &&
98 drm_intel_bo_busy(brw->batch.last_bo));
99 start_time = get_time();
100 }
101
102 if (unlikely(INTEL_DEBUG & DEBUG_CS))
103 brw_dump_ir("compute", prog, &cs->base, &cp->program.Base);
104
105 int st_index = -1;
106 if (INTEL_DEBUG & DEBUG_SHADER_TIME)
107 st_index = brw_get_shader_time_index(brw, prog, &cp->program.Base, ST_CS);
108
109 char *error_str;
110 program = brw_compile_cs(brw->intelScreen->compiler, brw, mem_ctx,
111 key, &prog_data, cp->program.Base.nir,
112 st_index, &program_size, &error_str);
113 if (program == NULL) {
114 prog->LinkStatus = false;
115 ralloc_strcat(&prog->InfoLog, error_str);
116 _mesa_problem(NULL, "Failed to compile compute shader: %s\n", error_str);
117
118 ralloc_free(mem_ctx);
119 return false;
120 }
121
122 if (unlikely(brw->perf_debug) && cs) {
123 if (cs->compiled_once) {
124 _mesa_problem(&brw->ctx, "CS programs shouldn't need recompiles");
125 }
126 cs->compiled_once = true;
127
128 if (start_busy && !drm_intel_bo_busy(brw->batch.last_bo)) {
129 perf_debug("CS compile took %.03f ms and stalled the GPU\n",
130 (get_time() - start_time) * 1000);
131 }
132 }
133
134 if (prog_data.base.total_scratch) {
135 brw_get_scratch_bo(brw, &brw->cs.base.scratch_bo,
136 prog_data.base.total_scratch * brw->max_cs_threads);
137 }
138
139 if (unlikely(INTEL_DEBUG & DEBUG_CS))
140 fprintf(stderr, "\n");
141
142 brw_upload_cache(&brw->cache, BRW_CACHE_CS_PROG,
143 key, sizeof(*key),
144 program, program_size,
145 &prog_data, sizeof(prog_data),
146 &brw->cs.base.prog_offset, &brw->cs.prog_data);
147 ralloc_free(mem_ctx);
148
149 return true;
150 }
151
152
153 static void
154 brw_cs_populate_key(struct brw_context *brw, struct brw_cs_prog_key *key)
155 {
156 struct gl_context *ctx = &brw->ctx;
157 /* BRW_NEW_COMPUTE_PROGRAM */
158 const struct brw_compute_program *cp =
159 (struct brw_compute_program *) brw->compute_program;
160 const struct gl_program *prog = (struct gl_program *) cp;
161
162 memset(key, 0, sizeof(*key));
163
164 /* _NEW_TEXTURE */
165 brw_populate_sampler_prog_key_data(ctx, prog, brw->cs.base.sampler_count,
166 &key->tex);
167
168 /* The unique compute program ID */
169 key->program_string_id = cp->id;
170 }
171
172
173 void
174 brw_upload_cs_prog(struct brw_context *brw)
175 {
176 struct gl_context *ctx = &brw->ctx;
177 struct brw_cs_prog_key key;
178 struct brw_compute_program *cp = (struct brw_compute_program *)
179 brw->compute_program;
180
181 if (!cp)
182 return;
183
184 if (!brw_state_dirty(brw, _NEW_TEXTURE, BRW_NEW_COMPUTE_PROGRAM))
185 return;
186
187 brw->cs.base.sampler_count =
188 _mesa_fls(ctx->ComputeProgram._Current->Base.SamplersUsed);
189
190 brw_cs_populate_key(brw, &key);
191
192 if (!brw_search_cache(&brw->cache, BRW_CACHE_CS_PROG,
193 &key, sizeof(key),
194 &brw->cs.base.prog_offset, &brw->cs.prog_data)) {
195 bool success =
196 brw_codegen_cs_prog(brw,
197 ctx->Shader.CurrentProgram[MESA_SHADER_COMPUTE],
198 cp, &key);
199 (void) success;
200 assert(success);
201 }
202 brw->cs.base.prog_data = &brw->cs.prog_data->base;
203 }
204
205
206 bool
207 brw_cs_precompile(struct gl_context *ctx,
208 struct gl_shader_program *shader_prog,
209 struct gl_program *prog)
210 {
211 struct brw_context *brw = brw_context(ctx);
212 struct brw_cs_prog_key key;
213
214 struct gl_compute_program *cp = (struct gl_compute_program *) prog;
215 struct brw_compute_program *bcp = brw_compute_program(cp);
216
217 memset(&key, 0, sizeof(key));
218 key.program_string_id = bcp->id;
219
220 brw_setup_tex_for_precompile(brw, &key.tex, prog);
221
222 uint32_t old_prog_offset = brw->cs.base.prog_offset;
223 struct brw_cs_prog_data *old_prog_data = brw->cs.prog_data;
224
225 bool success = brw_codegen_cs_prog(brw, shader_prog, bcp, &key);
226
227 brw->cs.base.prog_offset = old_prog_offset;
228 brw->cs.prog_data = old_prog_data;
229
230 return success;
231 }