Merge branch 'mesa_7_6_branch' of git+ssh://agd5f@git.freedesktop.org/git/mesa/mesa
[mesa.git] / src / mesa / state_tracker / st_cb_accum.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 * Authors:
30 * Brian Paul
31 */
32
33 #include "main/imports.h"
34 #include "main/image.h"
35 #include "main/macros.h"
36
37 #include "st_debug.h"
38 #include "st_context.h"
39 #include "st_cb_accum.h"
40 #include "st_cb_fbo.h"
41 #include "st_draw.h"
42 #include "st_public.h"
43 #include "st_format.h"
44 #include "st_texture.h"
45 #include "st_inlines.h"
46 #include "pipe/p_context.h"
47 #include "pipe/p_defines.h"
48 #include "pipe/p_inlines.h"
49 #include "util/u_tile.h"
50
51
52 /**
53 * For hardware that supports deep color buffers, we could accelerate
54 * most/all the accum operations with blending/texturing.
55 * For now, just use the get/put_tile() functions and do things in software.
56 */
57
58
59 void
60 st_clear_accum_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
61 {
62 struct st_renderbuffer *acc_strb = st_renderbuffer(rb);
63 const GLint xpos = ctx->DrawBuffer->_Xmin;
64 const GLint ypos = ctx->DrawBuffer->_Ymin;
65 const GLint width = ctx->DrawBuffer->_Xmax - xpos;
66 const GLint height = ctx->DrawBuffer->_Ymax - ypos;
67 size_t stride = acc_strb->stride;
68 GLubyte *data = acc_strb->data;
69
70 if(!data)
71 return;
72
73 switch (acc_strb->format) {
74 case PIPE_FORMAT_R16G16B16A16_SNORM:
75 {
76 GLshort r = FLOAT_TO_SHORT(ctx->Accum.ClearColor[0]);
77 GLshort g = FLOAT_TO_SHORT(ctx->Accum.ClearColor[1]);
78 GLshort b = FLOAT_TO_SHORT(ctx->Accum.ClearColor[2]);
79 GLshort a = FLOAT_TO_SHORT(ctx->Accum.ClearColor[3]);
80 int i, j;
81 for (i = 0; i < height; i++) {
82 GLshort *dst = (GLshort *) (data + (ypos + i) * stride + xpos * 8);
83 for (j = 0; j < width; j++) {
84 dst[0] = r;
85 dst[1] = g;
86 dst[2] = b;
87 dst[3] = a;
88 dst += 4;
89 }
90 }
91 }
92 break;
93 default:
94 _mesa_problem(ctx, "unexpected format in st_clear_accum_buffer()");
95 }
96 }
97
98
99 /** For ADD/MULT */
100 static void
101 accum_mad(GLcontext *ctx, GLfloat scale, GLfloat bias,
102 GLint xpos, GLint ypos, GLint width, GLint height,
103 struct st_renderbuffer *acc_strb)
104 {
105 size_t stride = acc_strb->stride;
106 GLubyte *data = acc_strb->data;
107
108 switch (acc_strb->format) {
109 case PIPE_FORMAT_R16G16B16A16_SNORM:
110 {
111 int i, j;
112 for (i = 0; i < height; i++) {
113 GLshort *acc = (GLshort *) (data + (ypos + i) * stride + xpos * 8);
114 for (j = 0; j < width * 4; j++) {
115 float val = SHORT_TO_FLOAT(*acc) * scale + bias;
116 *acc++ = FLOAT_TO_SHORT(val);
117 }
118 }
119 }
120 break;
121 default:
122 _mesa_problem(NULL, "unexpected format in st_clear_accum_buffer()");
123 }
124 }
125
126
127 static void
128 accum_accum(struct st_context *st, GLfloat value,
129 GLint xpos, GLint ypos, GLint width, GLint height,
130 struct st_renderbuffer *acc_strb,
131 struct st_renderbuffer *color_strb)
132 {
133 struct pipe_context *pipe = st->pipe;
134 struct pipe_screen *screen = pipe->screen;
135 struct pipe_transfer *color_trans;
136 size_t stride = acc_strb->stride;
137 GLubyte *data = acc_strb->data;
138 GLfloat *buf;
139
140 if (ST_DEBUG & DEBUG_FALLBACK)
141 debug_printf("%s: fallback processing\n", __FUNCTION__);
142
143 color_trans = st_cond_flush_get_tex_transfer(st, color_strb->texture,
144 0, 0, 0,
145 PIPE_TRANSFER_READ, xpos, ypos,
146 width, height);
147
148 buf = (GLfloat *) _mesa_malloc(width * height * 4 * sizeof(GLfloat));
149
150 pipe_get_tile_rgba(color_trans, 0, 0, width, height, buf);
151
152 switch (acc_strb->format) {
153 case PIPE_FORMAT_R16G16B16A16_SNORM:
154 {
155 const GLfloat *color = buf;
156 int i, j;
157 for (i = 0; i < height; i++) {
158 GLshort *acc = (GLshort *) (data + (ypos + i) * stride + xpos * 8);
159 for (j = 0; j < width * 4; j++) {
160 float val = *color++ * value;
161 *acc++ += FLOAT_TO_SHORT(val);
162 }
163 }
164 }
165 break;
166 default:
167 _mesa_problem(NULL, "unexpected format in st_clear_accum_buffer()");
168 }
169
170 _mesa_free(buf);
171 screen->tex_transfer_destroy(color_trans);
172 }
173
174
175 static void
176 accum_load(struct st_context *st, GLfloat value,
177 GLint xpos, GLint ypos, GLint width, GLint height,
178 struct st_renderbuffer *acc_strb,
179 struct st_renderbuffer *color_strb)
180 {
181 struct pipe_context *pipe = st->pipe;
182 struct pipe_screen *screen = pipe->screen;
183 struct pipe_transfer *color_trans;
184 size_t stride = acc_strb->stride;
185 GLubyte *data = acc_strb->data;
186 GLfloat *buf;
187
188
189 if (ST_DEBUG & DEBUG_FALLBACK)
190 debug_printf("%s: fallback processing\n", __FUNCTION__);
191
192 color_trans = st_cond_flush_get_tex_transfer(st, color_strb->texture,
193 0, 0, 0,
194 PIPE_TRANSFER_READ, xpos, ypos,
195 width, height);
196
197 buf = (GLfloat *) _mesa_malloc(width * height * 4 * sizeof(GLfloat));
198
199 pipe_get_tile_rgba(color_trans, 0, 0, width, height, buf);
200
201 switch (acc_strb->format) {
202 case PIPE_FORMAT_R16G16B16A16_SNORM:
203 {
204 const GLfloat *color = buf;
205 int i, j;
206 for (i = 0; i < height; i++) {
207 GLshort *acc = (GLshort *) (data + (ypos + i) * stride + xpos * 8);
208 for (j = 0; j < width * 4; j++) {
209 float val = *color++ * value;
210 *acc++ = FLOAT_TO_SHORT(val);
211 }
212 }
213 }
214 break;
215 default:
216 _mesa_problem(NULL, "unexpected format in st_clear_accum_buffer()");
217 }
218
219 _mesa_free(buf);
220 screen->tex_transfer_destroy(color_trans);
221 }
222
223
224 static void
225 accum_return(GLcontext *ctx, GLfloat value,
226 GLint xpos, GLint ypos, GLint width, GLint height,
227 struct st_renderbuffer *acc_strb,
228 struct st_renderbuffer *color_strb)
229 {
230 struct pipe_context *pipe = ctx->st->pipe;
231 struct pipe_screen *screen = pipe->screen;
232 const GLubyte *colormask = ctx->Color.ColorMask;
233 enum pipe_transfer_usage usage;
234 struct pipe_transfer *color_trans;
235 size_t stride = acc_strb->stride;
236 const GLubyte *data = acc_strb->data;
237 GLfloat *buf;
238
239 if (ST_DEBUG & DEBUG_FALLBACK)
240 debug_printf("%s: fallback processing\n", __FUNCTION__);
241
242 buf = (GLfloat *) _mesa_malloc(width * height * 4 * sizeof(GLfloat));
243
244 if (!colormask[0] || !colormask[1] || !colormask[2] || !colormask[3])
245 usage = PIPE_TRANSFER_READ_WRITE;
246 else
247 usage = PIPE_TRANSFER_WRITE;
248
249 color_trans = st_cond_flush_get_tex_transfer(st_context(ctx),
250 color_strb->texture, 0, 0, 0,
251 usage,
252 xpos, ypos,
253 width, height);
254
255 if (usage & PIPE_TRANSFER_READ)
256 pipe_get_tile_rgba(color_trans, 0, 0, width, height, buf);
257
258 switch (acc_strb->format) {
259 case PIPE_FORMAT_R16G16B16A16_SNORM:
260 {
261 GLfloat *color = buf;
262 int i, j, ch;
263 for (i = 0; i < height; i++) {
264 const GLshort *acc = (const GLshort *) (data + (ypos + i) * stride + xpos * 8);
265 for (j = 0; j < width; j++) {
266 for (ch = 0; ch < 4; ch++) {
267 if (colormask[ch]) {
268 GLfloat val = SHORT_TO_FLOAT(*acc * value);
269 *color = CLAMP(val, 0.0f, 1.0f);
270 }
271 else {
272 /* No change */
273 }
274 ++acc;
275 ++color;
276 }
277 }
278 }
279 }
280 break;
281 default:
282 _mesa_problem(NULL, "unexpected format in st_clear_accum_buffer()");
283 }
284
285 pipe_put_tile_rgba(color_trans, 0, 0, width, height, buf);
286
287 _mesa_free(buf);
288 screen->tex_transfer_destroy(color_trans);
289 }
290
291
292 static void
293 st_Accum(GLcontext *ctx, GLenum op, GLfloat value)
294 {
295 struct st_context *st = ctx->st;
296 struct st_renderbuffer *acc_strb
297 = st_renderbuffer(ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer);
298 struct st_renderbuffer *color_strb
299 = st_renderbuffer(ctx->ReadBuffer->_ColorReadBuffer);
300
301 const GLint xpos = ctx->DrawBuffer->_Xmin;
302 const GLint ypos = ctx->DrawBuffer->_Ymin;
303 const GLint width = ctx->DrawBuffer->_Xmax - xpos;
304 const GLint height = ctx->DrawBuffer->_Ymax - ypos;
305
306 if(!acc_strb->data)
307 return;
308
309 /* make sure color bufs aren't cached */
310 st_flush( st, PIPE_FLUSH_RENDER_CACHE, NULL );
311
312 switch (op) {
313 case GL_ADD:
314 if (value != 0.0F) {
315 accum_mad(ctx, 1.0, value, xpos, ypos, width, height, acc_strb);
316 }
317 break;
318 case GL_MULT:
319 if (value != 1.0F) {
320 accum_mad(ctx, value, 0.0, xpos, ypos, width, height, acc_strb);
321 }
322 break;
323 case GL_ACCUM:
324 if (value != 0.0F) {
325 accum_accum(st, value, xpos, ypos, width, height, acc_strb, color_strb);
326 }
327 break;
328 case GL_LOAD:
329 accum_load(st, value, xpos, ypos, width, height, acc_strb, color_strb);
330 break;
331 case GL_RETURN:
332 accum_return(ctx, value, xpos, ypos, width, height, acc_strb, color_strb);
333 break;
334 default:
335 assert(0);
336 }
337 }
338
339
340
341 void st_init_accum_functions(struct dd_function_table *functions)
342 {
343 functions->Accum = st_Accum;
344 }