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_state.h"
33 #include "brw_ff_gs.h"
34 #include "compiler/brw_nir.h"
35 #include "brw_program.h"
36 #include "compiler/glsl/ir_uniform.h"
37
38 static void
assign_gs_binding_table_offsets(const struct gen_device_info * devinfo,const struct gl_program * prog,struct brw_gs_prog_data * prog_data)39 assign_gs_binding_table_offsets(const struct gen_device_info *devinfo,
40 const struct gl_program *prog,
41 struct brw_gs_prog_data *prog_data)
42 {
43 /* In gen6 we reserve the first BRW_MAX_SOL_BINDINGS entries for transform
44 * feedback surfaces.
45 */
46 uint32_t reserved = devinfo->gen == 6 ? BRW_MAX_SOL_BINDINGS : 0;
47
48 brw_assign_common_binding_table_offsets(devinfo, prog,
49 &prog_data->base.base, reserved);
50 }
51
52 static bool
brw_codegen_gs_prog(struct brw_context * brw,struct brw_program * gp,struct brw_gs_prog_key * key)53 brw_codegen_gs_prog(struct brw_context *brw,
54 struct brw_program *gp,
55 struct brw_gs_prog_key *key)
56 {
57 struct brw_compiler *compiler = brw->screen->compiler;
58 const struct gen_device_info *devinfo = &brw->screen->devinfo;
59 struct brw_stage_state *stage_state = &brw->gs.base;
60 struct brw_gs_prog_data prog_data;
61 bool start_busy = false;
62 double start_time = 0;
63
64 memset(&prog_data, 0, sizeof(prog_data));
65
66 void *mem_ctx = ralloc_context(NULL);
67
68 nir_shader *nir = nir_shader_clone(mem_ctx, gp->program.nir);
69
70 assign_gs_binding_table_offsets(devinfo, &gp->program, &prog_data);
71
72 brw_nir_setup_glsl_uniforms(mem_ctx, nir, &gp->program,
73 &prog_data.base.base,
74 compiler->scalar_stage[MESA_SHADER_GEOMETRY]);
75 brw_nir_analyze_ubo_ranges(compiler, nir, NULL,
76 prog_data.base.base.ubo_ranges);
77
78 uint64_t outputs_written = nir->info.outputs_written;
79
80 brw_compute_vue_map(devinfo,
81 &prog_data.base.vue_map, outputs_written,
82 gp->program.info.separate_shader, 1);
83
84 int st_index = -1;
85 if (INTEL_DEBUG & DEBUG_SHADER_TIME)
86 st_index = brw_get_shader_time_index(brw, &gp->program, ST_GS, true);
87
88 if (unlikely(brw->perf_debug)) {
89 start_busy = brw->batch.last_bo && brw_bo_busy(brw->batch.last_bo);
90 start_time = get_time();
91 }
92
93 char *error_str;
94 const unsigned *program =
95 brw_compile_gs(brw->screen->compiler, brw, mem_ctx, key,
96 &prog_data, nir, &gp->program, st_index,
97 NULL, &error_str);
98 if (program == NULL) {
99 ralloc_strcat(&gp->program.sh.data->InfoLog, error_str);
100 _mesa_problem(NULL, "Failed to compile geometry shader: %s\n", error_str);
101
102 ralloc_free(mem_ctx);
103 return false;
104 }
105
106 if (unlikely(brw->perf_debug)) {
107 if (gp->compiled_once) {
108 brw_debug_recompile(brw, MESA_SHADER_GEOMETRY, gp->program.Id,
109 &key->base);
110 }
111 if (start_busy && !brw_bo_busy(brw->batch.last_bo)) {
112 perf_debug("GS compile took %.03f ms and stalled the GPU\n",
113 (get_time() - start_time) * 1000);
114 }
115 gp->compiled_once = true;
116 }
117
118 /* Scratch space is used for register spilling */
119 brw_alloc_stage_scratch(brw, stage_state,
120 prog_data.base.base.total_scratch);
121
122 /* The param and pull_param arrays will be freed by the shader cache. */
123 ralloc_steal(NULL, prog_data.base.base.param);
124 ralloc_steal(NULL, prog_data.base.base.pull_param);
125 brw_upload_cache(&brw->cache, BRW_CACHE_GS_PROG,
126 key, sizeof(*key),
127 program, prog_data.base.base.program_size,
128 &prog_data, sizeof(prog_data),
129 &stage_state->prog_offset, &brw->gs.base.prog_data);
130 ralloc_free(mem_ctx);
131
132 return true;
133 }
134
135 static bool
brw_gs_state_dirty(const struct brw_context * brw)136 brw_gs_state_dirty(const struct brw_context *brw)
137 {
138 return brw_state_dirty(brw,
139 _NEW_TEXTURE,
140 BRW_NEW_GEOMETRY_PROGRAM |
141 BRW_NEW_TRANSFORM_FEEDBACK);
142 }
143
144 void
brw_gs_populate_key(struct brw_context * brw,struct brw_gs_prog_key * key)145 brw_gs_populate_key(struct brw_context *brw,
146 struct brw_gs_prog_key *key)
147 {
148 struct gl_context *ctx = &brw->ctx;
149 struct brw_program *gp =
150 (struct brw_program *) brw->programs[MESA_SHADER_GEOMETRY];
151
152 memset(key, 0, sizeof(*key));
153
154 brw_populate_base_prog_key(ctx, gp, &key->base);
155 }
156
157 void
brw_upload_gs_prog(struct brw_context * brw)158 brw_upload_gs_prog(struct brw_context *brw)
159 {
160 struct brw_stage_state *stage_state = &brw->gs.base;
161 struct brw_gs_prog_key key;
162 /* BRW_NEW_GEOMETRY_PROGRAM */
163 struct brw_program *gp =
164 (struct brw_program *) brw->programs[MESA_SHADER_GEOMETRY];
165
166 if (!brw_gs_state_dirty(brw))
167 return;
168
169 brw_gs_populate_key(brw, &key);
170
171 if (brw_search_cache(&brw->cache, BRW_CACHE_GS_PROG, &key, sizeof(key),
172 &stage_state->prog_offset, &brw->gs.base.prog_data,
173 true))
174 return;
175
176 if (brw_disk_cache_upload_program(brw, MESA_SHADER_GEOMETRY))
177 return;
178
179 gp = (struct brw_program *) brw->programs[MESA_SHADER_GEOMETRY];
180 gp->id = key.base.program_string_id;
181
182 ASSERTED bool success = brw_codegen_gs_prog(brw, gp, &key);
183 assert(success);
184 }
185
186 void
brw_gs_populate_default_key(const struct brw_compiler * compiler,struct brw_gs_prog_key * key,struct gl_program * prog)187 brw_gs_populate_default_key(const struct brw_compiler *compiler,
188 struct brw_gs_prog_key *key,
189 struct gl_program *prog)
190 {
191 const struct gen_device_info *devinfo = compiler->devinfo;
192
193 memset(key, 0, sizeof(*key));
194
195 brw_populate_default_base_prog_key(devinfo, brw_program(prog),
196 &key->base);
197 }
198
199 bool
brw_gs_precompile(struct gl_context * ctx,struct gl_program * prog)200 brw_gs_precompile(struct gl_context *ctx, struct gl_program *prog)
201 {
202 struct brw_context *brw = brw_context(ctx);
203 struct brw_gs_prog_key key;
204 uint32_t old_prog_offset = brw->gs.base.prog_offset;
205 struct brw_stage_prog_data *old_prog_data = brw->gs.base.prog_data;
206 bool success;
207
208 struct brw_program *bgp = brw_program(prog);
209
210 brw_gs_populate_default_key(brw->screen->compiler, &key, prog);
211
212 success = brw_codegen_gs_prog(brw, bgp, &key);
213
214 brw->gs.base.prog_offset = old_prog_offset;
215 brw->gs.base.prog_data = old_prog_data;
216
217 return success;
218 }
219