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