1/* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3/*
4 * Copyright (C) 2014 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 "tgsi/tgsi_text.h"
30#include "tgsi/tgsi_ureg.h"
31
32#include "freedreno_program.h"
33#include "freedreno_context.h"
34
35static void
36fd_fp_state_bind(struct pipe_context *pctx, void *hwcso)
37{
38	struct fd_context *ctx = fd_context(pctx);
39	ctx->prog.fp = hwcso;
40	ctx->dirty |= FD_SHADER_DIRTY_FP;
41}
42
43static void
44fd_vp_state_bind(struct pipe_context *pctx, void *hwcso)
45{
46	struct fd_context *ctx = fd_context(pctx);
47	ctx->prog.vp = hwcso;
48	ctx->dirty |= FD_SHADER_DIRTY_VP;
49}
50
51static const char *solid_fp =
52	"FRAG                                        \n"
53	"PROPERTY FS_COLOR0_WRITES_ALL_CBUFS 1       \n"
54	"DCL CONST[0]                                \n"
55	"DCL OUT[0], COLOR                           \n"
56	"  0: MOV OUT[0], CONST[0]                   \n"
57	"  1: END                                    \n";
58
59static const char *solid_vp =
60	"VERT                                        \n"
61	"DCL IN[0]                                   \n"
62	"DCL OUT[0], POSITION                        \n"
63	"  0: MOV OUT[0], IN[0]                      \n"
64	"  1: END                                    \n";
65
66static const char *blit_vp =
67	"VERT                                        \n"
68	"DCL IN[0]                                   \n"
69	"DCL IN[1]                                   \n"
70	"DCL OUT[0], TEXCOORD[0]                     \n"
71	"DCL OUT[1], POSITION                        \n"
72	"  0: MOV OUT[0], IN[0]                      \n"
73	"  0: MOV OUT[1], IN[1]                      \n"
74	"  1: END                                    \n";
75
76static void * assemble_tgsi(struct pipe_context *pctx,
77		const char *src, bool frag)
78{
79	struct tgsi_token toks[32];
80	struct pipe_shader_state cso = {
81			.tokens = toks,
82	};
83
84	bool ret = tgsi_text_translate(src, toks, ARRAY_SIZE(toks));
85	assume(ret);
86
87	if (frag)
88		return pctx->create_fs_state(pctx, &cso);
89	else
90		return pctx->create_vs_state(pctx, &cso);
91}
92
93static void *
94fd_prog_blit(struct pipe_context *pctx, int rts, bool depth)
95{
96	int i;
97	struct ureg_src tc;
98	struct ureg_program *ureg;
99
100	debug_assert(rts <= MAX_RENDER_TARGETS);
101
102	ureg = ureg_create(PIPE_SHADER_FRAGMENT);
103	if (!ureg)
104		return NULL;
105
106	tc = ureg_DECL_fs_input(
107			ureg, TGSI_SEMANTIC_GENERIC, 0, TGSI_INTERPOLATE_PERSPECTIVE);
108	for (i = 0; i < rts; i++)
109		ureg_TEX(ureg, ureg_DECL_output(ureg, TGSI_SEMANTIC_COLOR, i),
110				 TGSI_TEXTURE_2D, tc, ureg_DECL_sampler(ureg, i));
111	if (depth)
112		ureg_TEX(ureg,
113				 ureg_writemask(
114						 ureg_DECL_output(ureg, TGSI_SEMANTIC_POSITION, 0),
115						 TGSI_WRITEMASK_Z),
116				 TGSI_TEXTURE_2D, tc, ureg_DECL_sampler(ureg, rts));
117
118	ureg_END(ureg);
119
120	return ureg_create_shader_and_destroy(ureg, pctx);
121}
122
123
124void fd_prog_init(struct pipe_context *pctx)
125{
126	struct fd_context *ctx = fd_context(pctx);
127	int i;
128
129	pctx->bind_fs_state = fd_fp_state_bind;
130	pctx->bind_vs_state = fd_vp_state_bind;
131
132	// XXX for now, let a2xx keep it's own hand-rolled shaders
133	// for solid and blit progs:
134	if (ctx->screen->gpu_id < 300)
135		return;
136
137	ctx->solid_prog.fp = assemble_tgsi(pctx, solid_fp, true);
138	ctx->solid_prog.vp = assemble_tgsi(pctx, solid_vp, false);
139	ctx->blit_prog[0].vp = assemble_tgsi(pctx, blit_vp, false);
140	ctx->blit_prog[0].fp = fd_prog_blit(pctx, 1, false);
141	for (i = 1; i < ctx->screen->max_rts; i++) {
142		ctx->blit_prog[i].vp = ctx->blit_prog[0].vp;
143		ctx->blit_prog[i].fp = fd_prog_blit(pctx, i + 1, false);
144	}
145
146	ctx->blit_z.vp = ctx->blit_prog[0].vp;
147	ctx->blit_z.fp = fd_prog_blit(pctx, 0, true);
148	ctx->blit_zs.vp = ctx->blit_prog[0].vp;
149	ctx->blit_zs.fp = fd_prog_blit(pctx, 1, true);
150}
151
152void fd_prog_fini(struct pipe_context *pctx)
153{
154	struct fd_context *ctx = fd_context(pctx);
155	int i;
156
157	pctx->delete_vs_state(pctx, ctx->solid_prog.vp);
158	pctx->delete_fs_state(pctx, ctx->solid_prog.fp);
159	pctx->delete_vs_state(pctx, ctx->blit_prog[0].vp);
160	for (i = 0; i < ctx->screen->max_rts; i++)
161		pctx->delete_fs_state(pctx, ctx->blit_prog[i].fp);
162	pctx->delete_fs_state(pctx, ctx->blit_z.fp);
163	pctx->delete_fs_state(pctx, ctx->blit_zs.fp);
164}
165