1 /*
2 * Copyright © 2014 Rob Clark <[email protected]>
3 * SPDX-License-Identifier: MIT
4 *
5 * Authors:
6 * Rob Clark <[email protected]>
7 */
8
9 #include "tgsi/tgsi_text.h"
10 #include "tgsi/tgsi_ureg.h"
11
12 #include "util/u_simple_shaders.h"
13
14 #include "freedreno_context.h"
15 #include "freedreno_program.h"
16
17 static void
update_bound_stage(struct fd_context * ctx,enum pipe_shader_type shader,bool bound)18 update_bound_stage(struct fd_context *ctx, enum pipe_shader_type shader,
19 bool bound) assert_dt
20 {
21 uint32_t bound_shader_stages = ctx->bound_shader_stages;
22 if (bound) {
23 ctx->bound_shader_stages |= BIT(shader);
24 } else {
25 ctx->bound_shader_stages &= ~BIT(shader);
26 }
27 if (ctx->update_draw && (bound_shader_stages != ctx->bound_shader_stages))
28 ctx->update_draw(ctx);
29 }
30
31 static void
fd_set_tess_state(struct pipe_context * pctx,const float default_outer_level[4],const float default_inner_level[2])32 fd_set_tess_state(struct pipe_context *pctx,
33 const float default_outer_level[4],
34 const float default_inner_level[2])
35 in_dt
36 {
37 struct fd_context *ctx = fd_context(pctx);
38
39 /* These turn into driver-params where are emitted on every draw if
40 * needed by the shader (they will only be needed by pass-through
41 * TCS shader)
42 */
43 memcpy(ctx->default_outer_level,
44 default_outer_level,
45 sizeof(ctx->default_outer_level));
46
47 memcpy(ctx->default_inner_level,
48 default_inner_level,
49 sizeof(ctx->default_inner_level));
50 }
51
52 static void
fd_set_patch_vertices(struct pipe_context * pctx,uint8_t patch_vertices)53 fd_set_patch_vertices(struct pipe_context *pctx, uint8_t patch_vertices) in_dt
54 {
55 struct fd_context *ctx = fd_context(pctx);
56
57 if (ctx->patch_vertices == patch_vertices)
58 return;
59
60 ctx->patch_vertices = patch_vertices;
61
62 /* If we have tessellation this dirties the TCS state. Check for TES
63 * stage as TCS could be NULL (passthrough)
64 */
65 if (ctx->prog.ds || ctx->prog.hs) {
66 fd_context_dirty_shader(ctx, PIPE_SHADER_TESS_CTRL, FD_DIRTY_SHADER_PROG);
67 }
68 }
69
70 static void
fd_vs_state_bind(struct pipe_context * pctx,void * hwcso)71 fd_vs_state_bind(struct pipe_context *pctx, void *hwcso) in_dt
72 {
73 struct fd_context *ctx = fd_context(pctx);
74 ctx->prog.vs = hwcso;
75 fd_context_dirty_shader(ctx, PIPE_SHADER_VERTEX, FD_DIRTY_SHADER_PROG);
76 update_bound_stage(ctx, PIPE_SHADER_VERTEX, !!hwcso);
77 }
78
79 static void
fd_tcs_state_bind(struct pipe_context * pctx,void * hwcso)80 fd_tcs_state_bind(struct pipe_context *pctx, void *hwcso) in_dt
81 {
82 struct fd_context *ctx = fd_context(pctx);
83 ctx->prog.hs = hwcso;
84 fd_context_dirty_shader(ctx, PIPE_SHADER_TESS_CTRL, FD_DIRTY_SHADER_PROG);
85 update_bound_stage(ctx, PIPE_SHADER_TESS_CTRL, !!hwcso);
86 }
87
88 static void
fd_tes_state_bind(struct pipe_context * pctx,void * hwcso)89 fd_tes_state_bind(struct pipe_context *pctx, void *hwcso) in_dt
90 {
91 struct fd_context *ctx = fd_context(pctx);
92 ctx->prog.ds = hwcso;
93 fd_context_dirty_shader(ctx, PIPE_SHADER_TESS_EVAL, FD_DIRTY_SHADER_PROG);
94 update_bound_stage(ctx, PIPE_SHADER_TESS_EVAL, !!hwcso);
95 }
96
97 static void
fd_gs_state_bind(struct pipe_context * pctx,void * hwcso)98 fd_gs_state_bind(struct pipe_context *pctx, void *hwcso) in_dt
99 {
100 struct fd_context *ctx = fd_context(pctx);
101 ctx->prog.gs = hwcso;
102 fd_context_dirty_shader(ctx, PIPE_SHADER_GEOMETRY, FD_DIRTY_SHADER_PROG);
103 update_bound_stage(ctx, PIPE_SHADER_GEOMETRY, !!hwcso);
104 }
105
106 static void
fd_fs_state_bind(struct pipe_context * pctx,void * hwcso)107 fd_fs_state_bind(struct pipe_context *pctx, void *hwcso) in_dt
108 {
109 struct fd_context *ctx = fd_context(pctx);
110 ctx->prog.fs = hwcso;
111 fd_context_dirty_shader(ctx, PIPE_SHADER_FRAGMENT, FD_DIRTY_SHADER_PROG);
112 update_bound_stage(ctx, PIPE_SHADER_FRAGMENT, !!hwcso);
113 }
114
115 static const char *solid_fs = "FRAG \n"
116 "PROPERTY FS_COLOR0_WRITES_ALL_CBUFS 1 \n"
117 "DCL CONST[0] \n"
118 "DCL OUT[0], COLOR \n"
119 " 0: MOV OUT[0], CONST[0] \n"
120 " 1: END \n";
121
122 static const char *solid_vs = "VERT \n"
123 "DCL IN[0] \n"
124 "DCL OUT[0], POSITION \n"
125 " 0: MOV OUT[0], IN[0] \n"
126 " 1: END \n";
127
128 static void *
assemble_tgsi(struct pipe_context * pctx,const char * src,bool frag)129 assemble_tgsi(struct pipe_context *pctx, const char *src, bool frag)
130 {
131 struct tgsi_token toks[32];
132 struct pipe_shader_state cso = {
133 .tokens = toks,
134 };
135
136 bool ret = tgsi_text_translate(src, toks, ARRAY_SIZE(toks));
137 assume(ret);
138
139 if (frag)
140 return pctx->create_fs_state(pctx, &cso);
141 else
142 return pctx->create_vs_state(pctx, &cso);
143 }
144
145 /* the correct semantic to use for the texcoord varying depends on pipe-cap: */
146 static enum tgsi_semantic
texcoord_semantic(struct pipe_context * pctx)147 texcoord_semantic(struct pipe_context *pctx)
148 {
149 struct pipe_screen *pscreen = pctx->screen;
150
151 if (pscreen->get_param(pscreen, PIPE_CAP_TGSI_TEXCOORD)) {
152 return TGSI_SEMANTIC_TEXCOORD;
153 } else {
154 return TGSI_SEMANTIC_GENERIC;
155 }
156 }
157
158 static void *
fd_prog_blit_vs(struct pipe_context * pctx)159 fd_prog_blit_vs(struct pipe_context *pctx)
160 {
161 struct ureg_program *ureg;
162
163 ureg = ureg_create(PIPE_SHADER_VERTEX);
164 if (!ureg)
165 return NULL;
166
167 struct ureg_src in0 = ureg_DECL_vs_input(ureg, 0);
168 struct ureg_src in1 = ureg_DECL_vs_input(ureg, 1);
169
170 struct ureg_dst out0 = ureg_DECL_output(ureg, texcoord_semantic(pctx), 0);
171 struct ureg_dst out1 = ureg_DECL_output(ureg, TGSI_SEMANTIC_POSITION, 1);
172
173 ureg_MOV(ureg, out0, in0);
174 ureg_MOV(ureg, out1, in1);
175
176 ureg_END(ureg);
177
178 return ureg_create_shader_and_destroy(ureg, pctx);
179 }
180
181 static void *
fd_prog_blit_fs(struct pipe_context * pctx,int rts,bool depth)182 fd_prog_blit_fs(struct pipe_context *pctx, int rts, bool depth)
183 {
184 int i;
185 struct ureg_src tc;
186 struct ureg_program *ureg;
187
188 assert(rts <= MAX_RENDER_TARGETS);
189
190 ureg = ureg_create(PIPE_SHADER_FRAGMENT);
191 if (!ureg)
192 return NULL;
193
194 tc = ureg_DECL_fs_input(ureg, texcoord_semantic(pctx), 0,
195 TGSI_INTERPOLATE_PERSPECTIVE);
196 for (i = 0; i < rts; i++)
197 ureg_TEX(ureg, ureg_DECL_output(ureg, TGSI_SEMANTIC_COLOR, i),
198 TGSI_TEXTURE_2D, tc, ureg_DECL_sampler(ureg, i));
199 if (depth)
200 ureg_TEX(ureg,
201 ureg_writemask(ureg_DECL_output(ureg, TGSI_SEMANTIC_POSITION, 0),
202 TGSI_WRITEMASK_Z),
203 TGSI_TEXTURE_2D, tc, ureg_DECL_sampler(ureg, rts));
204
205 ureg_END(ureg);
206
207 return ureg_create_shader_and_destroy(ureg, pctx);
208 }
209
210 void
fd_prog_init(struct pipe_context * pctx)211 fd_prog_init(struct pipe_context *pctx)
212 {
213 struct fd_context *ctx = fd_context(pctx);
214 int i;
215
216 pctx->bind_vs_state = fd_vs_state_bind;
217 pctx->bind_tcs_state = fd_tcs_state_bind;
218 pctx->bind_tes_state = fd_tes_state_bind;
219 pctx->bind_gs_state = fd_gs_state_bind;
220 pctx->bind_fs_state = fd_fs_state_bind;
221 pctx->set_tess_state = fd_set_tess_state;
222 pctx->set_patch_vertices = fd_set_patch_vertices;
223
224 if (ctx->flags & PIPE_CONTEXT_COMPUTE_ONLY)
225 return;
226
227 ctx->solid_prog.fs = assemble_tgsi(pctx, solid_fs, true);
228 ctx->solid_prog.vs = assemble_tgsi(pctx, solid_vs, false);
229
230 if (ctx->screen->gen >= 6) {
231 ctx->solid_layered_prog.fs = assemble_tgsi(pctx, solid_fs, true);
232 ctx->solid_layered_prog.vs = util_make_layered_clear_vertex_shader(pctx);
233 }
234
235 if (ctx->screen->gen >= 5)
236 return;
237
238 ctx->blit_prog[0].vs = fd_prog_blit_vs(pctx);
239 ctx->blit_prog[0].fs = fd_prog_blit_fs(pctx, 1, false);
240
241 if (ctx->screen->gen < 3)
242 return;
243
244 for (i = 1; i < ctx->screen->max_rts; i++) {
245 ctx->blit_prog[i].vs = ctx->blit_prog[0].vs;
246 ctx->blit_prog[i].fs = fd_prog_blit_fs(pctx, i + 1, false);
247 }
248
249 ctx->blit_z.vs = ctx->blit_prog[0].vs;
250 ctx->blit_z.fs = fd_prog_blit_fs(pctx, 0, true);
251 ctx->blit_zs.vs = ctx->blit_prog[0].vs;
252 ctx->blit_zs.fs = fd_prog_blit_fs(pctx, 1, true);
253 }
254
255 void
fd_prog_fini(struct pipe_context * pctx)256 fd_prog_fini(struct pipe_context *pctx)
257 {
258 struct fd_context *ctx = fd_context(pctx);
259 int i;
260
261 if (ctx->flags & PIPE_CONTEXT_COMPUTE_ONLY)
262 return;
263
264 pctx->delete_vs_state(pctx, ctx->solid_prog.vs);
265 pctx->delete_fs_state(pctx, ctx->solid_prog.fs);
266
267 if (ctx->screen->gen >= 6) {
268 pctx->delete_vs_state(pctx, ctx->solid_layered_prog.vs);
269 pctx->delete_fs_state(pctx, ctx->solid_layered_prog.fs);
270 }
271
272 if (ctx->screen->gen >= 5)
273 return;
274
275 pctx->delete_vs_state(pctx, ctx->blit_prog[0].vs);
276 pctx->delete_fs_state(pctx, ctx->blit_prog[0].fs);
277
278 if (ctx->screen->gen < 3)
279 return;
280
281 for (i = 1; i < ctx->screen->max_rts; i++)
282 pctx->delete_fs_state(pctx, ctx->blit_prog[i].fs);
283 pctx->delete_fs_state(pctx, ctx->blit_z.fs);
284 pctx->delete_fs_state(pctx, ctx->blit_zs.fs);
285 }
286