summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/i965simple/brw_sf.c
blob: 96f8fb87a3d493c8e5c9fbefb8bb4b5ab7f1861f (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
 Copyright (C) Intel Corp.  2006.  All Rights Reserved.
 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
 develop this 3D driver.

 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, sublicense, 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 NONINFRINGEMENT.
 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) 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.

 **********************************************************************/
 /*
  * Authors:
  *   Keith Whitwell <keith@tungstengraphics.com>
  */


#include "brw_defines.h"
#include "brw_context.h"
#include "brw_eu.h"
#include "brw_util.h"
#include "brw_sf.h"
#include "brw_state.h"
#include "tgsi/util/tgsi_parse.h"


static void compile_sf_prog( struct brw_context *brw,
			     struct brw_sf_prog_key *key )
{
   struct brw_sf_compile c;
   const unsigned *program;
   unsigned program_size;

   memset(&c, 0, sizeof(c));

   /* Begin the compilation:
    */
   brw_init_compile(&c.func);

   c.key = *key;


   c.nr_attrs = c.key.vp_output_count;
   c.nr_attr_regs = (c.nr_attrs+1)/2;

   c.nr_setup_attrs = c.key.fp_input_count + 1; /* +1 for position */
   c.nr_setup_regs = (c.nr_setup_attrs+1)/2;

   c.prog_data.urb_read_length = c.nr_attr_regs;
   c.prog_data.urb_entry_size = c.nr_setup_regs * 2;


   /* Which primitive?  Or all three?
    */
   switch (key->primitive) {
   case SF_TRIANGLES:
      c.nr_verts = 3;
      brw_emit_tri_setup( &c );
      break;
   case SF_LINES:
      c.nr_verts = 2;
      brw_emit_line_setup( &c );
      break;
   case SF_POINTS:
      c.nr_verts = 1;
      brw_emit_point_setup( &c );
      break;

   case SF_UNFILLED_TRIS:
   default:
      assert(0);
      return;
   }



   /* get the program
    */
   program = brw_get_program(&c.func, &program_size);

   /* Upload
    */
   brw->sf.prog_gs_offset = brw_upload_cache( &brw->cache[BRW_SF_PROG],
					      &c.key,
					      sizeof(c.key),
					      program,
					      program_size,
					      &c.prog_data,
					      &brw->sf.prog_data );
}


static boolean search_cache( struct brw_context *brw,
			       struct brw_sf_prog_key *key )
{
   return brw_search_cache(&brw->cache[BRW_SF_PROG],
			   key, sizeof(*key),
			   &brw->sf.prog_data,
			   &brw->sf.prog_gs_offset);
}


/* Calculate interpolants for triangle and line rasterization.
 */
static void upload_sf_prog( struct brw_context *brw )
{
   const struct brw_fragment_program *fs = brw->attribs.FragmentProgram;
   struct brw_sf_prog_key key;
   struct tgsi_parse_context parse;
   int i, done = 0;


   memset(&key, 0, sizeof(key));

   /* Populate the key, noting state dependencies:
    */
   /* CACHE_NEW_VS_PROG */
   key.vp_output_count = brw->vs.prog_data->outputs_written;

   /* BRW_NEW_FS */
   key.fp_input_count = brw->attribs.FragmentProgram->info.file_max[TGSI_FILE_INPUT] + 1;


   /* BRW_NEW_REDUCED_PRIMITIVE */
   switch (brw->reduced_primitive) {
   case PIPE_PRIM_TRIANGLES:
//      if (key.attrs & (1<<VERT_RESULT_EDGE))
//	 key.primitive = SF_UNFILLED_TRIS;
//      else
      key.primitive = SF_TRIANGLES;
      break;
   case PIPE_PRIM_LINES:
      key.primitive = SF_LINES;
      break;
   case PIPE_PRIM_POINTS:
      key.primitive = SF_POINTS;
      break;
   }



   /* Scan fp inputs to figure out what interpolation modes are
    * required for each incoming vp output.  There is an assumption
    * that the state tracker makes sure there is a 1:1 linkage between
    * these sets of attributes (XXX: position??)
    */
   tgsi_parse_init( &parse, fs->program.tokens );
   while( !done &&
	  !tgsi_parse_end_of_tokens( &parse ) ) 
   {
      tgsi_parse_token( &parse );

      switch( parse.FullToken.Token.Type ) {
      case TGSI_TOKEN_TYPE_DECLARATION:
	 if (parse.FullToken.FullDeclaration.Declaration.File == TGSI_FILE_INPUT) 
	 {
	    int first = parse.FullToken.FullDeclaration.DeclarationRange.First;
	    int last = parse.FullToken.FullDeclaration.DeclarationRange.Last;
	    int interp_mode = parse.FullToken.FullDeclaration.Declaration.Interpolate;
	    //int semantic = parse.FullToken.FullDeclaration.Semantic.SemanticName;
	    //int semantic_index = parse.FullToken.FullDeclaration.Semantic.SemanticIndex;

	    debug_printf("fs input %d..%d interp mode %d\n", first, last, interp_mode);
	    
	    switch (interp_mode) {
	    case TGSI_INTERPOLATE_CONSTANT:
	       for (i = first; i <= last; i++) 
		  key.const_mask |= (1 << i);
	       break;
	    case TGSI_INTERPOLATE_LINEAR:
	       for (i = first; i <= last; i++) 
		  key.linear_mask |= (1 << i);
	       break;
	    case TGSI_INTERPOLATE_PERSPECTIVE:
	       for (i = first; i <= last; i++) 
		  key.persp_mask |= (1 << i);
	       break;
	    default:
	       break;
	    }

	    /* Also need stuff for flat shading, twosided color.
	     */

	 }
	 break;
      default:
	 done = 1;
	 break;
      }
   }

   /* Hack: Adjust for position.  Optimize away when not required (ie
    * for perspective interpolation).
    */
   key.persp_mask <<= 1;
   key.linear_mask <<= 1; 
   key.linear_mask |= 1;
   key.const_mask <<= 1;

   debug_printf("key.persp_mask: %x\n", key.persp_mask);
   debug_printf("key.linear_mask: %x\n", key.linear_mask);
   debug_printf("key.const_mask: %x\n", key.const_mask);


//   key.do_point_sprite = brw->attribs.Point->PointSprite;
//   key.SpriteOrigin = brw->attribs.Point->SpriteOrigin;

//   key.do_flat_shading = (brw->attribs.Raster->flatshade);
//   key.do_twoside_color = (brw->attribs.Light->Enabled && brw->attribs.Light->Model.TwoSide);

//   if (key.do_twoside_color)
//      key.frontface_ccw = (brw->attribs.Polygon->FrontFace == GL_CCW);


   if (!search_cache(brw, &key))
      compile_sf_prog( brw, &key );
}


