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