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"
37 #include "st_context.h"
38 #include "st_cb_accum.h"
39 #include "st_cb_fbo.h"
41 #include "st_public.h"
42 #include "st_format.h"
43 #include "st_texture.h"
44 #include "st_inlines.h"
45 #include "pipe/p_context.h"
46 #include "pipe/p_defines.h"
47 #include "pipe/p_inlines.h"
48 #include "util/u_tile.h"
52 * For hardware that supports deep color buffers, we could accelerate
53 * most/all the accum operations with blending/texturing.
54 * For now, just use the get/put_tile() functions and do things in software.
59 st_clear_accum_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
61 struct st_renderbuffer
*acc_strb
= st_renderbuffer(rb
);
62 const GLint xpos
= ctx
->DrawBuffer
->_Xmin
;
63 const GLint ypos
= ctx
->DrawBuffer
->_Ymin
;
64 const GLint width
= ctx
->DrawBuffer
->_Xmax
- xpos
;
65 const GLint height
= ctx
->DrawBuffer
->_Ymax
- ypos
;
66 size_t stride
= acc_strb
->stride
;
67 GLubyte
*data
= acc_strb
->data
;
72 switch (acc_strb
->format
) {
73 case PIPE_FORMAT_R16G16B16A16_SNORM
:
75 GLshort r
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[0]);
76 GLshort g
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[1]);
77 GLshort b
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[2]);
78 GLshort a
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[3]);
80 for (i
= 0; i
< height
; i
++) {
81 GLshort
*dst
= (GLshort
*) (data
+ (ypos
+ i
) * stride
+ xpos
* 8);
82 for (j
= 0; j
< width
; j
++) {
93 _mesa_problem(ctx
, "unexpected format in st_clear_accum_buffer()");
100 accum_mad(GLcontext
*ctx
, GLfloat scale
, GLfloat bias
,
101 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
102 struct st_renderbuffer
*acc_strb
)
104 size_t stride
= acc_strb
->stride
;
105 GLubyte
*data
= acc_strb
->data
;
107 switch (acc_strb
->format
) {
108 case PIPE_FORMAT_R16G16B16A16_SNORM
:
111 for (i
= 0; i
< height
; i
++) {
112 GLshort
*acc
= (GLshort
*) (data
+ (ypos
+ i
) * stride
+ xpos
* 8);
113 for (j
= 0; j
< width
* 4; j
++) {
114 float val
= SHORT_TO_FLOAT(*acc
) * scale
+ bias
;
115 *acc
++ = FLOAT_TO_SHORT(val
);
121 _mesa_problem(NULL
, "unexpected format in st_clear_accum_buffer()");
127 accum_accum(struct st_context
*st
, GLfloat value
,
128 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
129 struct st_renderbuffer
*acc_strb
,
130 struct st_renderbuffer
*color_strb
)
132 struct pipe_context
*pipe
= st
->pipe
;
133 struct pipe_screen
*screen
= pipe
->screen
;
134 struct pipe_transfer
*color_trans
;
135 size_t stride
= acc_strb
->stride
;
136 GLubyte
*data
= acc_strb
->data
;
139 color_trans
= st_cond_flush_get_tex_transfer(st
, color_strb
->texture
,
141 PIPE_TRANSFER_READ
, xpos
, ypos
,
144 buf
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
146 pipe_get_tile_rgba(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 screen
->tex_transfer_destroy(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_screen
*screen
= pipe
->screen
;
179 struct pipe_transfer
*color_trans
;
180 size_t stride
= acc_strb
->stride
;
181 GLubyte
*data
= acc_strb
->data
;
184 color_trans
= st_cond_flush_get_tex_transfer(st
, color_strb
->texture
,
186 PIPE_TRANSFER_READ
, xpos
, ypos
,
189 buf
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
191 pipe_get_tile_rgba(color_trans
, 0, 0, width
, height
, buf
);
193 switch (acc_strb
->format
) {
194 case PIPE_FORMAT_R16G16B16A16_SNORM
:
196 const GLfloat
*color
= buf
;
198 for (i
= 0; i
< height
; i
++) {
199 GLshort
*acc
= (GLshort
*) (data
+ (ypos
+ i
) * stride
+ xpos
* 8);
200 for (j
= 0; j
< width
* 4; j
++) {
201 float val
= *color
++ * value
;
202 *acc
++ = FLOAT_TO_SHORT(val
);
208 _mesa_problem(NULL
, "unexpected format in st_clear_accum_buffer()");
212 screen
->tex_transfer_destroy(color_trans
);
217 accum_return(GLcontext
*ctx
, GLfloat value
,
218 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
219 struct st_renderbuffer
*acc_strb
,
220 struct st_renderbuffer
*color_strb
)
222 struct pipe_context
*pipe
= ctx
->st
->pipe
;
223 struct pipe_screen
*screen
= pipe
->screen
;
224 const GLubyte
*colormask
= ctx
->Color
.ColorMask
;
225 enum pipe_transfer_usage usage
;
226 struct pipe_transfer
*color_trans
;
227 size_t stride
= acc_strb
->stride
;
228 const GLubyte
*data
= acc_strb
->data
;
231 buf
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
233 if (!colormask
[0] || !colormask
[1] || !colormask
[2] || !colormask
[3])
234 usage
= PIPE_TRANSFER_READ_WRITE
;
236 usage
= PIPE_TRANSFER_WRITE
;
238 color_trans
= st_cond_flush_get_tex_transfer(st_context(ctx
),
239 color_strb
->texture
, 0, 0, 0,
244 if (usage
!= PIPE_TRANSFER_WRITE
)
245 pipe_get_tile_rgba(color_trans
, 0, 0, width
, height
, buf
);
247 switch (acc_strb
->format
) {
248 case PIPE_FORMAT_R16G16B16A16_SNORM
:
250 GLfloat
*color
= buf
;
252 for (i
= 0; i
< height
; i
++) {
253 const GLshort
*acc
= (const GLshort
*) (data
+ (ypos
+ i
) * stride
+ xpos
* 8);
254 for (j
= 0; j
< width
; j
++) {
255 for (ch
= 0; ch
< 4; ch
++) {
257 GLfloat val
= SHORT_TO_FLOAT(*acc
* value
);
258 *color
= CLAMP(val
, 0.0f
, 1.0f
);
271 _mesa_problem(NULL
, "unexpected format in st_clear_accum_buffer()");
274 pipe_put_tile_rgba(color_trans
, 0, 0, width
, height
, buf
);
277 screen
->tex_transfer_destroy(color_trans
);
282 st_Accum(GLcontext
*ctx
, GLenum op
, GLfloat value
)
284 struct st_context
*st
= ctx
->st
;
285 struct st_renderbuffer
*acc_strb
286 = st_renderbuffer(ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
);
287 struct st_renderbuffer
*color_strb
288 = st_renderbuffer(ctx
->ReadBuffer
->_ColorReadBuffer
);
290 const GLint xpos
= ctx
->DrawBuffer
->_Xmin
;
291 const GLint ypos
= ctx
->DrawBuffer
->_Ymin
;
292 const GLint width
= ctx
->DrawBuffer
->_Xmax
- xpos
;
293 const GLint height
= ctx
->DrawBuffer
->_Ymax
- ypos
;
298 /* make sure color bufs aren't cached */
299 st_flush( st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
304 accum_mad(ctx
, 1.0, value
, xpos
, ypos
, width
, height
, acc_strb
);
309 accum_mad(ctx
, value
, 0.0, xpos
, ypos
, width
, height
, acc_strb
);
314 accum_accum(st
, value
, xpos
, ypos
, width
, height
, acc_strb
, color_strb
);
318 accum_load(st
, value
, xpos
, ypos
, width
, height
, acc_strb
, color_strb
);
321 accum_return(ctx
, value
, xpos
, ypos
, width
, height
, acc_strb
, color_strb
);
330 void st_init_accum_functions(struct dd_function_table
*functions
)
332 functions
->Accum
= st_Accum
;