2 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
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:
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
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
24 * Rob Clark <robclark@freedesktop.org>
27 #include "tgsi/tgsi_text.h"
28 #include "tgsi/tgsi_ureg.h"
30 #include "freedreno_program.h"
31 #include "freedreno_context.h"
34 fd_vs_state_bind(struct pipe_context
*pctx
, void *hwcso
)
36 struct fd_context
*ctx
= fd_context(pctx
);
38 ctx
->dirty_shader
[PIPE_SHADER_VERTEX
] |= FD_DIRTY_SHADER_PROG
;
39 ctx
->dirty
|= FD_DIRTY_PROG
;
43 fd_tcs_state_bind(struct pipe_context
*pctx
, void *hwcso
)
45 struct fd_context
*ctx
= fd_context(pctx
);
47 ctx
->dirty_shader
[PIPE_SHADER_TESS_CTRL
] |= FD_DIRTY_SHADER_PROG
;
48 ctx
->dirty
|= FD_DIRTY_PROG
;
52 fd_tes_state_bind(struct pipe_context
*pctx
, void *hwcso
)
54 struct fd_context
*ctx
= fd_context(pctx
);
56 ctx
->dirty_shader
[PIPE_SHADER_TESS_EVAL
] |= FD_DIRTY_SHADER_PROG
;
57 ctx
->dirty
|= FD_DIRTY_PROG
;
61 fd_gs_state_bind(struct pipe_context
*pctx
, void *hwcso
)
63 struct fd_context
*ctx
= fd_context(pctx
);
65 ctx
->dirty_shader
[PIPE_SHADER_GEOMETRY
] |= FD_DIRTY_SHADER_PROG
;
66 ctx
->dirty
|= FD_DIRTY_PROG
;
70 fd_fs_state_bind(struct pipe_context
*pctx
, void *hwcso
)
72 struct fd_context
*ctx
= fd_context(pctx
);
74 ctx
->dirty_shader
[PIPE_SHADER_FRAGMENT
] |= FD_DIRTY_SHADER_PROG
;
75 ctx
->dirty
|= FD_DIRTY_PROG
;
78 static const char *solid_fs
=
80 "PROPERTY FS_COLOR0_WRITES_ALL_CBUFS 1 \n"
82 "DCL OUT[0], COLOR \n"
83 " 0: MOV OUT[0], CONST[0] \n"
86 static const char *solid_vs
=
89 "DCL OUT[0], POSITION \n"
90 " 0: MOV OUT[0], IN[0] \n"
93 static void * assemble_tgsi(struct pipe_context
*pctx
,
94 const char *src
, bool frag
)
96 struct tgsi_token toks
[32];
97 struct pipe_shader_state cso
= {
101 bool ret
= tgsi_text_translate(src
, toks
, ARRAY_SIZE(toks
));
105 return pctx
->create_fs_state(pctx
, &cso
);
107 return pctx
->create_vs_state(pctx
, &cso
);
110 /* the correct semantic to use for the texcoord varying depends on pipe-cap: */
111 static enum tgsi_semantic
112 texcoord_semantic(struct pipe_context
*pctx
)
114 struct pipe_screen
*pscreen
= pctx
->screen
;
116 if (pscreen
->get_param(pscreen
, PIPE_CAP_TGSI_TEXCOORD
)) {
117 return TGSI_SEMANTIC_TEXCOORD
;
119 return TGSI_SEMANTIC_GENERIC
;
124 fd_prog_blit_vs(struct pipe_context
*pctx
)
126 struct ureg_program
*ureg
;
128 ureg
= ureg_create(PIPE_SHADER_VERTEX
);
132 struct ureg_src in0
= ureg_DECL_vs_input(ureg
, 0);
133 struct ureg_src in1
= ureg_DECL_vs_input(ureg
, 1);
135 struct ureg_dst out0
= ureg_DECL_output(ureg
, texcoord_semantic(pctx
), 0);
136 struct ureg_dst out1
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_POSITION
, 1);
138 ureg_MOV(ureg
, out0
, in0
);
139 ureg_MOV(ureg
, out1
, in1
);
143 return ureg_create_shader_and_destroy(ureg
, pctx
);
147 fd_prog_blit_fs(struct pipe_context
*pctx
, int rts
, bool depth
)
151 struct ureg_program
*ureg
;
153 debug_assert(rts
<= MAX_RENDER_TARGETS
);
155 ureg
= ureg_create(PIPE_SHADER_FRAGMENT
);
159 tc
= ureg_DECL_fs_input(
160 ureg
, texcoord_semantic(pctx
), 0, TGSI_INTERPOLATE_PERSPECTIVE
);
161 for (i
= 0; i
< rts
; i
++)
162 ureg_TEX(ureg
, ureg_DECL_output(ureg
, TGSI_SEMANTIC_COLOR
, i
),
163 TGSI_TEXTURE_2D
, tc
, ureg_DECL_sampler(ureg
, i
));
167 ureg_DECL_output(ureg
, TGSI_SEMANTIC_POSITION
, 0),
169 TGSI_TEXTURE_2D
, tc
, ureg_DECL_sampler(ureg
, rts
));
173 return ureg_create_shader_and_destroy(ureg
, pctx
);
177 void fd_prog_init(struct pipe_context
*pctx
)
179 struct fd_context
*ctx
= fd_context(pctx
);
182 pctx
->bind_vs_state
= fd_vs_state_bind
;
183 pctx
->bind_tcs_state
= fd_tcs_state_bind
;
184 pctx
->bind_tes_state
= fd_tes_state_bind
;
185 pctx
->bind_gs_state
= fd_gs_state_bind
;
186 pctx
->bind_fs_state
= fd_fs_state_bind
;
188 ctx
->solid_prog
.fs
= assemble_tgsi(pctx
, solid_fs
, true);
189 ctx
->solid_prog
.vs
= assemble_tgsi(pctx
, solid_vs
, false);
191 if (ctx
->screen
->gpu_id
>= 500)
194 ctx
->blit_prog
[0].vs
= fd_prog_blit_vs(pctx
);
195 ctx
->blit_prog
[0].fs
= fd_prog_blit_fs(pctx
, 1, false);
197 if (ctx
->screen
->gpu_id
< 300)
200 for (i
= 1; i
< ctx
->screen
->max_rts
; i
++) {
201 ctx
->blit_prog
[i
].vs
= ctx
->blit_prog
[0].vs
;
202 ctx
->blit_prog
[i
].fs
= fd_prog_blit_fs(pctx
, i
+ 1, false);
205 ctx
->blit_z
.vs
= ctx
->blit_prog
[0].vs
;
206 ctx
->blit_z
.fs
= fd_prog_blit_fs(pctx
, 0, true);
207 ctx
->blit_zs
.vs
= ctx
->blit_prog
[0].vs
;
208 ctx
->blit_zs
.fs
= fd_prog_blit_fs(pctx
, 1, true);
211 void fd_prog_fini(struct pipe_context
*pctx
)
213 struct fd_context
*ctx
= fd_context(pctx
);
216 pctx
->delete_vs_state(pctx
, ctx
->solid_prog
.vs
);
217 pctx
->delete_fs_state(pctx
, ctx
->solid_prog
.fs
);
218 if (ctx
->screen
->gpu_id
>= 500)
221 pctx
->delete_vs_state(pctx
, ctx
->blit_prog
[0].vs
);
222 pctx
->delete_fs_state(pctx
, ctx
->blit_prog
[0].fs
);
224 if (ctx
->screen
->gpu_id
< 300)
227 for (i
= 1; i
< ctx
->screen
->max_rts
; i
++)
228 pctx
->delete_fs_state(pctx
, ctx
->blit_prog
[i
].fs
);
229 pctx
->delete_fs_state(pctx
, ctx
->blit_z
.fs
);
230 pctx
->delete_fs_state(pctx
, ctx
->blit_zs
.fs
);