1bf215546Sopenharmony_ci/**************************************************************************
2bf215546Sopenharmony_ci *
3bf215546Sopenharmony_ci * Copyright 2007 VMware, Inc.
4bf215546Sopenharmony_ci * All Rights Reserved.
5bf215546Sopenharmony_ci *
6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the
8bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
9bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
10bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
11bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
12bf215546Sopenharmony_ci * the following conditions:
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
15bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
16bf215546Sopenharmony_ci * of the Software.
17bf215546Sopenharmony_ci *
18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21bf215546Sopenharmony_ci * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25bf215546Sopenharmony_ci *
26bf215546Sopenharmony_ci **************************************************************************/
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci/**
29bf215546Sopenharmony_ci * Execute fragment shader using the TGSI interpreter.
30bf215546Sopenharmony_ci */
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci#include "sp_context.h"
33bf215546Sopenharmony_ci#include "sp_state.h"
34bf215546Sopenharmony_ci#include "sp_fs.h"
35bf215546Sopenharmony_ci#include "sp_quad.h"
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_ci#include "pipe/p_state.h"
38bf215546Sopenharmony_ci#include "pipe/p_defines.h"
39bf215546Sopenharmony_ci#include "util/u_memory.h"
40bf215546Sopenharmony_ci#include "tgsi/tgsi_exec.h"
41bf215546Sopenharmony_ci#include "tgsi/tgsi_parse.h"
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ci/**
45bf215546Sopenharmony_ci * Subclass of sp_fragment_shader_variant
46bf215546Sopenharmony_ci */
47bf215546Sopenharmony_cistruct sp_exec_fragment_shader
48bf215546Sopenharmony_ci{
49bf215546Sopenharmony_ci   struct sp_fragment_shader_variant base;
50bf215546Sopenharmony_ci   /* No other members for now */
51bf215546Sopenharmony_ci};
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_cistatic void
55bf215546Sopenharmony_ciexec_prepare( const struct sp_fragment_shader_variant *var,
56bf215546Sopenharmony_ci              struct tgsi_exec_machine *machine,
57bf215546Sopenharmony_ci              struct tgsi_sampler *sampler,
58bf215546Sopenharmony_ci              struct tgsi_image *image,
59bf215546Sopenharmony_ci              struct tgsi_buffer *buffer )
60bf215546Sopenharmony_ci{
61bf215546Sopenharmony_ci   /*
62bf215546Sopenharmony_ci    * Bind tokens/shader to the interpreter's machine state.
63bf215546Sopenharmony_ci    */
64bf215546Sopenharmony_ci   tgsi_exec_machine_bind_shader(machine,
65bf215546Sopenharmony_ci                                 var->tokens,
66bf215546Sopenharmony_ci                                 sampler, image, buffer);
67bf215546Sopenharmony_ci}
68bf215546Sopenharmony_ci
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_ci/**
72bf215546Sopenharmony_ci * Compute quad X,Y,Z,W for the four fragments in a quad.
73bf215546Sopenharmony_ci *
74bf215546Sopenharmony_ci * This should really be part of the compiled shader.
75bf215546Sopenharmony_ci */
76bf215546Sopenharmony_cistatic void
77bf215546Sopenharmony_cisetup_pos_vector(const struct tgsi_interp_coef *coef,
78bf215546Sopenharmony_ci                 float x, float y,
79bf215546Sopenharmony_ci                 struct tgsi_exec_vector *quadpos)
80bf215546Sopenharmony_ci{
81bf215546Sopenharmony_ci   uint chan;
82bf215546Sopenharmony_ci   /* do X */
83bf215546Sopenharmony_ci   quadpos->xyzw[0].f[0] = x;
84bf215546Sopenharmony_ci   quadpos->xyzw[0].f[1] = x + 1;
85bf215546Sopenharmony_ci   quadpos->xyzw[0].f[2] = x;
86bf215546Sopenharmony_ci   quadpos->xyzw[0].f[3] = x + 1;
87bf215546Sopenharmony_ci
88bf215546Sopenharmony_ci   /* do Y */
89bf215546Sopenharmony_ci   quadpos->xyzw[1].f[0] = y;
90bf215546Sopenharmony_ci   quadpos->xyzw[1].f[1] = y;
91bf215546Sopenharmony_ci   quadpos->xyzw[1].f[2] = y + 1;
92bf215546Sopenharmony_ci   quadpos->xyzw[1].f[3] = y + 1;
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_ci   /* do Z and W for all fragments in the quad */
95bf215546Sopenharmony_ci   for (chan = 2; chan < 4; chan++) {
96bf215546Sopenharmony_ci      const float dadx = coef->dadx[chan];
97bf215546Sopenharmony_ci      const float dady = coef->dady[chan];
98bf215546Sopenharmony_ci      const float a0 = coef->a0[chan] + dadx * x + dady * y;
99bf215546Sopenharmony_ci      quadpos->xyzw[chan].f[0] = a0;
100bf215546Sopenharmony_ci      quadpos->xyzw[chan].f[1] = a0 + dadx;
101bf215546Sopenharmony_ci      quadpos->xyzw[chan].f[2] = a0 + dady;
102bf215546Sopenharmony_ci      quadpos->xyzw[chan].f[3] = a0 + dadx + dady;
103bf215546Sopenharmony_ci   }
104bf215546Sopenharmony_ci}
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_ci/* TODO: hide the machine struct in here somewhere, remove from this
108bf215546Sopenharmony_ci * interface:
109bf215546Sopenharmony_ci */
110bf215546Sopenharmony_cistatic unsigned
111bf215546Sopenharmony_ciexec_run( const struct sp_fragment_shader_variant *var,
112bf215546Sopenharmony_ci	  struct tgsi_exec_machine *machine,
113bf215546Sopenharmony_ci	  struct quad_header *quad,
114bf215546Sopenharmony_ci	  bool early_depth_test )
115bf215546Sopenharmony_ci{
116bf215546Sopenharmony_ci   /* Compute X, Y, Z, W vals for this quad */
117bf215546Sopenharmony_ci   setup_pos_vector(quad->posCoef,
118bf215546Sopenharmony_ci                    (float)quad->input.x0, (float)quad->input.y0,
119bf215546Sopenharmony_ci                    &machine->QuadPos);
120bf215546Sopenharmony_ci
121bf215546Sopenharmony_ci   /* convert 0 to 1.0 and 1 to -1.0 */
122bf215546Sopenharmony_ci   machine->Face = (float) (quad->input.facing * -2 + 1);
123bf215546Sopenharmony_ci
124bf215546Sopenharmony_ci   machine->NonHelperMask = quad->inout.mask;
125bf215546Sopenharmony_ci   quad->inout.mask &= tgsi_exec_machine_run( machine, 0 );
126bf215546Sopenharmony_ci   if (quad->inout.mask == 0)
127bf215546Sopenharmony_ci      return FALSE;
128bf215546Sopenharmony_ci
129bf215546Sopenharmony_ci   /* store outputs */
130bf215546Sopenharmony_ci   {
131bf215546Sopenharmony_ci      const ubyte *sem_name = var->info.output_semantic_name;
132bf215546Sopenharmony_ci      const ubyte *sem_index = var->info.output_semantic_index;
133bf215546Sopenharmony_ci      const uint n = var->info.num_outputs;
134bf215546Sopenharmony_ci      uint i;
135bf215546Sopenharmony_ci      for (i = 0; i < n; i++) {
136bf215546Sopenharmony_ci         switch (sem_name[i]) {
137bf215546Sopenharmony_ci         case TGSI_SEMANTIC_COLOR:
138bf215546Sopenharmony_ci            {
139bf215546Sopenharmony_ci               uint cbuf = sem_index[i];
140bf215546Sopenharmony_ci
141bf215546Sopenharmony_ci               assert(sizeof(quad->output.color[cbuf]) ==
142bf215546Sopenharmony_ci                      sizeof(machine->Outputs[i]));
143bf215546Sopenharmony_ci
144bf215546Sopenharmony_ci               /* copy float[4][4] result */
145bf215546Sopenharmony_ci               memcpy(quad->output.color[cbuf],
146bf215546Sopenharmony_ci                      &machine->Outputs[i],
147bf215546Sopenharmony_ci                      sizeof(quad->output.color[0]) );
148bf215546Sopenharmony_ci            }
149bf215546Sopenharmony_ci            break;
150bf215546Sopenharmony_ci         case TGSI_SEMANTIC_POSITION:
151bf215546Sopenharmony_ci            {
152bf215546Sopenharmony_ci               uint j;
153bf215546Sopenharmony_ci
154bf215546Sopenharmony_ci               if (!early_depth_test) {
155bf215546Sopenharmony_ci                  for (j = 0; j < 4; j++)
156bf215546Sopenharmony_ci                     quad->output.depth[j] = machine->Outputs[i].xyzw[2].f[j];
157bf215546Sopenharmony_ci               }
158bf215546Sopenharmony_ci            }
159bf215546Sopenharmony_ci            break;
160bf215546Sopenharmony_ci         case TGSI_SEMANTIC_STENCIL:
161bf215546Sopenharmony_ci            {
162bf215546Sopenharmony_ci               uint j;
163bf215546Sopenharmony_ci               if (!early_depth_test) {
164bf215546Sopenharmony_ci                  for (j = 0; j < 4; j++)
165bf215546Sopenharmony_ci                     quad->output.stencil[j] = (unsigned)machine->Outputs[i].xyzw[1].u[j];
166bf215546Sopenharmony_ci               }
167bf215546Sopenharmony_ci            }
168bf215546Sopenharmony_ci            break;
169bf215546Sopenharmony_ci         }
170bf215546Sopenharmony_ci      }
171bf215546Sopenharmony_ci   }
172bf215546Sopenharmony_ci
173bf215546Sopenharmony_ci   return TRUE;
174bf215546Sopenharmony_ci}
175bf215546Sopenharmony_ci
176bf215546Sopenharmony_ci
177bf215546Sopenharmony_cistatic void
178bf215546Sopenharmony_ciexec_delete(struct sp_fragment_shader_variant *var,
179bf215546Sopenharmony_ci            struct tgsi_exec_machine *machine)
180bf215546Sopenharmony_ci{
181bf215546Sopenharmony_ci   if (machine->Tokens == var->tokens) {
182bf215546Sopenharmony_ci      tgsi_exec_machine_bind_shader(machine, NULL, NULL, NULL, NULL);
183bf215546Sopenharmony_ci   }
184bf215546Sopenharmony_ci
185bf215546Sopenharmony_ci   FREE( (void *) var->tokens );
186bf215546Sopenharmony_ci   FREE(var);
187bf215546Sopenharmony_ci}
188bf215546Sopenharmony_ci
189bf215546Sopenharmony_ci
190bf215546Sopenharmony_cistruct sp_fragment_shader_variant *
191bf215546Sopenharmony_cisoftpipe_create_fs_variant_exec(struct softpipe_context *softpipe)
192bf215546Sopenharmony_ci{
193bf215546Sopenharmony_ci   struct sp_exec_fragment_shader *shader;
194bf215546Sopenharmony_ci
195bf215546Sopenharmony_ci   shader = CALLOC_STRUCT(sp_exec_fragment_shader);
196bf215546Sopenharmony_ci   if (!shader)
197bf215546Sopenharmony_ci      return NULL;
198bf215546Sopenharmony_ci
199bf215546Sopenharmony_ci   shader->base.prepare = exec_prepare;
200bf215546Sopenharmony_ci   shader->base.run = exec_run;
201bf215546Sopenharmony_ci   shader->base.delete = exec_delete;
202bf215546Sopenharmony_ci
203bf215546Sopenharmony_ci   return &shader->base;
204bf215546Sopenharmony_ci}
205