Tizen 2.0 Release
[profile/ivi/osmesa.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 /**
39  * Determine how to map vertex program outputs to fragment program inputs.
40  * Basically, this will be used when computing the triangle interpolation
41  * coefficients from the post-transform vertex attributes.
42  */
43 static void
44 calculate_vertex_layout( struct cell_context *cell )
45 {
46    const struct cell_fragment_shader_state *fs = cell->fs;
47    const enum interp_mode colorInterp
48       = cell->rasterizer->flatshade ? INTERP_CONSTANT : INTERP_LINEAR;
49    struct vertex_info *vinfo = &cell->vertex_info;
50    uint i;
51    int src;
52
53 #if 0
54    if (cell->vbuf) {
55       /* if using the post-transform vertex buffer, tell draw_vbuf to
56        * simply emit the whole post-xform vertex as-is:
57        */
58       struct vertex_info *vinfo_vbuf = &cell->vertex_info_vbuf;
59       vinfo_vbuf->num_attribs = 0;
60       draw_emit_vertex_attr(vinfo_vbuf, EMIT_ALL, INTERP_NONE, 0);
61       vinfo_vbuf->size = 4 * vs->num_outputs + sizeof(struct vertex_header)/4;
62    }
63 #endif
64
65    /* reset vinfo */
66    vinfo->num_attribs = 0;
67
68    /* we always want to emit vertex pos */
69    src = draw_find_shader_output(cell->draw, TGSI_SEMANTIC_POSITION, 0);
70    assert(src >= 0);
71    draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_POS, src);
72
73
74    /*
75     * Loop over fragment shader inputs, searching for the matching output
76     * from the vertex shader.
77     */
78    for (i = 0; i < fs->info.num_inputs; i++) {
79       switch (fs->info.input_semantic_name[i]) {
80       case TGSI_SEMANTIC_POSITION:
81          /* already done above */
82          break;
83
84       case TGSI_SEMANTIC_COLOR:
85          src = draw_find_shader_output(cell->draw, TGSI_SEMANTIC_COLOR, 
86                                    fs->info.input_semantic_index[i]);
87          assert(src >= 0);
88          draw_emit_vertex_attr(vinfo, EMIT_4F, colorInterp, src);
89          break;
90
91       case TGSI_SEMANTIC_FOG:
92          src = draw_find_shader_output(cell->draw, TGSI_SEMANTIC_FOG, 0);
93 #if 1
94          if (src < 0) /* XXX temp hack, try demos/fogcoord.c with this */
95             src = 0;
96 #endif
97          assert(src >= 0);
98          draw_emit_vertex_attr(vinfo, EMIT_1F, INTERP_PERSPECTIVE, src);
99          break;
100
101       case TGSI_SEMANTIC_GENERIC:
102          /* this includes texcoords and varying vars */
103          src = draw_find_shader_output(cell->draw, TGSI_SEMANTIC_GENERIC,
104                               fs->info.input_semantic_index[i]);
105          assert(src >= 0);
106          draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_PERSPECTIVE, src);
107          break;
108
109       default:
110          assert(0);
111       }
112    }
113
114    draw_compute_vertex_size(vinfo);
115
116    /* XXX only signal this if format really changes */
117    cell->dirty |= CELL_NEW_VERTEX_INFO;
118 }
119
120
121 #if 0
122 /**
123  * Recompute cliprect from scissor bounds, scissor enable and surface size.
124  */
125 static void
126 compute_cliprect(struct cell_context *sp)
127 {
128    uint surfWidth = sp->framebuffer.width;
129    uint surfHeight = sp->framebuffer.height;
130
131    if (sp->rasterizer->scissor) {
132       /* clip to scissor rect */
133       sp->cliprect.minx = MAX2(sp->scissor.minx, 0);
134       sp->cliprect.miny = MAX2(sp->scissor.miny, 0);
135       sp->cliprect.maxx = MIN2(sp->scissor.maxx, surfWidth);
136       sp->cliprect.maxy = MIN2(sp->scissor.maxy, surfHeight);
137    }
138    else {
139       /* clip to surface bounds */
140       sp->cliprect.minx = 0;
141       sp->cliprect.miny = 0;
142       sp->cliprect.maxx = surfWidth;
143       sp->cliprect.maxy = surfHeight;
144    }
145 }
146 #endif
147
148
149
150 /**
151  * Update derived state, send current state to SPUs prior to rendering.
152  */
153 void cell_update_derived( struct cell_context *cell )
154 {
155    if (cell->dirty & (CELL_NEW_RASTERIZER |
156                       CELL_NEW_FS |
157                       CELL_NEW_VS))
158       calculate_vertex_layout( cell );
159
160 #if 0
161    if (cell->dirty & (CELL_NEW_SCISSOR |
162                       CELL_NEW_DEPTH_STENCIL_ALPHA |
163                       CELL_NEW_FRAMEBUFFER))
164       compute_cliprect(cell);
165 #endif
166
167    cell_emit_state(cell);
168
169    cell->dirty = 0;
170 }