cell: add -DDEBUG flag, fixes to Cell Makefiles
[mesa.git] / src / gallium / drivers / cell / ppu / cell_state_derived.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "util/u_memory.h"
29 #include "pipe/p_shader_tokens.h"
30 #include "draw/draw_context.h"
31 #include "draw/draw_vertex.h"
32 #include "cell_context.h"
33 #include "cell_batch.h"
34 #include "cell_state.h"
35 #include "cell_state_emit.h"
36
37
38 static int
39 find_vs_output(const struct cell_vertex_shader_state *vs,
40 uint semantic_name,
41 uint semantic_index)
42 {
43 uint i;
44 for (i = 0; i < vs->info.num_outputs; i++) {
45 if (vs->info.output_semantic_name[i] == semantic_name &&
46 vs->info.output_semantic_index[i] == semantic_index)
47 return i;
48 }
49 return -1;
50 }
51
52
53 /**
54 * Determine how to map vertex program outputs to fragment program inputs.
55 * Basically, this will be used when computing the triangle interpolation
56 * coefficients from the post-transform vertex attributes.
57 */
58 static void
59 calculate_vertex_layout( struct cell_context *cell )
60 {
61 const struct cell_vertex_shader_state *vs = cell->vs;
62 const struct cell_fragment_shader_state *fs = cell->fs;
63 const enum interp_mode colorInterp
64 = cell->rasterizer->flatshade ? INTERP_CONSTANT : INTERP_LINEAR;
65 struct vertex_info *vinfo = &cell->vertex_info;
66 uint i;
67 int src;
68
69 #if 0
70 if (cell->vbuf) {
71 /* if using the post-transform vertex buffer, tell draw_vbuf to
72 * simply emit the whole post-xform vertex as-is:
73 */
74 struct vertex_info *vinfo_vbuf = &cell->vertex_info_vbuf;
75 vinfo_vbuf->num_attribs = 0;
76 draw_emit_vertex_attr(vinfo_vbuf, EMIT_ALL, INTERP_NONE, 0);
77 vinfo_vbuf->size = 4 * vs->num_outputs + sizeof(struct vertex_header)/4;
78 }
79 #endif
80
81 /* reset vinfo */
82 vinfo->num_attribs = 0;
83
84 /* we always want to emit vertex pos */
85 src = find_vs_output(vs, TGSI_SEMANTIC_POSITION, 0);
86 assert(src >= 0);
87 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_POS, src);
88
89
90 /*
91 * Loop over fragment shader inputs, searching for the matching output
92 * from the vertex shader.
93 */
94 for (i = 0; i < fs->info.num_inputs; i++) {
95 switch (fs->info.input_semantic_name[i]) {
96 case TGSI_SEMANTIC_POSITION:
97 /* already done above */
98 break;
99
100 case TGSI_SEMANTIC_COLOR:
101 src = find_vs_output(vs, TGSI_SEMANTIC_COLOR,
102 fs->info.input_semantic_index[i]);
103 assert(src >= 0);
104 draw_emit_vertex_attr(vinfo, EMIT_4F, colorInterp, src);
105 break;
106
107 case TGSI_SEMANTIC_FOG:
108 src = find_vs_output(vs, TGSI_SEMANTIC_FOG, 0);
109 #if 1
110 if (src < 0) /* XXX temp hack, try demos/fogcoord.c with this */
111 src = 0;
112 #endif
113 assert(src >= 0);
114 draw_emit_vertex_attr(vinfo, EMIT_1F, INTERP_PERSPECTIVE, src);
115 break;
116
117 case TGSI_SEMANTIC_GENERIC:
118 /* this includes texcoords and varying vars */
119 src = find_vs_output(vs, TGSI_SEMANTIC_GENERIC,
120 fs->info.input_semantic_index[i]);
121 assert(src >= 0);
122 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_PERSPECTIVE, src);
123 break;
124
125 default:
126 assert(0);
127 }
128 }
129
130 draw_compute_vertex_size(vinfo);
131
132 /* XXX only signal this if format really changes */
133 cell->dirty |= CELL_NEW_VERTEX_INFO;
134 }
135
136
137 #if 0
138 /**
139 * Recompute cliprect from scissor bounds, scissor enable and surface size.
140 */
141 static void
142 compute_cliprect(struct cell_context *sp)
143 {
144 uint surfWidth = sp->framebuffer.width;
145 uint surfHeight = sp->framebuffer.height;
146
147 if (sp->rasterizer->scissor) {
148 /* clip to scissor rect */
149 sp->cliprect.minx = MAX2(sp->scissor.minx, 0);
150 sp->cliprect.miny = MAX2(sp->scissor.miny, 0);
151 sp->cliprect.maxx = MIN2(sp->scissor.maxx, surfWidth);
152 sp->cliprect.maxy = MIN2(sp->scissor.maxy, surfHeight);
153 }
154 else {
155 /* clip to surface bounds */
156 sp->cliprect.minx = 0;
157 sp->cliprect.miny = 0;
158 sp->cliprect.maxx = surfWidth;
159 sp->cliprect.maxy = surfHeight;
160 }
161 }
162 #endif
163
164
165
166 void cell_update_derived( struct cell_context *cell )
167 {
168 if (cell->dirty & (CELL_NEW_RASTERIZER |
169 CELL_NEW_FS |
170 CELL_NEW_VS))
171 calculate_vertex_layout( cell );
172
173 #if 0
174 if (cell->dirty & (CELL_NEW_SCISSOR |
175 CELL_NEW_DEPTH_STENCIL_ALPHA |
176 CELL_NEW_FRAMEBUFFER))
177 compute_cliprect(cell);
178 #endif
179
180 cell_emit_state(cell);
181
182 cell->dirty = 0;
183 }