1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
33 #include "main/imports.h"
34 #include "main/image.h"
35 #include "main/macros.h"
36 #include "main/mfeatures.h"
39 #include "st_context.h"
40 #include "st_cb_accum.h"
41 #include "st_cb_fbo.h"
42 #include "st_texture.h"
43 #include "pipe/p_context.h"
44 #include "pipe/p_defines.h"
45 #include "util/u_format.h"
46 #include "util/u_inlines.h"
47 #include "util/u_tile.h"
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.
60 st_clear_accum_buffer(struct gl_context *ctx, struct gl_renderbuffer *rb)
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;
73 switch (acc_strb->format) {
74 case PIPE_FORMAT_R16G16B16A16_SNORM:
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]);
81 for (i = 0; i < height; i++) {
82 GLshort *dst = (GLshort *) (data + (ypos + i) * stride + xpos * 8);
83 for (j = 0; j < width; j++) {
94 _mesa_problem(ctx, "unexpected format in st_clear_accum_buffer()");
101 accum_mad(struct gl_context *ctx, GLfloat scale, GLfloat bias,
102 GLint xpos, GLint ypos, GLint width, GLint height,
103 struct st_renderbuffer *acc_strb)
105 size_t stride = acc_strb->stride;
106 GLubyte *data = acc_strb->data;
108 switch (acc_strb->format) {
109 case PIPE_FORMAT_R16G16B16A16_SNORM:
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);
122 _mesa_problem(NULL, "unexpected format in st_clear_accum_buffer()");
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)
133 struct pipe_context *pipe = st->pipe;
134 struct pipe_transfer *color_trans;
135 size_t stride = acc_strb->stride;
136 GLubyte *data = acc_strb->data;
139 if (ST_DEBUG & DEBUG_FALLBACK)
140 debug_printf("%s: fallback processing\n", __FUNCTION__);
142 color_trans = pipe_get_transfer(st->pipe,
145 PIPE_TRANSFER_READ, xpos, ypos,
148 buf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
150 pipe_get_tile_rgba_format(pipe, color_trans, 0, 0, width, height,
151 util_format_linear(color_strb->texture->format),
154 switch (acc_strb->format) {
155 case PIPE_FORMAT_R16G16B16A16_SNORM:
157 const GLfloat *color = buf;
159 for (i = 0; i < height; i++) {
160 GLshort *acc = (GLshort *) (data + (ypos + i) * stride + xpos * 8);
161 for (j = 0; j < width * 4; j++) {
162 float val = *color++ * value;
163 *acc++ += FLOAT_TO_SHORT(val);
169 _mesa_problem(NULL, "unexpected format in st_clear_accum_buffer()");
173 pipe->transfer_destroy(pipe, color_trans);
178 accum_load(struct st_context *st, GLfloat value,
179 GLint xpos, GLint ypos, GLint width, GLint height,
180 struct st_renderbuffer *acc_strb,
181 struct st_renderbuffer *color_strb)
183 struct pipe_context *pipe = st->pipe;
184 struct pipe_transfer *color_trans;
185 size_t stride = acc_strb->stride;
186 GLubyte *data = acc_strb->data;
189 if (ST_DEBUG & DEBUG_FALLBACK)
190 debug_printf("%s: fallback processing\n", __FUNCTION__);
192 color_trans = pipe_get_transfer(st->pipe, color_strb->texture,
194 PIPE_TRANSFER_READ, xpos, ypos,
197 buf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
199 pipe_get_tile_rgba_format(pipe, color_trans, 0, 0, width, height,
200 util_format_linear(color_strb->texture->format),
203 switch (acc_strb->format) {
204 case PIPE_FORMAT_R16G16B16A16_SNORM:
206 const GLfloat *color = buf;
208 for (i = 0; i < height; i++) {
209 GLshort *acc = (GLshort *) (data + (ypos + i) * stride + xpos * 8);
210 for (j = 0; j < width * 4; j++) {
211 float val = *color++ * value;
212 *acc++ = FLOAT_TO_SHORT(val);
218 _mesa_problem(NULL, "unexpected format in st_clear_accum_buffer()");
222 pipe->transfer_destroy(pipe, color_trans);
227 accum_return(struct gl_context *ctx, GLfloat value,
228 GLint xpos, GLint ypos, GLint width, GLint height,
229 struct st_renderbuffer *acc_strb,
230 struct st_renderbuffer *color_strb)
232 struct pipe_context *pipe = st_context(ctx)->pipe;
233 const GLubyte *colormask = ctx->Color.ColorMask[0];
234 enum pipe_transfer_usage usage;
235 struct pipe_transfer *color_trans;
236 size_t stride = acc_strb->stride;
237 const GLubyte *data = acc_strb->data;
239 enum pipe_format format = util_format_linear(color_strb->texture->format);
241 if (ST_DEBUG & DEBUG_FALLBACK)
242 debug_printf("%s: fallback processing\n", __FUNCTION__);
244 buf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
246 if (!colormask[0] || !colormask[1] || !colormask[2] || !colormask[3])
247 usage = PIPE_TRANSFER_READ_WRITE;
249 usage = PIPE_TRANSFER_WRITE;
251 color_trans = pipe_get_transfer(pipe,
252 color_strb->texture, 0, 0,
257 if (usage & PIPE_TRANSFER_READ)
258 pipe_get_tile_rgba_format(pipe, color_trans, 0, 0, width, height,
261 switch (acc_strb->format) {
262 case PIPE_FORMAT_R16G16B16A16_SNORM:
264 GLfloat *color = buf;
266 for (i = 0; i < height; i++) {
267 const GLshort *acc = (const GLshort *) (data + (ypos + i) * stride + xpos * 8);
268 for (j = 0; j < width; j++) {
269 for (ch = 0; ch < 4; ch++) {
271 GLfloat val = SHORT_TO_FLOAT(*acc * value);
272 *color = CLAMP(val, 0.0f, 1.0f);
285 _mesa_problem(NULL, "unexpected format in st_clear_accum_buffer()");
288 pipe_put_tile_rgba_format(pipe, color_trans, 0, 0, width, height,
292 pipe->transfer_destroy(pipe, color_trans);
297 st_Accum(struct gl_context *ctx, GLenum op, GLfloat value)
299 struct st_context *st = st_context(ctx);
300 struct st_renderbuffer *acc_strb
301 = st_renderbuffer(ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer);
302 struct st_renderbuffer *color_strb
303 = st_renderbuffer(ctx->ReadBuffer->_ColorReadBuffer);
305 const GLint xpos = ctx->DrawBuffer->_Xmin;
306 const GLint ypos = ctx->DrawBuffer->_Ymin;
307 const GLint width = ctx->DrawBuffer->_Xmax - xpos;
308 const GLint height = ctx->DrawBuffer->_Ymax - ypos;
316 accum_mad(ctx, 1.0, value, xpos, ypos, width, height, acc_strb);
321 accum_mad(ctx, value, 0.0, xpos, ypos, width, height, acc_strb);
326 accum_accum(st, value, xpos, ypos, width, height, acc_strb, color_strb);
330 accum_load(st, value, xpos, ypos, width, height, acc_strb, color_strb);
333 accum_return(ctx, value, xpos, ypos, width, height, acc_strb, color_strb);
342 void st_init_accum_functions(struct dd_function_table *functions)
344 functions->Accum = st_Accum;
347 #endif /* FEATURE_accum */