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"
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.
56 st_clear_accum_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
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
;
69 switch (acc_strb
->format
) {
70 case PIPE_FORMAT_R16G16B16A16_SNORM
:
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]);
77 for (i
= 0; i
< height
; i
++) {
78 GLshort
*dst
= (GLshort
*) (data
+ (ypos
+ i
) * stride
+ xpos
* 8);
79 for (j
= 0; j
< width
; j
++) {
90 _mesa_problem(ctx
, "unexpected format in st_clear_accum_buffer()");
97 accum_mad(GLcontext
*ctx
, GLfloat scale
, GLfloat bias
,
98 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
99 struct st_renderbuffer
*acc_strb
)
101 size_t stride
= acc_strb
->stride
;
102 GLubyte
*data
= acc_strb
->data
;
104 switch (acc_strb
->format
) {
105 case PIPE_FORMAT_R16G16B16A16_SNORM
:
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
);
118 _mesa_problem(NULL
, "unexpected format in st_clear_accum_buffer()");
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
)
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
;
135 if (ST_DEBUG
& DEBUG_FALLBACK
)
136 debug_printf("%s: fallback processing\n", __FUNCTION__
);
138 color_trans
= pipe_get_transfer(st
->pipe
,
141 PIPE_TRANSFER_READ
, xpos
, ypos
,
144 buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
146 pipe_get_tile_rgba(pipe
, color_trans
, 0, 0, width
, height
, buf
);
148 switch (acc_strb
->format
) {
149 case PIPE_FORMAT_R16G16B16A16_SNORM
:
151 const GLfloat
*color
= buf
;
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
);
163 _mesa_problem(NULL
, "unexpected format in st_clear_accum_buffer()");
167 pipe
->transfer_destroy(pipe
, color_trans
);
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
)
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
;
184 if (ST_DEBUG
& DEBUG_FALLBACK
)
185 debug_printf("%s: fallback processing\n", __FUNCTION__
);
187 color_trans
= pipe_get_transfer(st
->pipe
, color_strb
->texture
,
189 PIPE_TRANSFER_READ
, xpos
, ypos
,
192 buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
194 pipe_get_tile_rgba(pipe
, color_trans
, 0, 0, width
, height
, buf
);
196 switch (acc_strb
->format
) {
197 case PIPE_FORMAT_R16G16B16A16_SNORM
:
199 const GLfloat
*color
= buf
;
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
);
211 _mesa_problem(NULL
, "unexpected format in st_clear_accum_buffer()");
215 pipe
->transfer_destroy(pipe
, color_trans
);
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
)
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
;
233 if (ST_DEBUG
& DEBUG_FALLBACK
)
234 debug_printf("%s: fallback processing\n", __FUNCTION__
);
236 buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
238 if (!colormask
[0] || !colormask
[1] || !colormask
[2] || !colormask
[3])
239 usage
= PIPE_TRANSFER_READ_WRITE
;
241 usage
= PIPE_TRANSFER_WRITE
;
243 color_trans
= pipe_get_transfer(st_context(ctx
)->pipe
,
244 color_strb
->texture
, 0, 0, 0,
249 if (usage
& PIPE_TRANSFER_READ
)
250 pipe_get_tile_rgba(pipe
, color_trans
, 0, 0, width
, height
, buf
);
252 switch (acc_strb
->format
) {
253 case PIPE_FORMAT_R16G16B16A16_SNORM
:
255 GLfloat
*color
= buf
;
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
++) {
262 GLfloat val
= SHORT_TO_FLOAT(*acc
* value
);
263 *color
= CLAMP(val
, 0.0f
, 1.0f
);
276 _mesa_problem(NULL
, "unexpected format in st_clear_accum_buffer()");
279 pipe_put_tile_rgba(pipe
, color_trans
, 0, 0, width
, height
, buf
);
282 pipe
->transfer_destroy(pipe
, color_trans
);
287 st_Accum(GLcontext
*ctx
, GLenum op
, GLfloat value
)
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
);
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
;
306 accum_mad(ctx
, 1.0, value
, xpos
, ypos
, width
, height
, acc_strb
);
311 accum_mad(ctx
, value
, 0.0, xpos
, ypos
, width
, height
, acc_strb
);
316 accum_accum(st
, value
, xpos
, ypos
, width
, height
, acc_strb
, color_strb
);
320 accum_load(st
, value
, xpos
, ypos
, width
, height
, acc_strb
, color_strb
);
323 accum_return(ctx
, value
, xpos
, ypos
, width
, height
, acc_strb
, color_strb
);
332 void st_init_accum_functions(struct dd_function_table
*functions
)
334 functions
->Accum
= st_Accum
;