sp_fs_exec.c revision 05aeb92a092c26e7773beb95692fc72e70a40e56
1c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell/**************************************************************************
2c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell *
3c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell * All Rights Reserved.
5c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell *
6c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell * Permission is hereby granted, free of charge, to any person obtaining a
7c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell * copy of this software and associated documentation files (the
8c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell * "Software"), to deal in the Software without restriction, including
9c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell * without limitation the rights to use, copy, modify, merge, publish,
10c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell * distribute, sub license, and/or sell copies of the Software, and to
11c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell * permit persons to whom the Software is furnished to do so, subject to
12c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell * the following conditions:
13c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell *
14c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell * The above copyright notice and this permission notice (including the
15c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell * next paragraph) shall be included in all copies or substantial portions
16c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell * of the Software.
17c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell *
18c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell *
26c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell **************************************************************************/
27c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell
28c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell
29c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell#include "sp_context.h"
30c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell#include "sp_state.h"
31c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell#include "sp_fs.h"
32c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell#include "sp_headers.h"
33c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell
34c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell
35c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell#include "pipe/p_state.h"
36c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell#include "pipe/p_defines.h"
374f25420bdd834e81a3e22733304efc5261c2998aBrian Paul#include "util/u_memory.h"
38c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell#include "pipe/p_inlines.h"
39c208a2c791fa24c7c5887fc496738cbddbfafc72José Fonseca#include "tgsi/tgsi_exec.h"
40c208a2c791fa24c7c5887fc496738cbddbfafc72José Fonseca#include "tgsi/tgsi_parse.h"
41c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell
4205aeb92a092c26e7773beb95692fc72e70a40e56Brian Paulstruct sp_exec_fragment_shader
4305aeb92a092c26e7773beb95692fc72e70a40e56Brian Paul{
44c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell   struct sp_fragment_shader base;
4505aeb92a092c26e7773beb95692fc72e70a40e56Brian Paul   struct tgsi_token *machine_tokens;
46c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell};
47c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell
48c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell
4905aeb92a092c26e7773beb95692fc72e70a40e56Brian Paul/** cast wrapper */
5005aeb92a092c26e7773beb95692fc72e70a40e56Brian Paulstatic INLINE struct sp_exec_fragment_shader *
5105aeb92a092c26e7773beb95692fc72e70a40e56Brian Paulsp_exec_fragment_shader(struct sp_fragment_shader *base)
5205aeb92a092c26e7773beb95692fc72e70a40e56Brian Paul{
5305aeb92a092c26e7773beb95692fc72e70a40e56Brian Paul   return (struct sp_exec_fragment_shader *) base;
5405aeb92a092c26e7773beb95692fc72e70a40e56Brian Paul}
5505aeb92a092c26e7773beb95692fc72e70a40e56Brian Paul
56c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell
5720fbcbf5801c28865c0bfab3cda45302c8474a66Keith Whitwell/**
5820fbcbf5801c28865c0bfab3cda45302c8474a66Keith Whitwell * Compute quad X,Y,Z,W for the four fragments in a quad.
5920fbcbf5801c28865c0bfab3cda45302c8474a66Keith Whitwell *
6020fbcbf5801c28865c0bfab3cda45302c8474a66Keith Whitwell * This should really be part of the compiled shader.
6120fbcbf5801c28865c0bfab3cda45302c8474a66Keith Whitwell */
6220fbcbf5801c28865c0bfab3cda45302c8474a66Keith Whitwellvoid
6320fbcbf5801c28865c0bfab3cda45302c8474a66Keith Whitwellsp_setup_pos_vector(const struct tgsi_interp_coef *coef,
6420fbcbf5801c28865c0bfab3cda45302c8474a66Keith Whitwell		    float x, float y,
6520fbcbf5801c28865c0bfab3cda45302c8474a66Keith Whitwell		    struct tgsi_exec_vector *quadpos)
6620fbcbf5801c28865c0bfab3cda45302c8474a66Keith Whitwell{
6720fbcbf5801c28865c0bfab3cda45302c8474a66Keith Whitwell   uint chan;
6820fbcbf5801c28865c0bfab3cda45302c8474a66Keith Whitwell   /* do X */
6920fbcbf5801c28865c0bfab3cda45302c8474a66Keith Whitwell   quadpos->xyzw[0].f[0] = x;
7020fbcbf5801c28865c0bfab3cda45302c8474a66Keith Whitwell   quadpos->xyzw[0].f[1] = x + 1;
7120fbcbf5801c28865c0bfab3cda45302c8474a66Keith Whitwell   quadpos->xyzw[0].f[2] = x;
7220fbcbf5801c28865c0bfab3cda45302c8474a66Keith Whitwell   quadpos->xyzw[0].f[3] = x + 1;
7320fbcbf5801c28865c0bfab3cda45302c8474a66Keith Whitwell
7420fbcbf5801c28865c0bfab3cda45302c8474a66Keith Whitwell   /* do Y */
7520fbcbf5801c28865c0bfab3cda45302c8474a66Keith Whitwell   quadpos->xyzw[1].f[0] = y;
7620fbcbf5801c28865c0bfab3cda45302c8474a66Keith Whitwell   quadpos->xyzw[1].f[1] = y;
7720fbcbf5801c28865c0bfab3cda45302c8474a66Keith Whitwell   quadpos->xyzw[1].f[2] = y + 1;
7820fbcbf5801c28865c0bfab3cda45302c8474a66Keith Whitwell   quadpos->xyzw[1].f[3] = y + 1;
7920fbcbf5801c28865c0bfab3cda45302c8474a66Keith Whitwell
8020fbcbf5801c28865c0bfab3cda45302c8474a66Keith Whitwell   /* do Z and W for all fragments in the quad */
8120fbcbf5801c28865c0bfab3cda45302c8474a66Keith Whitwell   for (chan = 2; chan < 4; chan++) {
8220fbcbf5801c28865c0bfab3cda45302c8474a66Keith Whitwell      const float dadx = coef->dadx[chan];
8320fbcbf5801c28865c0bfab3cda45302c8474a66Keith Whitwell      const float dady = coef->dady[chan];
8420fbcbf5801c28865c0bfab3cda45302c8474a66Keith Whitwell      const float a0 = coef->a0[chan] + dadx * x + dady * y;
8520fbcbf5801c28865c0bfab3cda45302c8474a66Keith Whitwell      quadpos->xyzw[chan].f[0] = a0;
8620fbcbf5801c28865c0bfab3cda45302c8474a66Keith Whitwell      quadpos->xyzw[chan].f[1] = a0 + dadx;
8720fbcbf5801c28865c0bfab3cda45302c8474a66Keith Whitwell      quadpos->xyzw[chan].f[2] = a0 + dady;
8820fbcbf5801c28865c0bfab3cda45302c8474a66Keith Whitwell      quadpos->xyzw[chan].f[3] = a0 + dadx + dady;
8920fbcbf5801c28865c0bfab3cda45302c8474a66Keith Whitwell   }
9020fbcbf5801c28865c0bfab3cda45302c8474a66Keith Whitwell}
9120fbcbf5801c28865c0bfab3cda45302c8474a66Keith Whitwell
92c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell
93c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwellstatic void
94e8de5c70e3370e9112a5facc870075eea60c4c46José Fonsecaexec_prepare( const struct sp_fragment_shader *base,
95c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell	      struct tgsi_exec_machine *machine,
96c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell	      struct tgsi_sampler *samplers )
97c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell{
9805aeb92a092c26e7773beb95692fc72e70a40e56Brian Paul   struct sp_exec_fragment_shader *spefs =
9905aeb92a092c26e7773beb95692fc72e70a40e56Brian Paul      sp_exec_fragment_shader(base);
10005aeb92a092c26e7773beb95692fc72e70a40e56Brian Paul
10105aeb92a092c26e7773beb95692fc72e70a40e56Brian Paul   /*
10205aeb92a092c26e7773beb95692fc72e70a40e56Brian Paul    * Bind tokens/shader to the interpreter's machine state.
10305aeb92a092c26e7773beb95692fc72e70a40e56Brian Paul    * Avoid redundant binding.
10405aeb92a092c26e7773beb95692fc72e70a40e56Brian Paul    */
10505aeb92a092c26e7773beb95692fc72e70a40e56Brian Paul   if (spefs->machine_tokens != base->shader.tokens) {
10605aeb92a092c26e7773beb95692fc72e70a40e56Brian Paul      tgsi_exec_machine_bind_shader( machine,
10705aeb92a092c26e7773beb95692fc72e70a40e56Brian Paul                                     base->shader.tokens,
10805aeb92a092c26e7773beb95692fc72e70a40e56Brian Paul                                     PIPE_MAX_SAMPLERS,
10905aeb92a092c26e7773beb95692fc72e70a40e56Brian Paul                                     samplers );
11005aeb92a092c26e7773beb95692fc72e70a40e56Brian Paul      spefs->machine_tokens = base->shader.tokens;
11105aeb92a092c26e7773beb95692fc72e70a40e56Brian Paul   }
112c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell}
113c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell
114c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell
115c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell
116c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell
117c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell/* TODO: hide the machine struct in here somewhere, remove from this
118c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell * interface:
119c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell */
120c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwellstatic unsigned
121e8de5c70e3370e9112a5facc870075eea60c4c46José Fonsecaexec_run( const struct sp_fragment_shader *base,
122c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell	  struct tgsi_exec_machine *machine,
123c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell	  struct quad_header *quad )
124c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell{
125c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell
126c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell   /* Compute X, Y, Z, W vals for this quad */
127c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell   sp_setup_pos_vector(quad->posCoef,
12801f9e5120395f88bba8321e8639cac0bb9c85296Michal Krol		       (float)quad->input.x0, (float)quad->input.y0,
129c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell		       &machine->QuadPos);
130c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell
131c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell   return tgsi_exec_machine_run( machine );
132c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell}
133c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell
134c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell
135c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell
136c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwellstatic void
137c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwellexec_delete( struct sp_fragment_shader *base )
138c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell{
1399671f7ae476cadb46f9f8f9d0363f24aabaf9f87Brian Paul   FREE((void *) base->shader.tokens);
140c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell   FREE(base);
141c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell}
142c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell
143c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell
144c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell
145c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell
146c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell
147c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwellstruct sp_fragment_shader *
148c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwellsoftpipe_create_fs_exec(struct softpipe_context *softpipe,
149c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell			const struct pipe_shader_state *templ)
150c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell{
151c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell   struct sp_exec_fragment_shader *shader;
152c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell
153c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell   /* Decide whether we'll be codegenerating this shader and if so do
154c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell    * that now.
155c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell    */
156c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell
157c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell   shader = CALLOC_STRUCT(sp_exec_fragment_shader);
158c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell   if (!shader)
159c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell      return NULL;
160c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell
1619671f7ae476cadb46f9f8f9d0363f24aabaf9f87Brian Paul   /* we need to keep a local copy of the tokens */
1629671f7ae476cadb46f9f8f9d0363f24aabaf9f87Brian Paul   shader->base.shader.tokens = tgsi_dup_tokens(templ->tokens);
163c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell   shader->base.prepare = exec_prepare;
164c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell   shader->base.run = exec_run;
165c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell   shader->base.delete = exec_delete;
166c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell
167c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell   return &shader->base;
168c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell}
169c04a7f8929d674971a472ffa4d3a31200c22aa5aKeith Whitwell
170