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"
42 #include "st_public.h"
43 #include "st_format.h"
44 #include "st_texture.h"
45 #include "st_inlines.h"
46 #include "pipe/p_context.h"
47 #include "pipe/p_defines.h"
48 #include "pipe/p_inlines.h"
49 #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(GLcontext
*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(GLcontext
*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_screen
*screen
= pipe
->screen
;
135 struct pipe_transfer
*color_trans
;
136 size_t stride
= acc_strb
->stride
;
137 GLubyte
*data
= acc_strb
->data
;
140 if (ST_DEBUG
& DEBUG_FALLBACK
)
141 debug_printf("%s: fallback processing\n", __FUNCTION__
);
143 color_trans
= st_cond_flush_get_tex_transfer(st
, color_strb
->texture
,
145 PIPE_TRANSFER_READ
, xpos
, ypos
,
148 buf
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
150 pipe_get_tile_rgba(color_trans
, 0, 0, width
, height
, buf
);
152 switch (acc_strb
->format
) {
153 case PIPE_FORMAT_R16G16B16A16_SNORM
:
155 const GLfloat
*color
= buf
;
157 for (i
= 0; i
< height
; i
++) {
158 GLshort
*acc
= (GLshort
*) (data
+ (ypos
+ i
) * stride
+ xpos
* 8);
159 for (j
= 0; j
< width
* 4; j
++) {
160 float val
= *color
++ * value
;
161 *acc
++ += FLOAT_TO_SHORT(val
);
167 _mesa_problem(NULL
, "unexpected format in st_clear_accum_buffer()");
171 screen
->tex_transfer_destroy(color_trans
);
176 accum_load(struct st_context
*st
, GLfloat value
,
177 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
178 struct st_renderbuffer
*acc_strb
,
179 struct st_renderbuffer
*color_strb
)
181 struct pipe_context
*pipe
= st
->pipe
;
182 struct pipe_screen
*screen
= pipe
->screen
;
183 struct pipe_transfer
*color_trans
;
184 size_t stride
= acc_strb
->stride
;
185 GLubyte
*data
= acc_strb
->data
;
189 if (ST_DEBUG
& DEBUG_FALLBACK
)
190 debug_printf("%s: fallback processing\n", __FUNCTION__
);
192 color_trans
= st_cond_flush_get_tex_transfer(st
, color_strb
->texture
,
194 PIPE_TRANSFER_READ
, xpos
, ypos
,
197 buf
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
199 pipe_get_tile_rgba(color_trans
, 0, 0, width
, height
, buf
);
201 switch (acc_strb
->format
) {
202 case PIPE_FORMAT_R16G16B16A16_SNORM
:
204 const GLfloat
*color
= buf
;
206 for (i
= 0; i
< height
; i
++) {
207 GLshort
*acc
= (GLshort
*) (data
+ (ypos
+ i
) * stride
+ xpos
* 8);
208 for (j
= 0; j
< width
* 4; j
++) {
209 float val
= *color
++ * value
;
210 *acc
++ = FLOAT_TO_SHORT(val
);
216 _mesa_problem(NULL
, "unexpected format in st_clear_accum_buffer()");
220 screen
->tex_transfer_destroy(color_trans
);
225 accum_return(GLcontext
*ctx
, GLfloat value
,
226 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
227 struct st_renderbuffer
*acc_strb
,
228 struct st_renderbuffer
*color_strb
)
230 struct pipe_context
*pipe
= ctx
->st
->pipe
;
231 struct pipe_screen
*screen
= pipe
->screen
;
232 const GLubyte
*colormask
= ctx
->Color
.ColorMask
;
233 enum pipe_transfer_usage usage
;
234 struct pipe_transfer
*color_trans
;
235 size_t stride
= acc_strb
->stride
;
236 const GLubyte
*data
= acc_strb
->data
;
239 if (ST_DEBUG
& DEBUG_FALLBACK
)
240 debug_printf("%s: fallback processing\n", __FUNCTION__
);
242 buf
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
244 if (!colormask
[0] || !colormask
[1] || !colormask
[2] || !colormask
[3])
245 usage
= PIPE_TRANSFER_READ_WRITE
;
247 usage
= PIPE_TRANSFER_WRITE
;
249 color_trans
= st_cond_flush_get_tex_transfer(st_context(ctx
),
250 color_strb
->texture
, 0, 0, 0,
255 if (usage
& PIPE_TRANSFER_READ
)
256 pipe_get_tile_rgba(color_trans
, 0, 0, width
, height
, buf
);
258 switch (acc_strb
->format
) {
259 case PIPE_FORMAT_R16G16B16A16_SNORM
:
261 GLfloat
*color
= buf
;
263 for (i
= 0; i
< height
; i
++) {
264 const GLshort
*acc
= (const GLshort
*) (data
+ (ypos
+ i
) * stride
+ xpos
* 8);
265 for (j
= 0; j
< width
; j
++) {
266 for (ch
= 0; ch
< 4; ch
++) {
268 GLfloat val
= SHORT_TO_FLOAT(*acc
* value
);
269 *color
= CLAMP(val
, 0.0f
, 1.0f
);
282 _mesa_problem(NULL
, "unexpected format in st_clear_accum_buffer()");
285 pipe_put_tile_rgba(color_trans
, 0, 0, width
, height
, buf
);
288 screen
->tex_transfer_destroy(color_trans
);
293 st_Accum(GLcontext
*ctx
, GLenum op
, GLfloat value
)
295 struct st_context
*st
= ctx
->st
;
296 struct st_renderbuffer
*acc_strb
297 = st_renderbuffer(ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
);
298 struct st_renderbuffer
*color_strb
299 = st_renderbuffer(ctx
->ReadBuffer
->_ColorReadBuffer
);
301 const GLint xpos
= ctx
->DrawBuffer
->_Xmin
;
302 const GLint ypos
= ctx
->DrawBuffer
->_Ymin
;
303 const GLint width
= ctx
->DrawBuffer
->_Xmax
- xpos
;
304 const GLint height
= ctx
->DrawBuffer
->_Ymax
- ypos
;
309 /* make sure color bufs aren't cached */
310 st_flush( st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
315 accum_mad(ctx
, 1.0, value
, xpos
, ypos
, width
, height
, acc_strb
);
320 accum_mad(ctx
, value
, 0.0, xpos
, ypos
, width
, height
, acc_strb
);
325 accum_accum(st
, value
, xpos
, ypos
, width
, height
, acc_strb
, color_strb
);
329 accum_load(st
, value
, xpos
, ypos
, width
, height
, acc_strb
, color_strb
);
332 accum_return(ctx
, value
, xpos
, ypos
, width
, height
, acc_strb
, color_strb
);
341 void st_init_accum_functions(struct dd_function_table
*functions
)
343 functions
->Accum
= st_Accum
;