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