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