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