1 /**************************************************************************
3 * Copyright (C) 2015 Advanced Micro Devices, Inc.
4 * Copyright 2007 VMware, Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
29 #include "st_cb_bitmap.h"
30 #include "tgsi/tgsi_transform.h"
31 #include "tgsi/tgsi_scan.h"
32 #include "tgsi/tgsi_dump.h"
33 #include "util/u_debug.h"
35 struct tgsi_bitmap_transform
{
36 struct tgsi_transform_context base
;
37 struct tgsi_shader_info info
;
38 unsigned sampler_index
;
41 bool first_instruction_emitted
;
44 static inline struct tgsi_bitmap_transform
*
45 tgsi_bitmap_transform(struct tgsi_transform_context
*tctx
)
47 return (struct tgsi_bitmap_transform
*)tctx
;
51 transform_instr(struct tgsi_transform_context
*tctx
,
52 struct tgsi_full_instruction
*current_inst
)
54 struct tgsi_bitmap_transform
*ctx
= tgsi_bitmap_transform(tctx
);
55 struct tgsi_full_declaration decl
;
56 struct tgsi_full_instruction inst
;
58 int texcoord_index
= -1;
60 if (ctx
->first_instruction_emitted
) {
61 tctx
->emit_instruction(tctx
, current_inst
);
65 ctx
->first_instruction_emitted
= true;
67 /* Add TEMP[0] if it's missing. */
68 if (ctx
->info
.file_max
[TGSI_FILE_TEMPORARY
] == -1) {
69 decl
= tgsi_default_full_declaration();
70 decl
.Declaration
.File
= TGSI_FILE_TEMPORARY
;
71 tctx
->emit_declaration(tctx
, &decl
);
74 /* Add TEXCOORD[0] if it's missing. */
75 semantic
= ctx
->use_texcoord
? TGSI_SEMANTIC_TEXCOORD
:
76 TGSI_SEMANTIC_GENERIC
;
77 for (i
= 0; i
< ctx
->info
.num_inputs
; i
++) {
78 if (ctx
->info
.input_semantic_name
[i
] == semantic
&&
79 ctx
->info
.input_semantic_index
[i
] == 0) {
85 if (texcoord_index
== -1) {
86 decl
= tgsi_default_full_declaration();
87 decl
.Declaration
.File
= TGSI_FILE_INPUT
;
88 decl
.Declaration
.Semantic
= 1;
89 decl
.Semantic
.Name
= semantic
;
90 decl
.Declaration
.Interpolate
= 1;
91 decl
.Interp
.Interpolate
= TGSI_INTERPOLATE_PERSPECTIVE
;
92 decl
.Range
.First
= decl
.Range
.Last
= ctx
->info
.num_inputs
;
93 texcoord_index
= ctx
->info
.num_inputs
;
94 tctx
->emit_declaration(tctx
, &decl
);
97 /* Declare the sampler. */
98 decl
= tgsi_default_full_declaration();
99 decl
.Declaration
.File
= TGSI_FILE_SAMPLER
;
100 decl
.Range
.First
= decl
.Range
.Last
= ctx
->sampler_index
;
101 tctx
->emit_declaration(tctx
, &decl
);
103 /* TEX tmp0, fragment.texcoord[0], texture[0], 2D; */
104 inst
= tgsi_default_full_instruction();
105 inst
.Instruction
.Opcode
= TGSI_OPCODE_TEX
;
106 inst
.Instruction
.Texture
= 1;
107 inst
.Texture
.Texture
= TGSI_TEXTURE_2D
;
109 inst
.Instruction
.NumDstRegs
= 1;
110 inst
.Dst
[0].Register
.File
= TGSI_FILE_TEMPORARY
;
111 inst
.Dst
[0].Register
.Index
= 0;
112 inst
.Dst
[0].Register
.WriteMask
= TGSI_WRITEMASK_XYZW
;
114 inst
.Instruction
.NumSrcRegs
= 2;
115 inst
.Src
[0].Register
.File
= TGSI_FILE_INPUT
;
116 inst
.Src
[0].Register
.Index
= texcoord_index
;
117 inst
.Src
[0].Register
.SwizzleX
= TGSI_SWIZZLE_X
;
118 inst
.Src
[0].Register
.SwizzleY
= TGSI_SWIZZLE_Y
;
119 inst
.Src
[0].Register
.SwizzleZ
= TGSI_SWIZZLE_Z
;
120 inst
.Src
[0].Register
.SwizzleW
= TGSI_SWIZZLE_W
;
121 inst
.Src
[1].Register
.File
= TGSI_FILE_SAMPLER
;
122 inst
.Src
[1].Register
.Index
= ctx
->sampler_index
;
124 tctx
->emit_instruction(tctx
, &inst
);
126 /* KIL if -tmp0 < 0 # texel=0 -> keep / texel=0 -> discard */
127 inst
= tgsi_default_full_instruction();
128 inst
.Instruction
.Opcode
= TGSI_OPCODE_KILL_IF
;
129 inst
.Instruction
.NumDstRegs
= 0;
130 inst
.Instruction
.NumSrcRegs
= 1;
132 inst
.Src
[0].Register
.File
= TGSI_FILE_TEMPORARY
;
133 inst
.Src
[0].Register
.Index
= 0;
134 inst
.Src
[0].Register
.Negate
= 1;
135 inst
.Src
[0].Register
.SwizzleX
= TGSI_SWIZZLE_X
;
136 if (ctx
->swizzle_xxxx
) {
137 inst
.Src
[0].Register
.SwizzleY
= TGSI_SWIZZLE_X
;
138 inst
.Src
[0].Register
.SwizzleZ
= TGSI_SWIZZLE_X
;
139 inst
.Src
[0].Register
.SwizzleW
= TGSI_SWIZZLE_X
;
141 inst
.Src
[0].Register
.SwizzleY
= TGSI_SWIZZLE_Y
;
142 inst
.Src
[0].Register
.SwizzleZ
= TGSI_SWIZZLE_Z
;
143 inst
.Src
[0].Register
.SwizzleW
= TGSI_SWIZZLE_W
;
145 tctx
->emit_instruction(tctx
, &inst
);
147 /* And emit the instruction we got. */
148 tctx
->emit_instruction(tctx
, current_inst
);
151 const struct tgsi_token
*
152 st_get_bitmap_shader(const struct tgsi_token
*tokens
,
153 unsigned sampler_index
,
154 bool use_texcoord
, bool swizzle_xxxx
)
156 struct tgsi_bitmap_transform ctx
;
157 struct tgsi_token
*newtoks
;
160 memset(&ctx
, 0, sizeof(ctx
));
161 ctx
.base
.transform_instruction
= transform_instr
;
162 ctx
.sampler_index
= sampler_index
;
163 ctx
.use_texcoord
= use_texcoord
;
164 ctx
.swizzle_xxxx
= swizzle_xxxx
;
165 tgsi_scan_shader(tokens
, &ctx
.info
);
167 newlen
= tgsi_num_tokens(tokens
) + 20;
168 newtoks
= tgsi_alloc_tokens(newlen
);
172 tgsi_transform_shader(tokens
, newtoks
, newlen
, &ctx
.base
);