cb3fae66ec35b6f384a92ccaae5887ff14834314
[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
34 bool
35 brw_cs_prog_data_compare(const void *in_a, const void *in_b)
36 {
37 const struct brw_cs_prog_data *a =
38 (const struct brw_cs_prog_data *)in_a;
39 const struct brw_cs_prog_data *b =
40 (const struct brw_cs_prog_data *)in_b;
41
42 /* Compare the base structure. */
43 if (!brw_stage_prog_data_compare(&a->base, &b->base))
44 return false;
45
46 /* Compare the rest of the structure. */
47 const unsigned offset = sizeof(struct brw_stage_prog_data);
48 if (memcmp(((char *) a) + offset, ((char *) b) + offset,
49 sizeof(struct brw_cs_prog_data) - offset))
50 return false;
51
52 return true;
53 }
54
55 static bool
56 brw_codegen_cs_prog(struct brw_context *brw,
57 struct gl_shader_program *prog,
58 struct brw_compute_program *cp,
59 struct brw_cs_prog_key *key)
60 {
61 struct gl_context *ctx = &brw->ctx;
62 const GLuint *program;
63 void *mem_ctx = ralloc_context(NULL);
64 GLuint program_size;
65 struct brw_cs_prog_data prog_data;
66 bool start_busy = false;
67 double start_time = 0;
68
69 struct brw_shader *cs =
70 (struct brw_shader *) prog->_LinkedShaders[MESA_SHADER_COMPUTE];
71 assert (cs);
72
73 memset(&prog_data, 0, sizeof(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 = cs->base.num_uniform_components +
80 cs->base.NumImages * BRW_IMAGE_PARAM_SIZE;
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 if (unlikely(brw->perf_debug)) {
94 start_busy = (brw->batch.last_bo &&
95 drm_intel_bo_busy(brw->batch.last_bo));
96 start_time = get_time();
97 }
98
99 program = brw_cs_emit(brw, mem_ctx, key, &prog_data,
100 &cp->program, prog, &program_size);
101 if (program == NULL) {
102 ralloc_free(mem_ctx);
103 return false;
104 }
105
106 if (unlikely(brw->perf_debug) && cs) {
107 if (cs->compiled_once) {
108 _mesa_problem(&brw->ctx, "CS programs shouldn't need recompiles");
109 }
110 cs->compiled_once = true;
111
112 if (start_busy && !drm_intel_bo_busy(brw->batch.last_bo)) {
113 perf_debug("CS compile took %.03f ms and stalled the GPU\n",
114 (get_time() - start_time) * 1000);
115 }
116 }
117
118 if (prog_data.base.total_scratch) {
119 brw_get_scratch_bo(brw, &brw->cs.base.scratch_bo,
120 prog_data.base.total_scratch * brw->max_cs_threads);
121 }
122
123 if (unlikely(INTEL_DEBUG & DEBUG_CS))
124 fprintf(stderr, "\n");
125
126 brw_upload_cache(&brw->cache, BRW_CACHE_CS_PROG,
127 key, sizeof(*key),
128 program, program_size,
129 &prog_data, sizeof(prog_data),
130 &brw->cs.base.prog_offset, &brw->cs.prog_data);
131 ralloc_free(mem_ctx);
132
133 return true;
134 }
135
136
137 static void
138 brw_cs_populate_key(struct brw_context *brw, struct brw_cs_prog_key *key)
139 {
140 struct gl_context *ctx = &brw->ctx;
141 /* BRW_NEW_COMPUTE_PROGRAM */
142 const struct brw_compute_program *cp =
143 (struct brw_compute_program *) brw->compute_program;
144 const struct gl_program *prog = (struct gl_program *) cp;
145
146 memset(key, 0, sizeof(*key));
147
148 /* _NEW_TEXTURE */
149 brw_populate_sampler_prog_key_data(ctx, prog, brw->cs.base.sampler_count,
150 &key->tex);
151
152 /* The unique compute program ID */
153 key->program_string_id = cp->id;
154 }
155
156
157 void
158 brw_upload_cs_prog(struct brw_context *brw)
159 {
160 struct gl_context *ctx = &brw->ctx;
161 struct brw_cs_prog_key key;
162 struct brw_compute_program *cp = (struct brw_compute_program *)
163 brw->compute_program;
164
165 if (!cp)
166 return;
167
168 if (!brw_state_dirty(brw, _NEW_TEXTURE, BRW_NEW_COMPUTE_PROGRAM))
169 return;
170
171 brw->cs.base.sampler_count =
172 _mesa_fls(ctx->ComputeProgram._Current->Base.SamplersUsed);
173
174 brw_cs_populate_key(brw, &key);
175
176 if (!brw_search_cache(&brw->cache, BRW_CACHE_CS_PROG,
177 &key, sizeof(key),
178 &brw->cs.base.prog_offset, &brw->cs.prog_data)) {
179 bool success =
180 brw_codegen_cs_prog(brw,
181 ctx->Shader.CurrentProgram[MESA_SHADER_COMPUTE],
182 cp, &key);
183 (void) success;
184 assert(success);
185 }
186 brw->cs.base.prog_data = &brw->cs.prog_data->base;
187 }
188
189
190 bool
191 brw_cs_precompile(struct gl_context *ctx,
192 struct gl_shader_program *shader_prog,
193 struct gl_program *prog)
194 {
195 struct brw_context *brw = brw_context(ctx);
196 struct brw_cs_prog_key key;
197
198 struct gl_compute_program *cp = (struct gl_compute_program *) prog;
199 struct brw_compute_program *bcp = brw_compute_program(cp);
200
201 memset(&key, 0, sizeof(key));
202 key.program_string_id = bcp->id;
203
204 brw_setup_tex_for_precompile(brw, &key.tex, prog);
205
206 uint32_t old_prog_offset = brw->cs.base.prog_offset;
207 struct brw_cs_prog_data *old_prog_data = brw->cs.prog_data;
208
209 bool success = brw_codegen_cs_prog(brw, shader_prog, bcp, &key);
210
211 brw->cs.base.prog_offset = old_prog_offset;
212 brw->cs.prog_data = old_prog_data;
213
214 return success;
215 }