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