i965: Enable OpenGL 4.5 on Haswell.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_gs.c
1 /*
2 * Copyright © 2013 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 /**
25 * \file brw_vec4_gs.c
26 *
27 * State atom for client-programmable geometry shaders, and support code.
28 */
29
30 #include "brw_gs.h"
31 #include "brw_context.h"
32 #include "brw_vec4_gs_visitor.h"
33 #include "brw_state.h"
34 #include "brw_ff_gs.h"
35 #include "brw_nir.h"
36 #include "brw_program.h"
37 #include "compiler/glsl/ir_uniform.h"
38
39 static void
40 brw_gs_debug_recompile(struct brw_context *brw, struct gl_program *prog,
41 const struct brw_gs_prog_key *key)
42 {
43 struct brw_cache_item *c = NULL;
44 const struct brw_gs_prog_key *old_key = NULL;
45 bool found = false;
46
47 perf_debug("Recompiling geometry shader for program %d\n", prog->Id);
48
49 for (unsigned int i = 0; i < brw->cache.size; i++) {
50 for (c = brw->cache.items[i]; c; c = c->next) {
51 if (c->cache_id == BRW_CACHE_GS_PROG) {
52 old_key = c->key;
53
54 if (old_key->program_string_id == key->program_string_id)
55 break;
56 }
57 }
58 if (c)
59 break;
60 }
61
62 if (!c) {
63 perf_debug(" Didn't find previous compile in the shader cache for "
64 "debug\n");
65 return;
66 }
67
68 found |= brw_debug_recompile_sampler_key(brw, &old_key->tex, &key->tex);
69
70 if (!found) {
71 perf_debug(" Something else\n");
72 }
73 }
74
75 static void
76 assign_gs_binding_table_offsets(const struct gen_device_info *devinfo,
77 const struct gl_program *prog,
78 struct brw_gs_prog_data *prog_data)
79 {
80 /* In gen6 we reserve the first BRW_MAX_SOL_BINDINGS entries for transform
81 * feedback surfaces.
82 */
83 uint32_t reserved = devinfo->gen == 6 ? BRW_MAX_SOL_BINDINGS : 0;
84
85 brw_assign_common_binding_table_offsets(devinfo, prog,
86 &prog_data->base.base, reserved);
87 }
88
89 static bool
90 brw_codegen_gs_prog(struct brw_context *brw,
91 struct brw_program *gp,
92 struct brw_gs_prog_key *key)
93 {
94 struct brw_compiler *compiler = brw->screen->compiler;
95 const struct gen_device_info *devinfo = &brw->screen->devinfo;
96 struct brw_stage_state *stage_state = &brw->gs.base;
97 struct brw_gs_prog_data prog_data;
98 bool start_busy = false;
99 double start_time = 0;
100
101 memset(&prog_data, 0, sizeof(prog_data));
102
103 assign_gs_binding_table_offsets(devinfo, &gp->program, &prog_data);
104
105 /* Allocate the references to the uniforms that will end up in the
106 * prog_data associated with the compiled program, and which will be freed
107 * by the state cache.
108 *
109 * Note: param_count needs to be num_uniform_components * 4, since we add
110 * padding around uniform values below vec4 size, so the worst case is that
111 * every uniform is a float which gets padded to the size of a vec4.
112 */
113 int param_count = gp->program.nir->num_uniforms / 4;
114
115 prog_data.base.base.param =
116 rzalloc_array(NULL, const gl_constant_value *, param_count);
117 prog_data.base.base.pull_param =
118 rzalloc_array(NULL, const gl_constant_value *, param_count);
119 prog_data.base.base.image_param =
120 rzalloc_array(NULL, struct brw_image_param,
121 gp->program.info.num_images);
122 prog_data.base.base.nr_params = param_count;
123 prog_data.base.base.nr_image_params = gp->program.info.num_images;
124
125 brw_nir_setup_glsl_uniforms(gp->program.nir, &gp->program,
126 &prog_data.base.base,
127 compiler->scalar_stage[MESA_SHADER_GEOMETRY]);
128
129 uint64_t outputs_written = gp->program.info.outputs_written;
130
131 brw_compute_vue_map(devinfo,
132 &prog_data.base.vue_map, outputs_written,
133 gp->program.info.separate_shader);
134
135 int st_index = -1;
136 if (INTEL_DEBUG & DEBUG_SHADER_TIME)
137 st_index = brw_get_shader_time_index(brw, &gp->program, ST_GS, true);
138
139 if (unlikely(brw->perf_debug)) {
140 start_busy = brw->batch.last_bo && drm_intel_bo_busy(brw->batch.last_bo);
141 start_time = get_time();
142 }
143
144 void *mem_ctx = ralloc_context(NULL);
145 unsigned program_size;
146 char *error_str;
147 const unsigned *program =
148 brw_compile_gs(brw->screen->compiler, brw, mem_ctx, key,
149 &prog_data, gp->program.nir, &gp->program,
150 st_index, &program_size, &error_str);
151 if (program == NULL) {
152 ralloc_strcat(&gp->program.sh.data->InfoLog, error_str);
153 _mesa_problem(NULL, "Failed to compile geometry shader: %s\n", error_str);
154
155 ralloc_free(mem_ctx);
156 return false;
157 }
158
159 if (unlikely(brw->perf_debug)) {
160 if (gp->compiled_once) {
161 brw_gs_debug_recompile(brw, &gp->program, key);
162 }
163 if (start_busy && !drm_intel_bo_busy(brw->batch.last_bo)) {
164 perf_debug("GS compile took %.03f ms and stalled the GPU\n",
165 (get_time() - start_time) * 1000);
166 }
167 gp->compiled_once = true;
168 }
169
170 /* Scratch space is used for register spilling */
171 brw_alloc_stage_scratch(brw, stage_state,
172 prog_data.base.base.total_scratch,
173 devinfo->max_gs_threads);
174
175 brw_upload_cache(&brw->cache, BRW_CACHE_GS_PROG,
176 key, sizeof(*key),
177 program, program_size,
178 &prog_data, sizeof(prog_data),
179 &stage_state->prog_offset, &brw->gs.base.prog_data);
180 ralloc_free(mem_ctx);
181
182 return true;
183 }
184
185 static bool
186 brw_gs_state_dirty(const struct brw_context *brw)
187 {
188 return brw_state_dirty(brw,
189 _NEW_TEXTURE,
190 BRW_NEW_GEOMETRY_PROGRAM |
191 BRW_NEW_TRANSFORM_FEEDBACK);
192 }
193
194 void
195 brw_gs_populate_key(struct brw_context *brw,
196 struct brw_gs_prog_key *key)
197 {
198 struct gl_context *ctx = &brw->ctx;
199 struct brw_program *gp = (struct brw_program *) brw->geometry_program;
200
201 memset(key, 0, sizeof(*key));
202
203 key->program_string_id = gp->id;
204
205 /* _NEW_TEXTURE */
206 brw_populate_sampler_prog_key_data(ctx, &gp->program, &key->tex);
207 }
208
209 void
210 brw_upload_gs_prog(struct brw_context *brw)
211 {
212 struct brw_stage_state *stage_state = &brw->gs.base;
213 struct brw_gs_prog_key key;
214 /* BRW_NEW_GEOMETRY_PROGRAM */
215 struct brw_program *gp = (struct brw_program *) brw->geometry_program;
216
217 if (!brw_gs_state_dirty(brw))
218 return;
219
220 if (gp == NULL) {
221 /* No geometry shader. Vertex data just passes straight through. */
222 if (brw->gen == 6 &&
223 (brw->ctx.NewDriverState & BRW_NEW_TRANSFORM_FEEDBACK)) {
224 gen6_brw_upload_ff_gs_prog(brw);
225 return;
226 }
227
228 /* Other state atoms had better not try to access prog_data, since
229 * there's no GS program.
230 */
231 brw->gs.base.prog_data = NULL;
232
233 return;
234 }
235
236 brw_gs_populate_key(brw, &key);
237
238 if (!brw_search_cache(&brw->cache, BRW_CACHE_GS_PROG,
239 &key, sizeof(key),
240 &stage_state->prog_offset,
241 &brw->gs.base.prog_data)) {
242 bool success = brw_codegen_gs_prog(brw, gp, &key);
243 assert(success);
244 (void)success;
245 }
246 }
247
248 bool
249 brw_gs_precompile(struct gl_context *ctx, struct gl_program *prog)
250 {
251 struct brw_context *brw = brw_context(ctx);
252 struct brw_gs_prog_key key;
253 uint32_t old_prog_offset = brw->gs.base.prog_offset;
254 struct brw_stage_prog_data *old_prog_data = brw->gs.base.prog_data;
255 bool success;
256
257 struct brw_program *bgp = brw_program(prog);
258
259 memset(&key, 0, sizeof(key));
260
261 brw_setup_tex_for_precompile(brw, &key.tex, prog);
262 key.program_string_id = bgp->id;
263
264 success = brw_codegen_gs_prog(brw, bgp, &key);
265
266 brw->gs.base.prog_offset = old_prog_offset;
267 brw->gs.base.prog_data = old_prog_data;
268
269 return success;
270 }