summaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/softpipe/sp_fs_exec.c
blob: cb22850496fcfa80b4243708457b4dff40015bae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**************************************************************************
 * 
 * Copyright 2007 VMware, Inc.
 * All Rights Reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 **************************************************************************/

/**
 * Execute fragment shader using the TGSI interpreter.
 */

#include "sp_context.h"
#include "sp_state.h"
#include "sp_fs.h"
#include "sp_quad.h"

#include "pipe/p_state.h"
#include "pipe/p_defines.h"
#include "util/u_memory.h"
#include "tgsi/tgsi_exec.h"
#include "tgsi/tgsi_parse.h"


/**
 * Subclass of sp_fragment_shader_variant
 */
struct sp_exec_fragment_shader
{
   struct sp_fragment_shader_variant base;
   /* No other members for now */
};


static void
exec_prepare( const struct sp_fragment_shader_variant *var,
              struct tgsi_exec_machine *machine,
              struct tgsi_sampler *sampler,
              struct tgsi_image *image,
              struct tgsi_buffer *buffer )
{
   /*
    * Bind tokens/shader to the interpreter's machine state.
    */
   tgsi_exec_machine_bind_shader(machine,
                                 var->tokens,
                                 sampler, image, buffer);
}



/**
 * Compute quad X,Y,Z,W for the four fragments in a quad.
 *
 * This should really be part of the compiled shader.
 */
static void
setup_pos_vector(const struct tgsi_interp_coef *coef,
                 float x, float y,
                 struct tgsi_exec_vector *quadpos)
{
   uint chan;
   /* do X */
   quadpos->xyzw[0].f[0] = x;
   quadpos->xyzw[0].f[1] = x + 1;
   quadpos->xyzw[0].f[2] = x;
   quadpos->xyzw[0].f[3] = x + 1;

   /* do Y */
   quadpos->xyzw[1].f[0] = y;
   quadpos->xyzw[1].f[1] = y;
   quadpos->xyzw[1].f[2] = y + 1;
   quadpos->xyzw[1].f[3] = y + 1;

   /* do Z and W for all fragments in the quad */
   for (chan = 2; chan < 4; chan++) {
      const float dadx = coef->dadx[chan];
      const float dady = coef->dady[chan];
      const float a0 = coef->a0[chan] + dadx * x + dady * y;
      quadpos->xyzw[chan].f[0] = a0;
      quadpos->xyzw[chan].f[1] = a0 + dadx;
      quadpos->xyzw[chan].f[2] = a0 + dady;
      quadpos->xyzw[chan].f[3] = a0 + dadx + dady;
   }
}


/* TODO: hide the machine struct in here somewhere, remove from this
 * interface:
 */
static unsigned 
exec_run( const struct sp_fragment_shader_variant *var,
	  struct tgsi_exec_machine *machine,
	  struct quad_header *quad,
	  bool early_depth_test )
{
   /* Compute X, Y, Z, W vals for this quad */
   setup_pos_vector(quad->posCoef, 
                    (float)quad->input.x0, (float)quad->input.y0, 
                    &machine->QuadPos);

   /* convert 0 to 1.0 and 1 to -1.0 */
   machine->Face = (float) (quad->input.facing * -2 + 1);

   machine->NonHelperMask = quad->inout.mask;
   quad->inout.mask &= tgsi_exec_machine_run( machine, 0 );
   if (quad->inout.mask == 0)
      return FALSE;

   /* store outputs */
   {
      const ubyte *sem_name = var->info.output_semantic_name;
      const ubyte *sem_index = var->info.output_semantic_index;
      const uint n = var->info.num_outputs;
      uint i;
      for (i = 0; i < n; i++) {
         switch (sem_name[i]) {
         case TGSI_SEMANTIC_COLOR:
            {
               uint cbuf = sem_index[i];

               assert(sizeof(quad->output.color[cbuf]) ==
                      sizeof(machine->Outputs[i]));

               /* copy float[4][4] result */
               memcpy(quad->output.color[cbuf],
                      &machine->Outputs[i],
                      sizeof(quad->output.color[0]) );
            }
            break;
         case TGSI_SEMANTIC_POSITION:
            {
               uint j;

               if (!early_depth_test) {
                  for (j = 0; j < 4; j++)
                     quad->output.depth[j] = machine->Outputs[i].xyzw[2].f[j];
               }
            }
            break;
         case TGSI_SEMANTIC_STENCIL:
            {
               uint j;
               if (!early_depth_test) {
                  for (j = 0; j < 4; j++)
                     quad->output.stencil[j] = (unsigned)machine->Outputs[i].xyzw[1].u[j];
               }
            }
            break;
         }
      }
   }

   return TRUE;
}


static void 
exec_delete(struct sp_fragment_shader_variant *var,
            struct tgsi_exec_machine *machine)
{
   if (machine->Tokens == var->tokens) {
      tgsi_exec_machine_bind_shader(machine, NULL, NULL, NULL, NULL);
   }

   FREE( (void *) var->tokens );
   FREE(var);
}


struct sp_fragment_shader_variant *
softpipe_create_fs_variant_exec(struct softpipe_context *softpipe)
{
   struct sp_exec_fragment_shader *shader;

   shader = CALLOC_STRUCT(sp_exec_fragment_shader);
   if (!shader)
      return NULL;

   shader->base.prepare = exec_prepare;
   shader->base.run = exec_run;
   shader->base.delete = exec_delete;

   return &shader->base;
}