2 * Copyright © 2013 Intel Corporation
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:
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
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.
27 * State atom for client-programmable geometry shaders, and support code.
31 #include "brw_context.h"
32 #include "brw_vec4_gs_visitor.h"
33 #include "brw_state.h"
34 #include "brw_ff_gs.h"
36 #include "brw_program.h"
37 #include "compiler/glsl/ir_uniform.h"
40 brw_gs_debug_recompile(struct brw_context
*brw
, struct gl_program
*prog
,
41 const struct brw_gs_prog_key
*key
)
43 struct brw_cache_item
*c
= NULL
;
44 const struct brw_gs_prog_key
*old_key
= NULL
;
47 perf_debug("Recompiling geometry shader for program %d\n", prog
->Id
);
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
) {
54 if (old_key
->program_string_id
== key
->program_string_id
)
63 perf_debug(" Didn't find previous compile in the shader cache for "
68 found
|= brw_debug_recompile_sampler_key(brw
, &old_key
->tex
, &key
->tex
);
71 perf_debug(" Something else\n");
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
)
80 /* In gen6 we reserve the first BRW_MAX_SOL_BINDINGS entries for transform
83 uint32_t reserved
= devinfo
->gen
== 6 ? BRW_MAX_SOL_BINDINGS
: 0;
85 brw_assign_common_binding_table_offsets(devinfo
, prog
,
86 &prog_data
->base
.base
, reserved
);
90 brw_codegen_gs_prog(struct brw_context
*brw
,
91 struct gl_shader_program
*prog
,
92 struct brw_program
*gp
,
93 struct brw_gs_prog_key
*key
)
95 struct brw_compiler
*compiler
= brw
->screen
->compiler
;
96 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
97 struct brw_stage_state
*stage_state
= &brw
->gs
.base
;
98 struct brw_gs_prog_data prog_data
;
99 bool start_busy
= false;
100 double start_time
= 0;
102 memset(&prog_data
, 0, sizeof(prog_data
));
104 assign_gs_binding_table_offsets(devinfo
, &gp
->program
, &prog_data
);
106 /* Allocate the references to the uniforms that will end up in the
107 * prog_data associated with the compiled program, and which will be freed
108 * by the state cache.
110 * Note: param_count needs to be num_uniform_components * 4, since we add
111 * padding around uniform values below vec4 size, so the worst case is that
112 * every uniform is a float which gets padded to the size of a vec4.
114 int param_count
= gp
->program
.nir
->num_uniforms
/ 4;
116 prog_data
.base
.base
.param
=
117 rzalloc_array(NULL
, const gl_constant_value
*, param_count
);
118 prog_data
.base
.base
.pull_param
=
119 rzalloc_array(NULL
, const gl_constant_value
*, param_count
);
120 prog_data
.base
.base
.image_param
=
121 rzalloc_array(NULL
, struct brw_image_param
,
122 gp
->program
.info
.num_images
);
123 prog_data
.base
.base
.nr_params
= param_count
;
124 prog_data
.base
.base
.nr_image_params
= gp
->program
.info
.num_images
;
126 brw_nir_setup_glsl_uniforms(gp
->program
.nir
, prog
, &gp
->program
,
127 &prog_data
.base
.base
,
128 compiler
->scalar_stage
[MESA_SHADER_GEOMETRY
]);
130 uint64_t outputs_written
= gp
->program
.info
.outputs_written
;
132 brw_compute_vue_map(devinfo
,
133 &prog_data
.base
.vue_map
, outputs_written
,
134 gp
->program
.info
.separate_shader
);
137 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
)
138 st_index
= brw_get_shader_time_index(brw
, &gp
->program
, ST_GS
, true);
140 if (unlikely(brw
->perf_debug
)) {
141 start_busy
= brw
->batch
.last_bo
&& drm_intel_bo_busy(brw
->batch
.last_bo
);
142 start_time
= get_time();
145 void *mem_ctx
= ralloc_context(NULL
);
146 unsigned program_size
;
148 const unsigned *program
=
149 brw_compile_gs(brw
->screen
->compiler
, brw
, mem_ctx
, key
,
150 &prog_data
, gp
->program
.nir
, &gp
->program
,
151 st_index
, &program_size
, &error_str
);
152 if (program
== NULL
) {
153 ralloc_strcat(&gp
->program
.sh
.data
->InfoLog
, error_str
);
154 _mesa_problem(NULL
, "Failed to compile geometry shader: %s\n", error_str
);
156 ralloc_free(mem_ctx
);
160 if (unlikely(brw
->perf_debug
)) {
161 if (gp
->compiled_once
) {
162 brw_gs_debug_recompile(brw
, &gp
->program
, key
);
164 if (start_busy
&& !drm_intel_bo_busy(brw
->batch
.last_bo
)) {
165 perf_debug("GS compile took %.03f ms and stalled the GPU\n",
166 (get_time() - start_time
) * 1000);
168 gp
->compiled_once
= true;
171 /* Scratch space is used for register spilling */
172 brw_alloc_stage_scratch(brw
, stage_state
,
173 prog_data
.base
.base
.total_scratch
,
174 devinfo
->max_gs_threads
);
176 brw_upload_cache(&brw
->cache
, BRW_CACHE_GS_PROG
,
178 program
, program_size
,
179 &prog_data
, sizeof(prog_data
),
180 &stage_state
->prog_offset
, &brw
->gs
.base
.prog_data
);
181 ralloc_free(mem_ctx
);
187 brw_gs_state_dirty(const struct brw_context
*brw
)
189 return brw_state_dirty(brw
,
191 BRW_NEW_GEOMETRY_PROGRAM
|
192 BRW_NEW_TRANSFORM_FEEDBACK
);
196 brw_gs_populate_key(struct brw_context
*brw
,
197 struct brw_gs_prog_key
*key
)
199 struct gl_context
*ctx
= &brw
->ctx
;
200 struct brw_program
*gp
= (struct brw_program
*) brw
->geometry_program
;
202 memset(key
, 0, sizeof(*key
));
204 key
->program_string_id
= gp
->id
;
207 brw_populate_sampler_prog_key_data(ctx
, &gp
->program
, &key
->tex
);
211 brw_upload_gs_prog(struct brw_context
*brw
)
213 struct gl_context
*ctx
= &brw
->ctx
;
214 struct gl_shader_program
**current
= ctx
->_Shader
->CurrentProgram
;
215 struct brw_stage_state
*stage_state
= &brw
->gs
.base
;
216 struct brw_gs_prog_key key
;
217 /* BRW_NEW_GEOMETRY_PROGRAM */
218 struct brw_program
*gp
= (struct brw_program
*) brw
->geometry_program
;
220 if (!brw_gs_state_dirty(brw
))
224 /* No geometry shader. Vertex data just passes straight through. */
226 (brw
->ctx
.NewDriverState
& BRW_NEW_TRANSFORM_FEEDBACK
)) {
227 gen6_brw_upload_ff_gs_prog(brw
);
231 /* Other state atoms had better not try to access prog_data, since
232 * there's no GS program.
234 brw
->gs
.base
.prog_data
= NULL
;
239 brw_gs_populate_key(brw
, &key
);
241 if (!brw_search_cache(&brw
->cache
, BRW_CACHE_GS_PROG
,
243 &stage_state
->prog_offset
,
244 &brw
->gs
.base
.prog_data
)) {
245 bool success
= brw_codegen_gs_prog(brw
, current
[MESA_SHADER_GEOMETRY
],
253 brw_gs_precompile(struct gl_context
*ctx
,
254 struct gl_shader_program
*shader_prog
,
255 struct gl_program
*prog
)
257 struct brw_context
*brw
= brw_context(ctx
);
258 struct brw_gs_prog_key key
;
259 uint32_t old_prog_offset
= brw
->gs
.base
.prog_offset
;
260 struct brw_stage_prog_data
*old_prog_data
= brw
->gs
.base
.prog_data
;
263 struct brw_program
*bgp
= brw_program(prog
);
265 memset(&key
, 0, sizeof(key
));
267 brw_setup_tex_for_precompile(brw
, &key
.tex
, prog
);
268 key
.program_string_id
= bgp
->id
;
270 success
= brw_codegen_gs_prog(brw
, shader_prog
, bgp
, &key
);
272 brw
->gs
.base
.prog_offset
= old_prog_offset
;
273 brw
->gs
.base
.prog_data
= old_prog_data
;