const struct brw_tracked_state brw_sf_prog = {
   .dirty = {
      .brw   = (BRW_NEW_RASTERIZER |
		BRW_NEW_REDUCED_PRIMITIVE |
		BRW_NEW_VS |
		BRW_NEW_FS),
      .cache = 0,
   },
   .update = upload_sf_prog
};



#if 0
/* Build a struct like the one we'd like the state tracker to pass to
 * us.
 */
static void update_sf_linkage( struct brw_context *brw )
{
   const struct brw_vertex_program *vs = brw->attribs.VertexProgram;
   const struct brw_fragment_program *fs = brw->attribs.FragmentProgram;
   struct pipe_setup_linkage state;
   struct tgsi_parse_context parse;

   int i, j;
   int nr_vp_outputs = 0;
   int done = 0;

   struct { 
      unsigned semantic:8;
      unsigned semantic_index:16;
   } fp_semantic[32], vp_semantic[32];

   memset(&state, 0, sizeof(state));

   state.fp_input_count = 0;



   


   assert(state.fp_input_count == fs->program.num_inputs);

      
   /* Then scan vp outputs
    */
   done = 0;
   tgsi_parse_init( &parse, vs->program.tokens );
   while( !done &&
	  !tgsi_parse_end_of_tokens( &parse ) ) 
   {
      tgsi_parse_token( &parse );

      switch( parse.FullToken.Token.Type ) {
      case TGSI_TOKEN_TYPE_DECLARATION:
	 if (parse.FullToken.FullDeclaration.Declaration.File == TGSI_FILE_INPUT) 
	 {
	    int first = parse.FullToken.FullDeclaration.DeclarationRange.First;
	    int last = parse.FullToken.FullDeclaration.DeclarationRange.Last;

	    for (i = first; i < last; i++) {
	       vp_semantic[i].semantic = 
		  parse.FullToken.FullDeclaration.Semantic.SemanticName;
	       vp_semantic[i].semantic_index = 
		  parse.FullToken.FullDeclaration.Semantic.SemanticIndex;
	    }
	    
	    assert(last > nr_vp_outputs);
	    nr_vp_outputs = last;
	 }
	 break;
      default:
	 done = 1;
	 break;
      }
   }


   /* Now match based on semantic information.
    */
   for (i = 0; i< state.fp_input_count; i++) {
      for (j = 0; j < nr_vp_outputs; j++) {
	 if (fp_semantic[i].semantic == vp_semantic[j].semantic &&
	     fp_semantic[i].semantic_index == vp_semantic[j].semantic_index) {
	    state.fp_input[i].vp_output = j;
	 }
      }
      if (fp_semantic[i].semantic == TGSI_SEMANTIC_COLOR) {
	 for (j = 0; j < nr_vp_outputs; j++) {
	    if (TGSI_SEMANTIC_BCOLOR == vp_semantic[j].semantic &&
		fp_semantic[i].semantic_index == vp_semantic[j].semantic_index) {
	       state.fp_input[i].bf_vp_output = j;
	    }
	 }
      }
   }

   if (memcmp(&brw->sf.linkage, &state, sizeof(state)) != 0) {
      brw->sf.linkage = state;
      brw->state.dirty.brw |= BRW_NEW_SF_LINKAGE;
   }
}


const struct brw_tracked_state brw_sf_linkage = {
   .dirty = {
      .brw   = (BRW_NEW_VS |
		BRW_NEW_FS),
      .cache = 0,
   },
   .update = update_sf_linkage
};


#endif