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