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
,
41 struct gl_shader_program
*shader_prog
,
42 const struct brw_gs_prog_key
*key
)
44 struct brw_cache_item
*c
= NULL
;
45 const struct brw_gs_prog_key
*old_key
= NULL
;
48 perf_debug("Recompiling geometry shader for program %d\n",
51 for (unsigned int i
= 0; i
< brw
->cache
.size
; i
++) {
52 for (c
= brw
->cache
.items
[i
]; c
; c
= c
->next
) {
53 if (c
->cache_id
== BRW_CACHE_GS_PROG
) {
56 if (old_key
->program_string_id
== key
->program_string_id
)
65 perf_debug(" Didn't find previous compile in the shader cache for "
70 found
|= brw_debug_recompile_sampler_key(brw
, &old_key
->tex
, &key
->tex
);
73 perf_debug(" Something else\n");
78 assign_gs_binding_table_offsets(const struct brw_device_info
*devinfo
,
79 const struct gl_shader_program
*shader_prog
,
80 const struct gl_program
*prog
,
81 struct brw_gs_prog_data
*prog_data
)
83 /* In gen6 we reserve the first BRW_MAX_SOL_BINDINGS entries for transform
86 uint32_t reserved
= devinfo
->gen
== 6 ? BRW_MAX_SOL_BINDINGS
: 0;
88 brw_assign_common_binding_table_offsets(MESA_SHADER_GEOMETRY
, devinfo
,
90 &prog_data
->base
.base
,
95 brw_codegen_gs_prog(struct brw_context
*brw
,
96 struct gl_shader_program
*prog
,
97 struct brw_geometry_program
*gp
,
98 struct brw_gs_prog_key
*key
)
100 struct brw_compiler
*compiler
= brw
->intelScreen
->compiler
;
101 struct gl_shader
*shader
= prog
->_LinkedShaders
[MESA_SHADER_GEOMETRY
];
102 struct brw_stage_state
*stage_state
= &brw
->gs
.base
;
103 struct brw_gs_prog_data prog_data
;
104 bool start_busy
= false;
105 double start_time
= 0;
107 memset(&prog_data
, 0, sizeof(prog_data
));
109 assign_gs_binding_table_offsets(brw
->intelScreen
->devinfo
, prog
,
110 &gp
->program
.Base
, &prog_data
);
112 /* Allocate the references to the uniforms that will end up in the
113 * prog_data associated with the compiled program, and which will be freed
114 * by the state cache.
116 * Note: param_count needs to be num_uniform_components * 4, since we add
117 * padding around uniform values below vec4 size, so the worst case is that
118 * every uniform is a float which gets padded to the size of a vec4.
120 struct gl_shader
*gs
= prog
->_LinkedShaders
[MESA_SHADER_GEOMETRY
];
121 struct brw_shader
*bgs
= (struct brw_shader
*) gs
;
122 int param_count
= gp
->program
.Base
.nir
->num_uniforms
;
123 if (!compiler
->scalar_stage
[MESA_SHADER_GEOMETRY
])
126 prog_data
.base
.base
.param
=
127 rzalloc_array(NULL
, const gl_constant_value
*, param_count
);
128 prog_data
.base
.base
.pull_param
=
129 rzalloc_array(NULL
, const gl_constant_value
*, param_count
);
130 prog_data
.base
.base
.image_param
=
131 rzalloc_array(NULL
, struct brw_image_param
, gs
->NumImages
);
132 prog_data
.base
.base
.nr_params
= param_count
;
133 prog_data
.base
.base
.nr_image_params
= gs
->NumImages
;
135 brw_nir_setup_glsl_uniforms(gp
->program
.Base
.nir
, prog
, &gp
->program
.Base
,
136 &prog_data
.base
.base
,
137 compiler
->scalar_stage
[MESA_SHADER_GEOMETRY
]);
139 GLbitfield64 outputs_written
= gp
->program
.Base
.OutputsWritten
;
141 prog_data
.base
.cull_distance_mask
=
142 ((1 << gp
->program
.Base
.CullDistanceArraySize
) - 1) <<
143 gp
->program
.Base
.ClipDistanceArraySize
;
145 brw_compute_vue_map(brw
->intelScreen
->devinfo
,
146 &prog_data
.base
.vue_map
, outputs_written
,
147 prog
? prog
->SeparateShader
: false);
149 if (unlikely(INTEL_DEBUG
& DEBUG_GS
))
150 brw_dump_ir("geometry", prog
, gs
, NULL
);
153 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
)
154 st_index
= brw_get_shader_time_index(brw
, prog
, NULL
, ST_GS
);
156 if (unlikely(brw
->perf_debug
)) {
157 start_busy
= brw
->batch
.last_bo
&& drm_intel_bo_busy(brw
->batch
.last_bo
);
158 start_time
= get_time();
161 void *mem_ctx
= ralloc_context(NULL
);
162 unsigned program_size
;
164 const unsigned *program
=
165 brw_compile_gs(brw
->intelScreen
->compiler
, brw
, mem_ctx
, key
,
166 &prog_data
, shader
->Program
->nir
, prog
,
167 st_index
, &program_size
, &error_str
);
168 if (program
== NULL
) {
169 ralloc_free(mem_ctx
);
173 if (unlikely(brw
->perf_debug
)) {
174 if (bgs
->compiled_once
) {
175 brw_gs_debug_recompile(brw
, prog
, key
);
177 if (start_busy
&& !drm_intel_bo_busy(brw
->batch
.last_bo
)) {
178 perf_debug("GS compile took %.03f ms and stalled the GPU\n",
179 (get_time() - start_time
) * 1000);
181 bgs
->compiled_once
= true;
184 /* Scratch space is used for register spilling */
185 if (prog_data
.base
.base
.total_scratch
) {
186 brw_get_scratch_bo(brw
, &stage_state
->scratch_bo
,
187 prog_data
.base
.base
.total_scratch
*
188 brw
->max_gs_threads
);
191 brw_upload_cache(&brw
->cache
, BRW_CACHE_GS_PROG
,
193 program
, program_size
,
194 &prog_data
, sizeof(prog_data
),
195 &stage_state
->prog_offset
, &brw
->gs
.prog_data
);
196 ralloc_free(mem_ctx
);
202 brw_gs_state_dirty(const struct brw_context
*brw
)
204 return brw_state_dirty(brw
,
206 BRW_NEW_GEOMETRY_PROGRAM
|
207 BRW_NEW_TRANSFORM_FEEDBACK
);
211 brw_gs_populate_key(struct brw_context
*brw
,
212 struct brw_gs_prog_key
*key
)
214 struct gl_context
*ctx
= &brw
->ctx
;
215 struct brw_stage_state
*stage_state
= &brw
->gs
.base
;
216 struct brw_geometry_program
*gp
=
217 (struct brw_geometry_program
*) brw
->geometry_program
;
218 struct gl_program
*prog
= &gp
->program
.Base
;
220 memset(key
, 0, sizeof(*key
));
222 key
->program_string_id
= gp
->id
;
225 brw_populate_sampler_prog_key_data(ctx
, prog
, stage_state
->sampler_count
,
230 brw_upload_gs_prog(struct brw_context
*brw
)
232 struct gl_context
*ctx
= &brw
->ctx
;
233 struct gl_shader_program
**current
= ctx
->_Shader
->CurrentProgram
;
234 struct brw_stage_state
*stage_state
= &brw
->gs
.base
;
235 struct brw_gs_prog_key key
;
236 /* BRW_NEW_GEOMETRY_PROGRAM */
237 struct brw_geometry_program
*gp
=
238 (struct brw_geometry_program
*) brw
->geometry_program
;
240 if (!brw_gs_state_dirty(brw
))
244 /* No geometry shader. Vertex data just passes straight through. */
246 (brw
->ctx
.NewDriverState
& BRW_NEW_TRANSFORM_FEEDBACK
)) {
247 gen6_brw_upload_ff_gs_prog(brw
);
251 /* Other state atoms had better not try to access prog_data, since
252 * there's no GS program.
254 brw
->gs
.prog_data
= NULL
;
255 brw
->gs
.base
.prog_data
= NULL
;
260 brw_gs_populate_key(brw
, &key
);
262 if (!brw_search_cache(&brw
->cache
, BRW_CACHE_GS_PROG
,
264 &stage_state
->prog_offset
, &brw
->gs
.prog_data
)) {
265 bool success
= brw_codegen_gs_prog(brw
, current
[MESA_SHADER_GEOMETRY
],
270 brw
->gs
.base
.prog_data
= &brw
->gs
.prog_data
->base
.base
;
274 brw_gs_precompile(struct gl_context
*ctx
,
275 struct gl_shader_program
*shader_prog
,
276 struct gl_program
*prog
)
278 struct brw_context
*brw
= brw_context(ctx
);
279 struct brw_gs_prog_key key
;
280 uint32_t old_prog_offset
= brw
->gs
.base
.prog_offset
;
281 struct brw_gs_prog_data
*old_prog_data
= brw
->gs
.prog_data
;
284 struct gl_geometry_program
*gp
= (struct gl_geometry_program
*) prog
;
285 struct brw_geometry_program
*bgp
= brw_geometry_program(gp
);
287 memset(&key
, 0, sizeof(key
));
289 brw_setup_tex_for_precompile(brw
, &key
.tex
, prog
);
290 key
.program_string_id
= bgp
->id
;
292 success
= brw_codegen_gs_prog(brw
, shader_prog
, bgp
, &key
);
294 brw
->gs
.base
.prog_offset
= old_prog_offset
;
295 brw
->gs
.prog_data
= old_prog_data
;