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