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"
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"
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.
58 st_clear_accum_buffer(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
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
;
71 switch (acc_strb
->format
) {
72 case PIPE_FORMAT_R16G16B16A16_SNORM
:
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]);
79 for (i
= 0; i
< height
; i
++) {
80 GLshort
*dst
= (GLshort
*) (data
+ (ypos
+ i
) * stride
+ xpos
* 8);
81 for (j
= 0; j
< width
; j
++) {
92 _mesa_problem(ctx
, "unexpected format in st_clear_accum_buffer()");
99 accum_mad(struct gl_context
*ctx
, GLfloat scale
, GLfloat bias
,
100 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
101 struct st_renderbuffer
*acc_strb
)
103 size_t stride
= acc_strb
->stride
;
104 GLubyte
*data
= acc_strb
->data
;
106 switch (acc_strb
->format
) {
107 case PIPE_FORMAT_R16G16B16A16_SNORM
:
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
);
120 _mesa_problem(NULL
, "unexpected format in st_clear_accum_buffer()");
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
)
131 struct pipe_context
*pipe
= st
->pipe
;
132 struct pipe_transfer
*color_trans
;
133 size_t stride
= acc_strb
->stride
;
134 GLubyte
*data
= acc_strb
->data
;
137 if (ST_DEBUG
& DEBUG_FALLBACK
)
138 debug_printf("%s: fallback processing\n", __FUNCTION__
);
140 color_trans
= pipe_get_transfer(st
->pipe
,
143 PIPE_TRANSFER_READ
, xpos
, ypos
,
146 buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
148 pipe_get_tile_rgba(pipe
, color_trans
, 0, 0, width
, height
, buf
);
150 switch (acc_strb
->format
) {
151 case PIPE_FORMAT_R16G16B16A16_SNORM
:
153 const GLfloat
*color
= buf
;
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
);
165 _mesa_problem(NULL
, "unexpected format in st_clear_accum_buffer()");
169 pipe
->transfer_destroy(pipe
, color_trans
);
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
)
179 struct pipe_context
*pipe
= st
->pipe
;
180 struct pipe_transfer
*color_trans
;
181 size_t stride
= acc_strb
->stride
;
182 GLubyte
*data
= acc_strb
->data
;
186 if (ST_DEBUG
& DEBUG_FALLBACK
)
187 debug_printf("%s: fallback processing\n", __FUNCTION__
);
189 color_trans
= pipe_get_transfer(st
->pipe
, color_strb
->texture
,
191 PIPE_TRANSFER_READ
, xpos
, ypos
,
194 buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
196 pipe_get_tile_rgba(pipe
, color_trans
, 0, 0, width
, height
, buf
);
198 switch (acc_strb
->format
) {
199 case PIPE_FORMAT_R16G16B16A16_SNORM
:
201 const GLfloat
*color
= buf
;
203 for (i
= 0; i
< height
; i
++) {
204 GLshort
*acc
= (GLshort
*) (data
+ (ypos
+ i
) * stride
+ xpos
* 8);
205 for (j
= 0; j
< width
* 4; j
++) {
206 float val
= *color
++ * value
;
207 *acc
++ = FLOAT_TO_SHORT(val
);
213 _mesa_problem(NULL
, "unexpected format in st_clear_accum_buffer()");
217 pipe
->transfer_destroy(pipe
, color_trans
);
222 accum_return(struct gl_context
*ctx
, GLfloat value
,
223 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
224 struct st_renderbuffer
*acc_strb
,
225 struct st_renderbuffer
*color_strb
)
227 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
228 const GLubyte
*colormask
= ctx
->Color
.ColorMask
[0];
229 enum pipe_transfer_usage usage
;
230 struct pipe_transfer
*color_trans
;
231 size_t stride
= acc_strb
->stride
;
232 const GLubyte
*data
= acc_strb
->data
;
235 if (ST_DEBUG
& DEBUG_FALLBACK
)
236 debug_printf("%s: fallback processing\n", __FUNCTION__
);
238 buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
240 if (!colormask
[0] || !colormask
[1] || !colormask
[2] || !colormask
[3])
241 usage
= PIPE_TRANSFER_READ_WRITE
;
243 usage
= PIPE_TRANSFER_WRITE
;
245 color_trans
= pipe_get_transfer(st_context(ctx
)->pipe
,
246 color_strb
->texture
, 0, 0,
251 if (usage
& PIPE_TRANSFER_READ
)
252 pipe_get_tile_rgba(pipe
, color_trans
, 0, 0, width
, height
, buf
);
254 switch (acc_strb
->format
) {
255 case PIPE_FORMAT_R16G16B16A16_SNORM
:
257 GLfloat
*color
= buf
;
259 for (i
= 0; i
< height
; i
++) {
260 const GLshort
*acc
= (const GLshort
*) (data
+ (ypos
+ i
) * stride
+ xpos
* 8);
261 for (j
= 0; j
< width
; j
++) {
262 for (ch
= 0; ch
< 4; ch
++) {
264 GLfloat val
= SHORT_TO_FLOAT(*acc
* value
);
265 *color
= CLAMP(val
, 0.0f
, 1.0f
);
278 _mesa_problem(NULL
, "unexpected format in st_clear_accum_buffer()");
281 pipe_put_tile_rgba(pipe
, color_trans
, 0, 0, width
, height
, buf
);
284 pipe
->transfer_destroy(pipe
, color_trans
);
289 st_Accum(struct gl_context
*ctx
, GLenum op
, GLfloat value
)
291 struct st_context
*st
= st_context(ctx
);
292 struct st_renderbuffer
*acc_strb
293 = st_renderbuffer(ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
);
294 struct st_renderbuffer
*color_strb
295 = st_renderbuffer(ctx
->ReadBuffer
->_ColorReadBuffer
);
297 const GLint xpos
= ctx
->DrawBuffer
->_Xmin
;
298 const GLint ypos
= ctx
->DrawBuffer
->_Ymin
;
299 const GLint width
= ctx
->DrawBuffer
->_Xmax
- xpos
;
300 const GLint height
= ctx
->DrawBuffer
->_Ymax
- ypos
;
308 accum_mad(ctx
, 1.0, value
, xpos
, ypos
, width
, height
, acc_strb
);
313 accum_mad(ctx
, value
, 0.0, xpos
, ypos
, width
, height
, acc_strb
);
318 accum_accum(st
, value
, xpos
, ypos
, width
, height
, acc_strb
, color_strb
);
322 accum_load(st
, value
, xpos
, ypos
, width
, height
, acc_strb
, color_strb
);
325 accum_return(ctx
, value
, xpos
, ypos
, width
, height
, acc_strb
, color_strb
);
334 void st_init_accum_functions(struct dd_function_table
*functions
)
336 functions
->Accum
= st_Accum
;
339 #endif /* FEATURE_accum */