1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Authors:
26 * Rob Clark <robclark@freedesktop.org>
27 */
28
29 #include "pipe/p_state.h"
30 #include "util/u_string.h"
31 #include "util/u_memory.h"
32 #include "util/u_inlines.h"
33 #include "util/u_format.h"
34 #include "tgsi/tgsi_dump.h"
35 #include "tgsi/tgsi_parse.h"
36
37 #include "freedreno_program.h"
38
39 #include "fd2_program.h"
40 #include "fd2_compiler.h"
41 #include "fd2_texture.h"
42 #include "fd2_util.h"
43
44 static struct fd2_shader_stateobj *
create_shader(enum shader_t type)45 create_shader(enum shader_t type)
46 {
47 struct fd2_shader_stateobj *so = CALLOC_STRUCT(fd2_shader_stateobj);
48 if (!so)
49 return NULL;
50 so->type = type;
51 return so;
52 }
53
54 static void
delete_shader(struct fd2_shader_stateobj * so)55 delete_shader(struct fd2_shader_stateobj *so)
56 {
57 ir2_shader_destroy(so->ir);
58 free(so->tokens);
59 free(so->bin);
60 free(so);
61 }
62
63 static struct fd2_shader_stateobj *
assemble(struct fd2_shader_stateobj * so)64 assemble(struct fd2_shader_stateobj *so)
65 {
66 free(so->bin);
67 so->bin = ir2_shader_assemble(so->ir, &so->info);
68 if (!so->bin)
69 goto fail;
70
71 if (fd_mesa_debug & FD_DBG_DISASM) {
72 DBG("disassemble: type=%d", so->type);
73 disasm_a2xx(so->bin, so->info.sizedwords, 0, so->type);
74 }
75
76 return so;
77
78 fail:
79 debug_error("assemble failed!");
80 delete_shader(so);
81 return NULL;
82 }
83
84 static struct fd2_shader_stateobj *
compile(struct fd_program_stateobj * prog,struct fd2_shader_stateobj * so)85 compile(struct fd_program_stateobj *prog, struct fd2_shader_stateobj *so)
86 {
87 int ret;
88
89 if (fd_mesa_debug & FD_DBG_DISASM) {
90 DBG("dump tgsi: type=%d", so->type);
91 tgsi_dump(so->tokens, 0);
92 }
93
94 ret = fd2_compile_shader(prog, so);
95 if (ret)
96 goto fail;
97
98 /* NOTE: we don't assemble yet because for VS we don't know the
99 * type information for vertex fetch yet.. so those need to be
100 * patched up later before assembling.
101 */
102
103 so->info.sizedwords = 0;
104
105 return so;
106
107 fail:
108 debug_error("compile failed!");
109 delete_shader(so);
110 return NULL;
111 }
112
113 static void
emit(struct fd_ringbuffer * ring,struct fd2_shader_stateobj * so)114 emit(struct fd_ringbuffer *ring, struct fd2_shader_stateobj *so)
115 {
116 unsigned i;
117
118 if (so->info.sizedwords == 0)
119 assemble(so);
120
121 OUT_PKT3(ring, CP_IM_LOAD_IMMEDIATE, 2 + so->info.sizedwords);
122 OUT_RING(ring, (so->type == SHADER_VERTEX) ? 0 : 1);
123 OUT_RING(ring, so->info.sizedwords);
124 for (i = 0; i < so->info.sizedwords; i++)
125 OUT_RING(ring, so->bin[i]);
126 }
127
128 static void *
fd2_fp_state_create(struct pipe_context * pctx,const struct pipe_shader_state * cso)129 fd2_fp_state_create(struct pipe_context *pctx,
130 const struct pipe_shader_state *cso)
131 {
132 struct fd2_shader_stateobj *so = create_shader(SHADER_FRAGMENT);
133 if (!so)
134 return NULL;
135 so->tokens = tgsi_dup_tokens(cso->tokens);
136 return so;
137 }
138
139 static void
fd2_fp_state_delete(struct pipe_context * pctx,void * hwcso)140 fd2_fp_state_delete(struct pipe_context *pctx, void *hwcso)
141 {
142 struct fd2_shader_stateobj *so = hwcso;
143 delete_shader(so);
144 }
145
146 static void *
fd2_vp_state_create(struct pipe_context * pctx,const struct pipe_shader_state * cso)147 fd2_vp_state_create(struct pipe_context *pctx,
148 const struct pipe_shader_state *cso)
149 {
150 struct fd2_shader_stateobj *so = create_shader(SHADER_VERTEX);
151 if (!so)
152 return NULL;
153 so->tokens = tgsi_dup_tokens(cso->tokens);
154 return so;
155 }
156
157 static void
fd2_vp_state_delete(struct pipe_context * pctx,void * hwcso)158 fd2_vp_state_delete(struct pipe_context *pctx, void *hwcso)
159 {
160 struct fd2_shader_stateobj *so = hwcso;
161 delete_shader(so);
162 }
163
164 static void
patch_vtx_fetches(struct fd_context * ctx,struct fd2_shader_stateobj * so,struct fd_vertex_stateobj * vtx)165 patch_vtx_fetches(struct fd_context *ctx, struct fd2_shader_stateobj *so,
166 struct fd_vertex_stateobj *vtx)
167 {
168 unsigned i;
169
170 assert(so->num_vfetch_instrs == vtx->num_elements);
171
172 /* update vtx fetch instructions: */
173 for (i = 0; i < so->num_vfetch_instrs; i++) {
174 struct ir2_instruction *instr = so->vfetch_instrs[i];
175 struct pipe_vertex_element *elem = &vtx->pipe[i];
176 struct pipe_vertex_buffer *vb =
177 &ctx->vtx.vertexbuf.vb[elem->vertex_buffer_index];
178 enum pipe_format format = elem->src_format;
179 const struct util_format_description *desc =
180 util_format_description(format);
181 unsigned j;
182
183 /* Find the first non-VOID channel. */
184 for (j = 0; j < 4; j++)
185 if (desc->channel[j].type != UTIL_FORMAT_TYPE_VOID)
186 break;
187
188 /* CI/CIS can probably be set in compiler instead: */
189 instr->fetch.const_idx = 20 + (i / 3);
190 instr->fetch.const_idx_sel = i % 3;
191
192 instr->fetch.fmt = fd2_pipe2surface(format);
193 instr->fetch.is_normalized = desc->channel[j].normalized;
194 instr->fetch.is_signed =
195 desc->channel[j].type == UTIL_FORMAT_TYPE_SIGNED;
196 instr->fetch.stride = vb->stride ? : 1;
197 instr->fetch.offset = elem->src_offset;
198
199 for (j = 0; j < 4; j++)
200 instr->regs[0]->swizzle[j] = "xyzw01__"[desc->swizzle[j]];
201
202 assert(instr->fetch.fmt != ~0);
203
204 DBG("vtx[%d]: %s (%d), ci=%d, cis=%d, id=%d, swizzle=%s, "
205 "stride=%d, offset=%d",
206 i, util_format_name(format),
207 instr->fetch.fmt,
208 instr->fetch.const_idx,
209 instr->fetch.const_idx_sel,
210 elem->instance_divisor,
211 instr->regs[0]->swizzle,
212 instr->fetch.stride,
213 instr->fetch.offset);
214 }
215
216 /* trigger re-assemble: */
217 so->info.sizedwords = 0;
218 }
219
220 static void
patch_tex_fetches(struct fd_context * ctx,struct fd2_shader_stateobj * so,struct fd_texture_stateobj * tex)221 patch_tex_fetches(struct fd_context *ctx, struct fd2_shader_stateobj *so,
222 struct fd_texture_stateobj *tex)
223 {
224 unsigned i;
225
226 /* update tex fetch instructions: */
227 for (i = 0; i < so->num_tfetch_instrs; i++) {
228 struct ir2_instruction *instr = so->tfetch_instrs[i].instr;
229 unsigned samp_id = so->tfetch_instrs[i].samp_id;
230 unsigned const_idx = fd2_get_const_idx(ctx, tex, samp_id);
231
232 if (const_idx != instr->fetch.const_idx) {
233 instr->fetch.const_idx = const_idx;
234 /* trigger re-assemble: */
235 so->info.sizedwords = 0;
236 }
237 }
238 }
239
240 void
fd2_program_validate(struct fd_context * ctx)241 fd2_program_validate(struct fd_context *ctx)
242 {
243 struct fd_program_stateobj *prog = &ctx->prog;
244 bool dirty_fp = !!(ctx->dirty_shader[PIPE_SHADER_FRAGMENT] & FD_DIRTY_SHADER_PROG);
245 bool dirty_vp = !!(ctx->dirty_shader[PIPE_SHADER_VERTEX] & FD_DIRTY_SHADER_PROG);
246
247 /* if vertex or frag shader is dirty, we may need to recompile. Compile
248 * frag shader first, as that assigns the register slots for exports
249 * from the vertex shader. And therefore if frag shader has changed we
250 * need to recompile both vert and frag shader.
251 */
252 if (dirty_fp)
253 compile(prog, prog->fp);
254
255 if (dirty_fp || dirty_vp)
256 compile(prog, prog->vp);
257
258 /* if necessary, fix up vertex fetch instructions: */
259 if (ctx->dirty & (FD_DIRTY_VTXSTATE | FD_DIRTY_PROG))
260 patch_vtx_fetches(ctx, prog->vp, ctx->vtx.vtx);
261
262 /* if necessary, fix up texture fetch instructions: */
263 if (ctx->dirty & (FD_DIRTY_TEXSTATE | FD_DIRTY_PROG)) {
264 patch_tex_fetches(ctx, prog->vp, &ctx->tex[PIPE_SHADER_VERTEX]);
265 patch_tex_fetches(ctx, prog->fp, &ctx->tex[PIPE_SHADER_FRAGMENT]);
266 }
267 }
268
269 void
fd2_program_emit(struct fd_ringbuffer * ring,struct fd_program_stateobj * prog)270 fd2_program_emit(struct fd_ringbuffer *ring,
271 struct fd_program_stateobj *prog)
272 {
273 struct ir2_shader_info *vsi =
274 &((struct fd2_shader_stateobj *)prog->vp)->info;
275 struct ir2_shader_info *fsi =
276 &((struct fd2_shader_stateobj *)prog->fp)->info;
277 uint8_t vs_gprs, fs_gprs, vs_export;
278
279 emit(ring, prog->vp);
280 emit(ring, prog->fp);
281
282 vs_gprs = (vsi->max_reg < 0) ? 0x80 : vsi->max_reg;
283 fs_gprs = (fsi->max_reg < 0) ? 0x80 : fsi->max_reg;
284 vs_export = MAX2(1, prog->num_exports) - 1;
285
286 OUT_PKT3(ring, CP_SET_CONSTANT, 2);
287 OUT_RING(ring, CP_REG(REG_A2XX_SQ_PROGRAM_CNTL));
288 OUT_RING(ring, A2XX_SQ_PROGRAM_CNTL_PS_EXPORT_MODE(POSITION_2_VECTORS_SPRITE) |
289 A2XX_SQ_PROGRAM_CNTL_VS_RESOURCE |
290 A2XX_SQ_PROGRAM_CNTL_PS_RESOURCE |
291 A2XX_SQ_PROGRAM_CNTL_VS_EXPORT_COUNT(vs_export) |
292 A2XX_SQ_PROGRAM_CNTL_PS_REGS(fs_gprs) |
293 A2XX_SQ_PROGRAM_CNTL_VS_REGS(vs_gprs));
294 }
295
296 /* Creates shader:
297 * EXEC ADDR(0x2) CNT(0x1)
298 * (S)FETCH: SAMPLE R0.xyzw = R0.xyx CONST(0) LOCATION(CENTER)
299 * ALLOC PARAM/PIXEL SIZE(0x0)
300 * EXEC_END ADDR(0x3) CNT(0x1)
301 * ALU: MAXv export0 = R0, R0 ; gl_FragColor
302 * NOP
303 */
304 static struct fd2_shader_stateobj *
create_blit_fp(void)305 create_blit_fp(void)
306 {
307 struct fd2_shader_stateobj *so = create_shader(SHADER_FRAGMENT);
308 struct ir2_cf *cf;
309 struct ir2_instruction *instr;
310
311 if (!so)
312 return NULL;
313
314 so->ir = ir2_shader_create();
315
316 cf = ir2_cf_create(so->ir, EXEC);
317
318 instr = ir2_instr_create_tex_fetch(cf, 0);
319 ir2_reg_create(instr, 0, "xyzw", 0);
320 ir2_reg_create(instr, 0, "xyx", 0);
321 instr->sync = true;
322
323 cf = ir2_cf_create_alloc(so->ir, SQ_PARAMETER_PIXEL, 0);
324 cf = ir2_cf_create(so->ir, EXEC_END);
325
326 instr = ir2_instr_create_alu(cf, MAXv, ~0);
327 ir2_reg_create(instr, 0, NULL, IR2_REG_EXPORT);
328 ir2_reg_create(instr, 0, NULL, 0);
329 ir2_reg_create(instr, 0, NULL, 0);
330
331 return assemble(so);
332 }
333
334 /* Creates shader:
335 * EXEC ADDR(0x3) CNT(0x2)
336 * FETCH: VERTEX R1.xy01 = R0.x FMT_32_32_FLOAT UNSIGNED STRIDE(8) CONST(26, 1)
337 * FETCH: VERTEX R2.xyz1 = R0.x FMT_32_32_32_FLOAT UNSIGNED STRIDE(12) CONST(26, 0)
338 * ALLOC POSITION SIZE(0x0)
339 * EXEC ADDR(0x5) CNT(0x1)
340 * ALU: MAXv export62 = R2, R2 ; gl_Position
341 * ALLOC PARAM/PIXEL SIZE(0x0)
342 * EXEC_END ADDR(0x6) CNT(0x1)
343 * ALU: MAXv export0 = R1, R1
344 * NOP
345 */
346 static struct fd2_shader_stateobj *
create_blit_vp(void)347 create_blit_vp(void)
348 {
349 struct fd2_shader_stateobj *so = create_shader(SHADER_VERTEX);
350 struct ir2_cf *cf;
351 struct ir2_instruction *instr;
352
353 if (!so)
354 return NULL;
355
356 so->ir = ir2_shader_create();
357
358 cf = ir2_cf_create(so->ir, EXEC);
359
360 instr = ir2_instr_create_vtx_fetch(cf, 26, 1, FMT_32_32_FLOAT, false, 8);
361 instr->fetch.is_normalized = true;
362 ir2_reg_create(instr, 1, "xy01", 0);
363 ir2_reg_create(instr, 0, "x", 0);
364
365 instr = ir2_instr_create_vtx_fetch(cf, 26, 0, FMT_32_32_32_FLOAT, false, 12);
366 instr->fetch.is_normalized = true;
367 ir2_reg_create(instr, 2, "xyz1", 0);
368 ir2_reg_create(instr, 0, "x", 0);
369
370 cf = ir2_cf_create_alloc(so->ir, SQ_POSITION, 0);
371 cf = ir2_cf_create(so->ir, EXEC);
372
373 instr = ir2_instr_create_alu(cf, MAXv, ~0);
374 ir2_reg_create(instr, 62, NULL, IR2_REG_EXPORT);
375 ir2_reg_create(instr, 2, NULL, 0);
376 ir2_reg_create(instr, 2, NULL, 0);
377
378 cf = ir2_cf_create_alloc(so->ir, SQ_PARAMETER_PIXEL, 0);
379 cf = ir2_cf_create(so->ir, EXEC_END);
380
381 instr = ir2_instr_create_alu(cf, MAXv, ~0);
382 ir2_reg_create(instr, 0, NULL, IR2_REG_EXPORT);
383 ir2_reg_create(instr, 1, NULL, 0);
384 ir2_reg_create(instr, 1, NULL, 0);
385
386 return assemble(so);
387 }
388
389 /* Creates shader:
390 * ALLOC PARAM/PIXEL SIZE(0x0)
391 * EXEC_END ADDR(0x1) CNT(0x1)
392 * ALU: MAXv export0 = C0, C0 ; gl_FragColor
393 */
394 static struct fd2_shader_stateobj *
create_solid_fp(void)395 create_solid_fp(void)
396 {
397 struct fd2_shader_stateobj *so = create_shader(SHADER_FRAGMENT);
398 struct ir2_cf *cf;
399 struct ir2_instruction *instr;
400
401 if (!so)
402 return NULL;
403
404 so->ir = ir2_shader_create();
405
406 cf = ir2_cf_create_alloc(so->ir, SQ_PARAMETER_PIXEL, 0);
407 cf = ir2_cf_create(so->ir, EXEC_END);
408
409 instr = ir2_instr_create_alu(cf, MAXv, ~0);
410 ir2_reg_create(instr, 0, NULL, IR2_REG_EXPORT);
411 ir2_reg_create(instr, 0, NULL, IR2_REG_CONST);
412 ir2_reg_create(instr, 0, NULL, IR2_REG_CONST);
413
414 return assemble(so);
415 }
416
417 /* Creates shader:
418 * EXEC ADDR(0x3) CNT(0x1)
419 * (S)FETCH: VERTEX R1.xyz1 = R0.x FMT_32_32_32_FLOAT
420 * UNSIGNED STRIDE(12) CONST(26, 0)
421 * ALLOC POSITION SIZE(0x0)
422 * EXEC ADDR(0x4) CNT(0x1)
423 * ALU: MAXv export62 = R1, R1 ; gl_Position
424 * ALLOC PARAM/PIXEL SIZE(0x0)
425 * EXEC_END ADDR(0x5) CNT(0x0)
426 */
427 static struct fd2_shader_stateobj *
create_solid_vp(void)428 create_solid_vp(void)
429 {
430 struct fd2_shader_stateobj *so = create_shader(SHADER_VERTEX);
431 struct ir2_cf *cf;
432 struct ir2_instruction *instr;
433
434 if (!so)
435 return NULL;
436
437 so->ir = ir2_shader_create();
438
439 cf = ir2_cf_create(so->ir, EXEC);
440
441 instr = ir2_instr_create_vtx_fetch(cf, 26, 0, FMT_32_32_32_FLOAT, false, 12);
442 ir2_reg_create(instr, 1, "xyz1", 0);
443 ir2_reg_create(instr, 0, "x", 0);
444
445 cf = ir2_cf_create_alloc(so->ir, SQ_POSITION, 0);
446 cf = ir2_cf_create(so->ir, EXEC);
447
448 instr = ir2_instr_create_alu(cf, MAXv, ~0);
449 ir2_reg_create(instr, 62, NULL, IR2_REG_EXPORT);
450 ir2_reg_create(instr, 1, NULL, 0);
451 ir2_reg_create(instr, 1, NULL, 0);
452
453 cf = ir2_cf_create_alloc(so->ir, SQ_PARAMETER_PIXEL, 0);
454 cf = ir2_cf_create(so->ir, EXEC_END);
455
456 return assemble(so);
457 }
458
459 void
fd2_prog_init(struct pipe_context * pctx)460 fd2_prog_init(struct pipe_context *pctx)
461 {
462 struct fd_context *ctx = fd_context(pctx);
463
464 pctx->create_fs_state = fd2_fp_state_create;
465 pctx->delete_fs_state = fd2_fp_state_delete;
466
467 pctx->create_vs_state = fd2_vp_state_create;
468 pctx->delete_vs_state = fd2_vp_state_delete;
469
470 fd_prog_init(pctx);
471
472 ctx->solid_prog.fp = create_solid_fp();
473 ctx->solid_prog.vp = create_solid_vp();
474 ctx->blit_prog[0].fp = create_blit_fp();
475 ctx->blit_prog[0].vp = create_blit_vp();
476 }
477