Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/gallium/auxiliary/tgsi/tgsi_two_side.c
4565 views
1
/*
2
* Copyright 2013 VMware, Inc.
3
* All Rights Reserved.
4
*
5
* Permission is hereby granted, free of charge, to any person obtaining a
6
* copy of this software and associated documentation files (the
7
* "Software"), to deal in the Software without restriction, including
8
* without limitation the rights to use, copy, modify, merge, publish,
9
* distribute, sub license, and/or sell copies of the Software, and to
10
* permit persons to whom the Software is furnished to do so, subject to
11
* the following conditions:
12
*
13
* The above copyright notice and this permission notice (including the
14
* next paragraph) shall be included in all copies or substantial portions
15
* of the Software.
16
*
17
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20
* IN NO EVENT SHALL THE AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR
21
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
*/
25
26
27
/**
28
* This utility transforms fragment shaders to facilitate two-sided lighting.
29
*
30
* Basically, if the FS has any color inputs (TGSI_SEMANTIC_COLOR) we'll:
31
* 1. create corresponding back-color inputs (TGSI_SEMANTIC_BCOLOR)
32
* 2. use the FACE register to choose between front/back colors and put the
33
* selected color in new temp regs.
34
* 3. replace reads of the original color inputs with the new temp regs.
35
*
36
* Then, the driver just needs to link the VS front/back output colors to
37
* the FS front/back input colors.
38
*/
39
40
#include "util/u_debug.h"
41
#include "util/u_math.h"
42
#include "tgsi_info.h"
43
#include "tgsi_two_side.h"
44
#include "tgsi_transform.h"
45
46
47
#define INVALID_INDEX 9999
48
49
50
struct two_side_transform_context
51
{
52
struct tgsi_transform_context base;
53
uint num_temps;
54
uint num_inputs;
55
uint face_input; /**< index of the FACE input */
56
uint front_color_input[2]; /**< INPUT regs */
57
uint front_color_interp[2];/**< TGSI_INTERPOLATE_x */
58
uint back_color_input[2]; /**< INPUT regs */
59
uint new_colors[2]; /**< TEMP regs */
60
};
61
62
63
static inline struct two_side_transform_context *
64
two_side_transform_context(struct tgsi_transform_context *ctx)
65
{
66
return (struct two_side_transform_context *) ctx;
67
}
68
69
70
static void
71
xform_decl(struct tgsi_transform_context *ctx,
72
struct tgsi_full_declaration *decl)
73
{
74
struct two_side_transform_context *ts = two_side_transform_context(ctx);
75
unsigned range_end = decl->Range.Last + 1;
76
77
if (decl->Declaration.File == TGSI_FILE_INPUT) {
78
if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR) {
79
/* found a front color */
80
assert(decl->Semantic.Index < 2);
81
ts->front_color_input[decl->Semantic.Index] = decl->Range.First;
82
ts->front_color_interp[decl->Semantic.Index] = decl->Interp.Interpolate;
83
}
84
else if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) {
85
ts->face_input = decl->Range.First;
86
}
87
ts->num_inputs = MAX2(ts->num_inputs, range_end);
88
}
89
else if (decl->Declaration.File == TGSI_FILE_TEMPORARY) {
90
ts->num_temps = MAX2(ts->num_temps, range_end);
91
}
92
93
ctx->emit_declaration(ctx, decl);
94
}
95
96
97
static void
98
emit_prolog(struct tgsi_transform_context *ctx)
99
{
100
struct two_side_transform_context *ts = two_side_transform_context(ctx);
101
struct tgsi_full_declaration decl;
102
struct tgsi_full_instruction inst;
103
uint num_colors = 0;
104
uint i;
105
106
/* Declare 0, 1 or 2 new BCOLOR inputs */
107
for (i = 0; i < 2; i++) {
108
if (ts->front_color_input[i] != INVALID_INDEX) {
109
decl = tgsi_default_full_declaration();
110
decl.Declaration.File = TGSI_FILE_INPUT;
111
decl.Declaration.Interpolate = 1;
112
decl.Declaration.Semantic = 1;
113
decl.Semantic.Name = TGSI_SEMANTIC_BCOLOR;
114
decl.Semantic.Index = i;
115
decl.Range.First = decl.Range.Last = ts->num_inputs++;
116
decl.Interp.Interpolate = ts->front_color_interp[i];
117
ctx->emit_declaration(ctx, &decl);
118
ts->back_color_input[i] = decl.Range.First;
119
num_colors++;
120
}
121
}
122
123
if (num_colors > 0) {
124
/* Declare 1 or 2 temp registers */
125
decl = tgsi_default_full_declaration();
126
decl.Declaration.File = TGSI_FILE_TEMPORARY;
127
decl.Range.First = ts->num_temps;
128
decl.Range.Last = ts->num_temps + num_colors - 1;
129
ctx->emit_declaration(ctx, &decl);
130
ts->new_colors[0] = ts->num_temps;
131
ts->new_colors[1] = ts->num_temps + 1;
132
133
if (ts->face_input == INVALID_INDEX) {
134
/* declare FACE INPUT register */
135
decl = tgsi_default_full_declaration();
136
decl.Declaration.File = TGSI_FILE_INPUT;
137
decl.Declaration.Semantic = 1;
138
decl.Semantic.Name = TGSI_SEMANTIC_FACE;
139
decl.Semantic.Index = 0;
140
decl.Range.First = decl.Range.Last = ts->num_inputs++;
141
ctx->emit_declaration(ctx, &decl);
142
ts->face_input = decl.Range.First;
143
}
144
145
/* CMP temp[c0], face, bcolor[c0], fcolor[c0]
146
* temp[c0] = face < 0.0 ? bcolor[c0] : fcolor[c0]
147
*/
148
for (i = 0; i < 2; i++) {
149
if (ts->front_color_input[i] != INVALID_INDEX) {
150
inst = tgsi_default_full_instruction();
151
inst.Instruction.Opcode = TGSI_OPCODE_CMP;
152
inst.Instruction.NumDstRegs = 1;
153
inst.Dst[0].Register.File = TGSI_FILE_TEMPORARY;
154
inst.Dst[0].Register.Index = ts->new_colors[i];
155
inst.Instruction.NumSrcRegs = 3;
156
inst.Src[0].Register.File = TGSI_FILE_INPUT;
157
inst.Src[0].Register.Index = ts->face_input;
158
inst.Src[1].Register.File = TGSI_FILE_INPUT;
159
inst.Src[1].Register.Index = ts->back_color_input[i];
160
inst.Src[2].Register.File = TGSI_FILE_INPUT;
161
inst.Src[2].Register.Index = ts->front_color_input[i];
162
163
ctx->emit_instruction(ctx, &inst);
164
}
165
}
166
}
167
}
168
169
170
static void
171
xform_inst(struct tgsi_transform_context *ctx,
172
struct tgsi_full_instruction *inst)
173
{
174
struct two_side_transform_context *ts = two_side_transform_context(ctx);
175
const struct tgsi_opcode_info *info =
176
tgsi_get_opcode_info(inst->Instruction.Opcode);
177
uint i, j;
178
179
/* Look for src regs which reference the input color and replace
180
* them with the temp color.
181
*/
182
for (i = 0; i < info->num_src; i++) {
183
if (inst->Src[i].Register.File == TGSI_FILE_INPUT) {
184
for (j = 0; j < 2; j++) {
185
if (inst->Src[i].Register.Index == (int)ts->front_color_input[j]) {
186
/* replace color input with temp reg */
187
inst->Src[i].Register.File = TGSI_FILE_TEMPORARY;
188
inst->Src[i].Register.Index = ts->new_colors[j];
189
break;
190
}
191
}
192
}
193
}
194
195
ctx->emit_instruction(ctx, inst);
196
}
197
198
199
struct tgsi_token *
200
tgsi_add_two_side(const struct tgsi_token *tokens_in)
201
{
202
struct two_side_transform_context transform;
203
const uint num_new_tokens = 100; /* should be enough */
204
const uint new_len = tgsi_num_tokens(tokens_in) + num_new_tokens;
205
struct tgsi_token *new_tokens;
206
207
/* setup transformation context */
208
memset(&transform, 0, sizeof(transform));
209
transform.base.transform_declaration = xform_decl;
210
transform.base.transform_instruction = xform_inst;
211
transform.base.prolog = emit_prolog;
212
transform.face_input = INVALID_INDEX;
213
transform.front_color_input[0] = INVALID_INDEX;
214
transform.front_color_input[1] = INVALID_INDEX;
215
transform.front_color_interp[0] = TGSI_INTERPOLATE_COLOR;
216
transform.front_color_interp[1] = TGSI_INTERPOLATE_COLOR;
217
transform.back_color_input[0] = INVALID_INDEX;
218
transform.back_color_input[1] = INVALID_INDEX;
219
220
/* allocate new tokens buffer */
221
new_tokens = tgsi_alloc_tokens(new_len);
222
if (!new_tokens)
223
return NULL;
224
225
/* transform the shader */
226
tgsi_transform_shader(tokens_in, new_tokens, new_len, &transform.base);
227
228
return new_tokens;
229
}
230
231