Merge commit 'origin/gallium-master-merge'
[mesa.git] / src / gallium / drivers / cell / ppu / cell_render.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 /**
29 * \brief Last stage of 'draw' pipeline: send tris to SPUs.
30 * \author Brian Paul
31 */
32
33 #include "cell_context.h"
34 #include "cell_render.h"
35 #include "cell_spu.h"
36 #include "util/u_memory.h"
37 #include "draw/draw_private.h"
38
39
40 struct render_stage {
41 struct draw_stage stage; /**< This must be first (base class) */
42
43 struct cell_context *cell;
44 };
45
46
47 static INLINE struct render_stage *
48 render_stage(struct draw_stage *stage)
49 {
50 return (struct render_stage *) stage;
51 }
52
53
54 static void render_begin( struct draw_stage *stage )
55 {
56 #if 0
57 struct render_stage *render = render_stage(stage);
58 struct cell_context *sp = render->cell;
59 const struct pipe_shader_state *fs = &render->cell->fs->shader;
60 render->quad.nr_attrs = render->cell->nr_frag_attrs;
61
62 render->firstFpInput = fs->input_semantic_name[0];
63
64 sp->quad.first->begin(sp->quad.first);
65 #endif
66 }
67
68
69 static void render_end( struct draw_stage *stage )
70 {
71 }
72
73
74 static void reset_stipple_counter( struct draw_stage *stage )
75 {
76 struct render_stage *render = render_stage(stage);
77 /*render->cell->line_stipple_counter = 0;*/
78 }
79
80
81 static void
82 render_point(struct draw_stage *stage, struct prim_header *prim)
83 {
84 }
85
86
87 static void
88 render_line(struct draw_stage *stage, struct prim_header *prim)
89 {
90 }
91
92
93 /** Write a vertex into the prim buffer */
94 static void
95 save_vertex(struct cell_prim_buffer *buf, uint pos,
96 const struct vertex_header *vert)
97 {
98 uint attr, j;
99
100 for (attr = 0; attr < 2; attr++) {
101 for (j = 0; j < 4; j++) {
102 buf->vertex[pos][attr][j] = vert->data[attr][j];
103 }
104 }
105
106 /* update bounding box */
107 if (vert->data[0][0] < buf->xmin)
108 buf->xmin = vert->data[0][0];
109 if (vert->data[0][0] > buf->xmax)
110 buf->xmax = vert->data[0][0];
111 if (vert->data[0][1] < buf->ymin)
112 buf->ymin = vert->data[0][1];
113 if (vert->data[0][1] > buf->ymax)
114 buf->ymax = vert->data[0][1];
115 }
116
117
118 static void
119 render_tri(struct draw_stage *stage, struct prim_header *prim)
120 {
121 struct render_stage *rs = render_stage(stage);
122 struct cell_context *cell = rs->cell;
123 struct cell_prim_buffer *buf = &cell->prim_buffer;
124 uint i;
125
126 if (buf->num_verts + 3 > CELL_MAX_VERTS) {
127 cell_flush_prim_buffer(cell);
128 }
129
130 i = buf->num_verts;
131 assert(i+2 <= CELL_MAX_VERTS);
132 save_vertex(buf, i+0, prim->v[0]);
133 save_vertex(buf, i+1, prim->v[1]);
134 save_vertex(buf, i+2, prim->v[2]);
135 buf->num_verts += 3;
136 }
137
138
139 /**
140 * Send the a RENDER command to all SPUs to have them render the prims
141 * in the current prim_buffer.
142 */
143 void
144 cell_flush_prim_buffer(struct cell_context *cell)
145 {
146 uint i;
147
148 if (cell->prim_buffer.num_verts == 0)
149 return;
150
151 for (i = 0; i < cell->num_spus; i++) {
152 struct cell_command_render *render = &cell_global.command[i].render;
153 render->prim_type = PIPE_PRIM_TRIANGLES;
154 render->num_verts = cell->prim_buffer.num_verts;
155 render->front_winding = cell->rasterizer->front_winding;
156 render->vertex_size = cell->vertex_info->size * 4;
157 render->xmin = cell->prim_buffer.xmin;
158 render->ymin = cell->prim_buffer.ymin;
159 render->xmax = cell->prim_buffer.xmax;
160 render->ymax = cell->prim_buffer.ymax;
161 render->vertex_data = &cell->prim_buffer.vertex;
162 ASSERT_ALIGN16(render->vertex_data);
163 send_mbox_message(cell_global.spe_contexts[i], CELL_CMD_RENDER);
164 }
165
166 cell->prim_buffer.num_verts = 0;
167
168 cell->prim_buffer.xmin = 1e100;
169 cell->prim_buffer.ymin = 1e100;
170 cell->prim_buffer.xmax = -1e100;
171 cell->prim_buffer.ymax = -1e100;
172
173 /* XXX temporary, need to double-buffer the prim buffer until we get
174 * a real command buffer/list system.
175 */
176 cell_flush(&cell->pipe, 0x0);
177 }
178
179
180
181 static void render_destroy( struct draw_stage *stage )
182 {
183 FREE( stage );
184 }
185
186
187 /**
188 * Create a new draw/render stage. This will be plugged into the
189 * draw module as the last pipeline stage.
190 */
191 struct draw_stage *cell_draw_render_stage( struct cell_context *cell )
192 {
193 struct render_stage *render = CALLOC_STRUCT(render_stage);
194
195 render->cell = cell;
196 render->stage.draw = cell->draw;
197 render->stage.begin = render_begin;
198 render->stage.point = render_point;
199 render->stage.line = render_line;
200 render->stage.tri = render_tri;
201 render->stage.end = render_end;
202 render->stage.reset_stipple_counter = reset_stipple_counter;
203 render->stage.destroy = render_destroy;
204
205 /*
206 render->quad.coef = render->coef;
207 render->quad.posCoef = &render->posCoef;
208 */
209
210 return &render->stage;
211 }