i965: Push down inclusion of brw_program.h.
[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
38 static void
39 assign_gs_binding_table_offsets(const struct brw_device_info *devinfo,
40 const struct gl_shader_program *shader_prog,
41 const struct gl_program *prog,
42 struct brw_gs_prog_data *prog_data)
43 {
44 /* In gen6 we reserve the first BRW_MAX_SOL_BINDINGS entries for transform
45 * feedback surfaces.
46 */
47 uint32_t reserved = devinfo->gen == 6 ? BRW_MAX_SOL_BINDINGS : 0;
48
49 brw_assign_common_binding_table_offsets(MESA_SHADER_GEOMETRY, devinfo,
50 shader_prog, prog,
51 &prog_data->base.base,
52 reserved);
53 }
54
55 bool
56 brw_codegen_gs_prog(struct brw_context *brw,
57 struct gl_shader_program *prog,
58 struct brw_geometry_program *gp,
59 struct brw_gs_prog_key *key)
60 {
61 struct brw_compiler *compiler = brw->intelScreen->compiler;
62 struct gl_shader *shader = prog->_LinkedShaders[MESA_SHADER_GEOMETRY];
63 struct brw_stage_state *stage_state = &brw->gs.base;
64 struct brw_gs_prog_data prog_data;
65 memset(&prog_data, 0, sizeof(prog_data));
66
67 assign_gs_binding_table_offsets(brw->intelScreen->devinfo, prog,
68 &gp->program.Base, &prog_data);
69
70 /* Allocate the references to the uniforms that will end up in the
71 * prog_data associated with the compiled program, and which will be freed
72 * by the state cache.
73 *
74 * Note: param_count needs to be num_uniform_components * 4, since we add
75 * padding around uniform values below vec4 size, so the worst case is that
76 * every uniform is a float which gets padded to the size of a vec4.
77 */
78 struct gl_shader *gs = prog->_LinkedShaders[MESA_SHADER_GEOMETRY];
79 int param_count = gp->program.Base.nir->num_uniforms;
80 if (!compiler->scalar_stage[MESA_SHADER_GEOMETRY])
81 param_count *= 4;
82
83 prog_data.base.base.param =
84 rzalloc_array(NULL, const gl_constant_value *, param_count);
85 prog_data.base.base.pull_param =
86 rzalloc_array(NULL, const gl_constant_value *, param_count);
87 prog_data.base.base.image_param =
88 rzalloc_array(NULL, struct brw_image_param, gs->NumImages);
89 prog_data.base.base.nr_params = param_count;
90 prog_data.base.base.nr_image_params = gs->NumImages;
91
92 brw_nir_setup_glsl_uniforms(gp->program.Base.nir, prog, &gp->program.Base,
93 &prog_data.base.base,
94 compiler->scalar_stage[MESA_SHADER_GEOMETRY]);
95
96 GLbitfield64 outputs_written = gp->program.Base.OutputsWritten;
97
98 brw_compute_vue_map(brw->intelScreen->devinfo,
99 &prog_data.base.vue_map, outputs_written,
100 prog ? prog->SeparateShader : false);
101
102 if (unlikely(INTEL_DEBUG & DEBUG_GS))
103 brw_dump_ir("geometry", prog, gs, NULL);
104
105 int st_index = -1;
106 if (INTEL_DEBUG & DEBUG_SHADER_TIME)
107 st_index = brw_get_shader_time_index(brw, prog, NULL, ST_GS);
108
109 void *mem_ctx = ralloc_context(NULL);
110 unsigned program_size;
111 char *error_str;
112 const unsigned *program =
113 brw_compile_gs(brw->intelScreen->compiler, brw, mem_ctx, key,
114 &prog_data, shader->Program->nir, prog,
115 st_index, &program_size, &error_str);
116 if (program == NULL) {
117 ralloc_free(mem_ctx);
118 return false;
119 }
120
121 /* Scratch space is used for register spilling */
122 if (prog_data.base.base.total_scratch) {
123 brw_get_scratch_bo(brw, &stage_state->scratch_bo,
124 prog_data.base.base.total_scratch *
125 brw->max_gs_threads);
126 }
127
128 brw_upload_cache(&brw->cache, BRW_CACHE_GS_PROG,
129 key, sizeof(*key),
130 program, program_size,
131 &prog_data, sizeof(prog_data),
132 &stage_state->prog_offset, &brw->gs.prog_data);
133 ralloc_free(mem_ctx);
134
135 return true;
136 }
137
138 static bool
139 brw_gs_state_dirty(struct brw_context *brw)
140 {
141 return brw_state_dirty(brw,
142 _NEW_TEXTURE,
143 BRW_NEW_GEOMETRY_PROGRAM |
144 BRW_NEW_TRANSFORM_FEEDBACK);
145 }
146
147 static void
148 brw_gs_populate_key(struct brw_context *brw,
149 struct brw_gs_prog_key *key)
150 {
151 struct gl_context *ctx = &brw->ctx;
152 struct brw_stage_state *stage_state = &brw->gs.base;
153 struct brw_geometry_program *gp =
154 (struct brw_geometry_program *) brw->geometry_program;
155 struct gl_program *prog = &gp->program.Base;
156
157 memset(key, 0, sizeof(*key));
158
159 key->program_string_id = gp->id;
160
161 /* _NEW_TEXTURE */
162 brw_populate_sampler_prog_key_data(ctx, prog, stage_state->sampler_count,
163 &key->tex);
164 }
165
166 void
167 brw_upload_gs_prog(struct brw_context *brw)
168 {
169 struct gl_context *ctx = &brw->ctx;
170 struct gl_shader_program **current = ctx->_Shader->CurrentProgram;
171 struct brw_stage_state *stage_state = &brw->gs.base;
172 struct brw_gs_prog_key key;
173 /* BRW_NEW_GEOMETRY_PROGRAM */
174 struct brw_geometry_program *gp =
175 (struct brw_geometry_program *) brw->geometry_program;
176
177 if (!brw_gs_state_dirty(brw))
178 return;
179
180 if (gp == NULL) {
181 /* No geometry shader. Vertex data just passes straight through. */
182 if (brw->gen == 6 &&
183 (brw->ctx.NewDriverState & BRW_NEW_TRANSFORM_FEEDBACK)) {
184 gen6_brw_upload_ff_gs_prog(brw);
185 return;
186 }
187
188 /* Other state atoms had better not try to access prog_data, since
189 * there's no GS program.
190 */
191 brw->gs.prog_data = NULL;
192 brw->gs.base.prog_data = NULL;
193
194 return;
195 }
196
197 brw_gs_populate_key(brw, &key);
198
199 if (!brw_search_cache(&brw->cache, BRW_CACHE_GS_PROG,
200 &key, sizeof(key),
201 &stage_state->prog_offset, &brw->gs.prog_data)) {
202 bool success = brw_codegen_gs_prog(brw, current[MESA_SHADER_GEOMETRY],
203 gp, &key);
204 assert(success);
205 (void)success;
206 }
207 brw->gs.base.prog_data = &brw->gs.prog_data->base.base;
208 }
209
210 bool
211 brw_gs_precompile(struct gl_context *ctx,
212 struct gl_shader_program *shader_prog,
213 struct gl_program *prog)
214 {
215 struct brw_context *brw = brw_context(ctx);
216 struct brw_gs_prog_key key;
217 uint32_t old_prog_offset = brw->gs.base.prog_offset;
218 struct brw_gs_prog_data *old_prog_data = brw->gs.prog_data;
219 bool success;
220
221 struct gl_geometry_program *gp = (struct gl_geometry_program *) prog;
222 struct brw_geometry_program *bgp = brw_geometry_program(gp);
223
224 memset(&key, 0, sizeof(key));
225
226 brw_setup_tex_for_precompile(brw, &key.tex, prog);
227 key.program_string_id = bgp->id;
228
229 success = brw_codegen_gs_prog(brw, shader_prog, bgp, &key);
230
231 brw->gs.base.prog_offset = old_prog_offset;
232 brw->gs.prog_data = old_prog_data;
233
234 return success;
235 }