freedreno_program.c revision 848b8605
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
31#include "freedreno_program.h"
32#include "freedreno_context.h"
33
34static void
35fd_fp_state_bind(struct pipe_context *pctx, void *hwcso)
36{
37	struct fd_context *ctx = fd_context(pctx);
38	ctx->prog.fp = hwcso;
39	ctx->prog.dirty |= FD_SHADER_DIRTY_FP;
40	ctx->dirty |= FD_DIRTY_PROG;
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->prog.dirty |= FD_SHADER_DIRTY_VP;
49	ctx->dirty |= FD_DIRTY_PROG;
50}
51
52static const char *solid_fp =
53	"FRAG                                        \n"
54	"PROPERTY FS_COLOR0_WRITES_ALL_CBUFS 1       \n"
55	"DCL CONST[0]                                \n"
56	"DCL OUT[0], COLOR                           \n"
57	"  0: MOV OUT[0], CONST[0]                   \n"
58	"  1: END                                    \n";
59
60static const char *solid_vp =
61	"VERT                                        \n"
62	"DCL IN[0]                                   \n"
63	"DCL OUT[0], POSITION                        \n"
64	"  0: MOV OUT[0], IN[0]                      \n"
65	"  1: END                                    \n";
66
67static const char *blit_fp =
68	"FRAG                                        \n"
69	"PROPERTY FS_COLOR0_WRITES_ALL_CBUFS 1       \n"
70	"DCL IN[0], TEXCOORD[0], PERSPECTIVE         \n"
71	"DCL OUT[0], COLOR                           \n"
72	"DCL SAMP[0]                                 \n"
73	"  0: TEX OUT[0], IN[0], SAMP[0], 2D         \n"
74	"  1: END                                    \n";
75
76static const char *blit_vp =
77	"VERT                                        \n"
78	"DCL IN[0]                                   \n"
79	"DCL IN[1]                                   \n"
80	"DCL OUT[0], TEXCOORD[0]                     \n"
81	"DCL OUT[1], POSITION                        \n"
82	"  0: MOV OUT[0], IN[0]                      \n"
83	"  0: MOV OUT[1], IN[1]                      \n"
84	"  1: END                                    \n";
85
86static void * assemble_tgsi(struct pipe_context *pctx,
87		const char *src, bool frag)
88{
89	struct tgsi_token toks[32];
90	struct pipe_shader_state cso = {
91			.tokens = toks,
92	};
93
94	tgsi_text_translate(src, toks, ARRAY_SIZE(toks));
95
96	if (frag)
97		return pctx->create_fs_state(pctx, &cso);
98	else
99		return pctx->create_vs_state(pctx, &cso);
100}
101
102void fd_prog_init(struct pipe_context *pctx)
103{
104	struct fd_context *ctx = fd_context(pctx);
105
106	pctx->bind_fs_state = fd_fp_state_bind;
107	pctx->bind_vs_state = fd_vp_state_bind;
108
109	// XXX for now, let a2xx keep it's own hand-rolled shaders
110	// for solid and blit progs:
111	if (ctx->screen->gpu_id < 300)
112		return;
113
114	ctx->solid_prog.fp = assemble_tgsi(pctx, solid_fp, true);
115	ctx->solid_prog.vp = assemble_tgsi(pctx, solid_vp, false);
116	ctx->blit_prog.fp = assemble_tgsi(pctx, blit_fp, true);
117	ctx->blit_prog.vp = assemble_tgsi(pctx, blit_vp, false);
118}
119
120void fd_prog_fini(struct pipe_context *pctx)
121{
122	struct fd_context *ctx = fd_context(pctx);
123
124	pctx->delete_vs_state(pctx, ctx->solid_prog.vp);
125	pctx->delete_fs_state(pctx, ctx->solid_prog.fp);
126	pctx->delete_vs_state(pctx, ctx->blit_prog.vp);
127	pctx->delete_fs_state(pctx, ctx->blit_prog.fp);
128}
129