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