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