1 /*
2 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 * Jonathan Marek <jonathan@marek.ca>
26 */
27
28 #include "nir/tgsi_to_nir.h"
29 #include "pipe/p_state.h"
30 #include "tgsi/tgsi_dump.h"
31 #include "util/format/u_format.h"
32 #include "util/u_inlines.h"
33 #include "util/u_memory.h"
34 #include "util/u_string.h"
35
36 #include "freedreno_program.h"
37
38 #include "ir2/instr-a2xx.h"
39 #include "fd2_program.h"
40 #include "fd2_texture.h"
41 #include "fd2_util.h"
42 #include "ir2.h"
43
44 static struct fd2_shader_stateobj *
create_shader(struct pipe_context * pctx,gl_shader_stage type)45 create_shader(struct pipe_context *pctx, gl_shader_stage type)
46 {
47 struct fd2_shader_stateobj *so = CALLOC_STRUCT(fd2_shader_stateobj);
48 if (!so)
49 return NULL;
50 so->type = type;
51 so->is_a20x = is_a20x(fd_context(pctx)->screen);
52 return so;
53 }
54
55 static void
delete_shader(struct fd2_shader_stateobj * so)56 delete_shader(struct fd2_shader_stateobj *so)
57 {
58 if (!so)
59 return;
60 ralloc_free(so->nir);
61 for (int i = 0; i < ARRAY_SIZE(so->variant); i++)
62 free(so->variant[i].info.dwords);
63 free(so);
64 }
65
66 static void
emit(struct fd_ringbuffer * ring,gl_shader_stage type,struct ir2_shader_info * info,struct util_dynarray * patches)67 emit(struct fd_ringbuffer *ring, gl_shader_stage type,
68 struct ir2_shader_info *info, struct util_dynarray *patches)
69 {
70 unsigned i;
71
72 assert(info->sizedwords);
73
74 OUT_PKT3(ring, CP_IM_LOAD_IMMEDIATE, 2 + info->sizedwords);
75 OUT_RING(ring, type == MESA_SHADER_FRAGMENT);
76 OUT_RING(ring, info->sizedwords);
77
78 if (patches)
79 util_dynarray_append(patches, uint32_t *,
80 &ring->cur[info->mem_export_ptr]);
81
82 for (i = 0; i < info->sizedwords; i++)
83 OUT_RING(ring, info->dwords[i]);
84 }
85
86 static int
ir2_glsl_type_size(const struct glsl_type * type,bool bindless)87 ir2_glsl_type_size(const struct glsl_type *type, bool bindless)
88 {
89 return glsl_count_attribute_slots(type, false);
90 }
91
92 static void *
fd2_fp_state_create(struct pipe_context * pctx,const struct pipe_shader_state * cso)93 fd2_fp_state_create(struct pipe_context *pctx,
94 const struct pipe_shader_state *cso)
95 {
96 struct fd2_shader_stateobj *so = create_shader(pctx, MESA_SHADER_FRAGMENT);
97 if (!so)
98 return NULL;
99
100 so->nir = (cso->type == PIPE_SHADER_IR_NIR)
101 ? cso->ir.nir
102 : tgsi_to_nir(cso->tokens, pctx->screen, false);
103
104 NIR_PASS_V(so->nir, nir_lower_io, nir_var_shader_in | nir_var_shader_out,
105 ir2_glsl_type_size, (nir_lower_io_options)0);
106
107 if (ir2_optimize_nir(so->nir, true))
108 goto fail;
109
110 so->first_immediate = so->nir->num_uniforms;
111
112 ir2_compile(so, 0, NULL);
113
114 ralloc_free(so->nir);
115 so->nir = NULL;
116 return so;
117
118 fail:
119 delete_shader(so);
120 return NULL;
121 }
122
123 static void
fd2_fp_state_delete(struct pipe_context * pctx,void * hwcso)124 fd2_fp_state_delete(struct pipe_context *pctx, void *hwcso)
125 {
126 struct fd2_shader_stateobj *so = hwcso;
127 delete_shader(so);
128 }
129
130 static void *
fd2_vp_state_create(struct pipe_context * pctx,const struct pipe_shader_state * cso)131 fd2_vp_state_create(struct pipe_context *pctx,
132 const struct pipe_shader_state *cso)
133 {
134 struct fd2_shader_stateobj *so = create_shader(pctx, MESA_SHADER_VERTEX);
135 if (!so)
136 return NULL;
137
138 so->nir = (cso->type == PIPE_SHADER_IR_NIR)
139 ? cso->ir.nir
140 : tgsi_to_nir(cso->tokens, pctx->screen, false);
141
142 NIR_PASS_V(so->nir, nir_lower_io, nir_var_shader_in | nir_var_shader_out,
143 ir2_glsl_type_size, (nir_lower_io_options)0);
144
145 if (ir2_optimize_nir(so->nir, true))
146 goto fail;
147
148 so->first_immediate = so->nir->num_uniforms;
149
150 /* compile binning variant now */
151 ir2_compile(so, 0, NULL);
152
153 return so;
154
155 fail:
156 delete_shader(so);
157 return NULL;
158 }
159
160 static void
fd2_vp_state_delete(struct pipe_context * pctx,void * hwcso)161 fd2_vp_state_delete(struct pipe_context *pctx, void *hwcso)
162 {
163 struct fd2_shader_stateobj *so = hwcso;
164 delete_shader(so);
165 }
166
167 static void
patch_vtx_fetch(struct fd_context * ctx,struct pipe_vertex_element * elem,instr_fetch_vtx_t * instr,uint16_t dst_swiz)168 patch_vtx_fetch(struct fd_context *ctx, struct pipe_vertex_element *elem,
169 instr_fetch_vtx_t *instr, uint16_t dst_swiz) assert_dt
170 {
171 struct surface_format fmt = fd2_pipe2surface(elem->src_format);
172
173 instr->dst_swiz = fd2_vtx_swiz(elem->src_format, dst_swiz);
174 instr->format_comp_all = fmt.sign == SQ_TEX_SIGN_SIGNED;
175 instr->num_format_all = fmt.num_format;
176 instr->format = fmt.format;
177 instr->exp_adjust_all = fmt.exp_adjust;
178 instr->stride = elem->src_stride;
179 instr->offset = elem->src_offset;
180 }
181
182 static void
patch_fetches(struct fd_context * ctx,struct ir2_shader_info * info,struct fd_vertex_stateobj * vtx,struct fd_texture_stateobj * tex)183 patch_fetches(struct fd_context *ctx, struct ir2_shader_info *info,
184 struct fd_vertex_stateobj *vtx,
185 struct fd_texture_stateobj *tex) assert_dt
186 {
187 for (int i = 0; i < info->num_fetch_instrs; i++) {
188 struct ir2_fetch_info *fi = &info->fetch_info[i];
189
190 instr_fetch_t *instr = (instr_fetch_t *)&info->dwords[fi->offset];
191 if (instr->opc == VTX_FETCH) {
192 unsigned idx =
193 (instr->vtx.const_index - 20) * 3 + instr->vtx.const_index_sel;
194 patch_vtx_fetch(ctx, &vtx->pipe[idx], &instr->vtx, fi->vtx.dst_swiz);
195 continue;
196 }
197
198 assert(instr->opc == TEX_FETCH);
199 instr->tex.const_idx = fd2_get_const_idx(ctx, tex, fi->tex.samp_id);
200 instr->tex.src_swiz = fi->tex.src_swiz;
201 }
202 }
203
204 void
fd2_program_emit(struct fd_context * ctx,struct fd_ringbuffer * ring,struct fd_program_stateobj * prog)205 fd2_program_emit(struct fd_context *ctx, struct fd_ringbuffer *ring,
206 struct fd_program_stateobj *prog)
207 {
208 struct fd2_shader_stateobj *fp = NULL, *vp;
209 struct ir2_shader_info *fpi, *vpi;
210 struct ir2_frag_linkage *f;
211 uint8_t vs_gprs, fs_gprs = 0, vs_export = 0;
212 enum a2xx_sq_ps_vtx_mode mode = POSITION_1_VECTOR;
213 bool binning = (ctx->batch && ring == ctx->batch->binning);
214 unsigned variant = 0;
215
216 vp = prog->vs;
217
218 /* find variant matching the linked fragment shader */
219 if (!binning) {
220 fp = prog->fs;
221 for (variant = 1; variant < ARRAY_SIZE(vp->variant); variant++) {
222 /* if checked all variants, compile a new variant */
223 if (!vp->variant[variant].info.sizedwords) {
224 ir2_compile(vp, variant, fp);
225 break;
226 }
227
228 /* check if fragment shader linkage matches */
229 if (!memcmp(&vp->variant[variant].f, &fp->variant[0].f,
230 sizeof(struct ir2_frag_linkage)))
231 break;
232 }
233 assert(variant < ARRAY_SIZE(vp->variant));
234 }
235
236 vpi = &vp->variant[variant].info;
237 fpi = &fp->variant[0].info;
238 f = &fp->variant[0].f;
239
240 /* clear/gmem2mem/mem2gmem need to be changed to remove this condition */
241 if (prog != &ctx->solid_prog && prog != &ctx->blit_prog[0]) {
242 patch_fetches(ctx, vpi, ctx->vtx.vtx, &ctx->tex[PIPE_SHADER_VERTEX]);
243 if (fp)
244 patch_fetches(ctx, fpi, NULL, &ctx->tex[PIPE_SHADER_FRAGMENT]);
245 }
246
247 emit(ring, MESA_SHADER_VERTEX, vpi,
248 binning ? &ctx->batch->shader_patches : NULL);
249
250 if (fp) {
251 emit(ring, MESA_SHADER_FRAGMENT, fpi, NULL);
252 fs_gprs = (fpi->max_reg < 0) ? 0x80 : fpi->max_reg;
253 vs_export = MAX2(1, f->inputs_count) - 1;
254 }
255
256 vs_gprs = (vpi->max_reg < 0) ? 0x80 : vpi->max_reg;
257
258 if (vp->writes_psize && !binning)
259 mode = POSITION_2_VECTORS_SPRITE;
260
261 /* set register to use for param (fragcoord/pointcoord/frontfacing) */
262 OUT_PKT3(ring, CP_SET_CONSTANT, 2);
263 OUT_RING(ring, CP_REG(REG_A2XX_SQ_CONTEXT_MISC));
264 OUT_RING(ring,
265 A2XX_SQ_CONTEXT_MISC_SC_SAMPLE_CNTL(CENTERS_ONLY) |
266 COND(fp, A2XX_SQ_CONTEXT_MISC_PARAM_GEN_POS(f->inputs_count)) |
267 /* we need SCREEN_XY for both fragcoord and frontfacing */
268 A2XX_SQ_CONTEXT_MISC_SC_OUTPUT_SCREEN_XY);
269
270 OUT_PKT3(ring, CP_SET_CONSTANT, 2);
271 OUT_RING(ring, CP_REG(REG_A2XX_SQ_PROGRAM_CNTL));
272 OUT_RING(ring,
273 A2XX_SQ_PROGRAM_CNTL_PS_EXPORT_MODE(2) |
274 A2XX_SQ_PROGRAM_CNTL_VS_EXPORT_MODE(mode) |
275 A2XX_SQ_PROGRAM_CNTL_VS_RESOURCE |
276 A2XX_SQ_PROGRAM_CNTL_PS_RESOURCE |
277 A2XX_SQ_PROGRAM_CNTL_VS_EXPORT_COUNT(vs_export) |
278 A2XX_SQ_PROGRAM_CNTL_PS_REGS(fs_gprs) |
279 A2XX_SQ_PROGRAM_CNTL_VS_REGS(vs_gprs) |
280 COND(fp && fp->need_param, A2XX_SQ_PROGRAM_CNTL_PARAM_GEN) |
281 COND(!fp, A2XX_SQ_PROGRAM_CNTL_GEN_INDEX_VTX));
282 }
283
284 void
fd2_prog_init(struct pipe_context * pctx)285 fd2_prog_init(struct pipe_context *pctx)
286 {
287 struct fd_context *ctx = fd_context(pctx);
288 struct fd_program_stateobj *prog;
289 struct fd2_shader_stateobj *so;
290 struct ir2_shader_info *info;
291 instr_fetch_vtx_t *instr;
292
293 pctx->create_fs_state = fd2_fp_state_create;
294 pctx->delete_fs_state = fd2_fp_state_delete;
295
296 pctx->create_vs_state = fd2_vp_state_create;
297 pctx->delete_vs_state = fd2_vp_state_delete;
298
299 fd_prog_init(pctx);
300
301 /* XXX maybe its possible to reuse patch_vtx_fetch somehow? */
302
303 prog = &ctx->solid_prog;
304 so = prog->vs;
305 ir2_compile(prog->vs, 1, prog->fs);
306
307 #define IR2_FETCH_SWIZ_XY01 0xb08
308 #define IR2_FETCH_SWIZ_XYZ1 0xa88
309
310 info = &so->variant[1].info;
311
312 instr = (instr_fetch_vtx_t *)&info->dwords[info->fetch_info[0].offset];
313 instr->const_index = 26;
314 instr->const_index_sel = 0;
315 instr->format = FMT_32_32_32_FLOAT;
316 instr->format_comp_all = false;
317 instr->stride = 12;
318 instr->num_format_all = true;
319 instr->dst_swiz = IR2_FETCH_SWIZ_XYZ1;
320
321 prog = &ctx->blit_prog[0];
322 so = prog->vs;
323 ir2_compile(prog->vs, 1, prog->fs);
324
325 info = &so->variant[1].info;
326
327 instr = (instr_fetch_vtx_t *)&info->dwords[info->fetch_info[0].offset];
328 instr->const_index = 26;
329 instr->const_index_sel = 1;
330 instr->format = FMT_32_32_FLOAT;
331 instr->format_comp_all = false;
332 instr->stride = 8;
333 instr->num_format_all = false;
334 instr->dst_swiz = IR2_FETCH_SWIZ_XY01;
335
336 instr = (instr_fetch_vtx_t *)&info->dwords[info->fetch_info[1].offset];
337 instr->const_index = 26;
338 instr->const_index_sel = 0;
339 instr->format = FMT_32_32_32_FLOAT;
340 instr->format_comp_all = false;
341 instr->stride = 12;
342 instr->num_format_all = false;
343 instr->dst_swiz = IR2_FETCH_SWIZ_XYZ1;
344 }
345