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"
51 #define UNCLAMPED_FLOAT_TO_SHORT(us, f) \
52 us = ( (short) ( CLAMP((f), -1.0, 1.0) * 32767.0F) )
56 * For hardware that supports deep color buffers, we could accelerate
57 * most/all the accum operations with blending/texturing.
58 * For now, just use the get/put_tile() functions and do things in software.
63 * Wrapper for pipe_get_tile_rgba(). Do format/cpp override to make the
64 * tile util function think the surface is 16bit/channel, even if it's not.
65 * See also: st_renderbuffer_alloc_storage()
68 acc_get_tile_rgba(struct pipe_context
*pipe
, struct pipe_transfer
*acc_pt
,
69 uint x
, uint y
, uint w
, uint h
, float *p
)
71 const enum pipe_format f
= acc_pt
->format
;
72 const struct pipe_format_block b
= acc_pt
->block
;
74 acc_pt
->format
= DEFAULT_ACCUM_PIPE_FORMAT
;
75 acc_pt
->block
.size
= 8;
76 acc_pt
->block
.width
= 1;
77 acc_pt
->block
.height
= 1;
79 pipe_get_tile_rgba(acc_pt
, x
, y
, w
, h
, p
);
87 * Wrapper for pipe_put_tile_rgba(). Do format/cpp override to make the
88 * tile util function think the surface is 16bit/channel, even if it's not.
89 * See also: st_renderbuffer_alloc_storage()
92 acc_put_tile_rgba(struct pipe_context
*pipe
, struct pipe_transfer
*acc_pt
,
93 uint x
, uint y
, uint w
, uint h
, const float *p
)
95 enum pipe_format f
= acc_pt
->format
;
96 const struct pipe_format_block b
= acc_pt
->block
;
98 acc_pt
->format
= DEFAULT_ACCUM_PIPE_FORMAT
;
99 acc_pt
->block
.size
= 8;
100 acc_pt
->block
.width
= 1;
101 acc_pt
->block
.height
= 1;
103 pipe_put_tile_rgba(acc_pt
, x
, y
, w
, h
, p
);
112 st_clear_accum_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
114 struct st_renderbuffer
*acc_strb
= st_renderbuffer(rb
);
115 struct pipe_transfer
*acc_pt
;
116 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
117 const GLint xpos
= ctx
->DrawBuffer
->_Xmin
;
118 const GLint ypos
= ctx
->DrawBuffer
->_Ymin
;
119 const GLint width
= ctx
->DrawBuffer
->_Xmax
- xpos
;
120 const GLint height
= ctx
->DrawBuffer
->_Ymax
- ypos
;
123 acc_pt
= st_cond_flush_get_tex_transfer(st_context(ctx
), acc_strb
->texture
,
125 PIPE_TRANSFER_WRITE
, xpos
, ypos
,
127 map
= screen
->transfer_map(screen
, acc_pt
);
129 /* note acc_strb->format might not equal acc_pt->format */
130 switch (acc_strb
->format
) {
131 case PIPE_FORMAT_R16G16B16A16_SNORM
:
133 GLshort r
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[0]);
134 GLshort g
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[1]);
135 GLshort b
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[2]);
136 GLshort a
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[3]);
138 for (i
= 0; i
< height
; i
++) {
139 GLshort
*dst
= (GLshort
*) (map
+ i
* acc_pt
->stride
+ xpos
* 8);
140 for (j
= 0; j
< width
; j
++) {
151 _mesa_problem(ctx
, "unexpected format in st_clear_accum_buffer()");
154 screen
->transfer_unmap(screen
, acc_pt
);
155 screen
->tex_transfer_destroy(acc_pt
);
161 accum_mad(GLcontext
*ctx
, GLfloat scale
, GLfloat bias
,
162 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
163 struct st_renderbuffer
*acc_strb
)
165 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
166 struct pipe_transfer
*acc_pt
;
169 acc_pt
= st_cond_flush_get_tex_transfer(st_context(ctx
), acc_strb
->texture
,
171 PIPE_TRANSFER_READ_WRITE
,
174 map
= screen
->transfer_map(screen
, acc_pt
);
176 /* note acc_strb->format might not equal acc_pt->format */
177 switch (acc_strb
->format
) {
178 case PIPE_FORMAT_R16G16B16A16_SNORM
:
181 for (i
= 0; i
< height
; i
++) {
182 GLshort
*acc
= (GLshort
*) (map
+ (ypos
+ i
) * acc_pt
->stride
+ xpos
* 8);
183 for (j
= 0; j
< width
* 4; j
++) {
184 float val
= SHORT_TO_FLOAT(acc
[j
]) * scale
+ bias
;
185 acc
[j
] = FLOAT_TO_SHORT(val
);
191 _mesa_problem(NULL
, "unexpected format in st_clear_accum_buffer()");
194 screen
->transfer_unmap(screen
, acc_pt
);
195 screen
->tex_transfer_destroy(acc_pt
);
200 accum_accum(struct st_context
*st
, GLfloat value
,
201 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
202 struct st_renderbuffer
*acc_strb
,
203 struct st_renderbuffer
*color_strb
)
205 struct pipe_context
*pipe
= st
->pipe
;
206 struct pipe_screen
*screen
= pipe
->screen
;
207 struct pipe_transfer
*acc_trans
, *color_trans
;
208 GLfloat
*colorBuf
, *accBuf
;
211 acc_trans
= st_cond_flush_get_tex_transfer(st
, acc_strb
->texture
, 0, 0, 0,
212 PIPE_TRANSFER_READ
, xpos
, ypos
,
215 color_trans
= st_cond_flush_get_tex_transfer(st
, color_strb
->texture
,
217 PIPE_TRANSFER_READ
, xpos
, ypos
,
220 colorBuf
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
221 accBuf
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
223 pipe_get_tile_rgba(color_trans
, 0, 0, width
, height
, colorBuf
);
224 acc_get_tile_rgba(pipe
, acc_trans
, 0, 0, width
, height
, accBuf
);
226 for (i
= 0; i
< 4 * width
* height
; i
++) {
227 accBuf
[i
] = accBuf
[i
] + colorBuf
[i
] * value
;
230 screen
->tex_transfer_destroy(acc_trans
);
231 acc_trans
= st_no_flush_get_tex_transfer(st
, acc_strb
->texture
, 0, 0, 0,
232 PIPE_TRANSFER_WRITE
, xpos
, ypos
,
235 acc_put_tile_rgba(pipe
, acc_trans
, 0, 0, width
, height
, accBuf
);
237 _mesa_free(colorBuf
);
239 screen
->tex_transfer_destroy(acc_trans
);
240 screen
->tex_transfer_destroy(color_trans
);
245 accum_load(struct st_context
*st
, GLfloat value
,
246 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
247 struct st_renderbuffer
*acc_strb
,
248 struct st_renderbuffer
*color_strb
)
250 struct pipe_context
*pipe
= st
->pipe
;
251 struct pipe_screen
*screen
= pipe
->screen
;
252 struct pipe_transfer
*acc_trans
, *color_trans
;
256 acc_trans
= st_cond_flush_get_tex_transfer(st
, acc_strb
->texture
, 0, 0, 0,
257 PIPE_TRANSFER_WRITE
, xpos
, ypos
,
260 color_trans
= st_cond_flush_get_tex_transfer(st
, color_strb
->texture
,
262 PIPE_TRANSFER_READ
, xpos
, ypos
,
265 buf
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
267 pipe_get_tile_rgba(color_trans
, 0, 0, width
, height
, buf
);
269 for (i
= 0; i
< 4 * width
* height
; i
++) {
270 buf
[i
] = buf
[i
] * value
;
273 acc_put_tile_rgba(pipe
, acc_trans
, 0, 0, width
, height
, buf
);
276 screen
->tex_transfer_destroy(acc_trans
);
277 screen
->tex_transfer_destroy(color_trans
);
282 accum_return(GLcontext
*ctx
, GLfloat value
,
283 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
284 struct st_renderbuffer
*acc_strb
,
285 struct st_renderbuffer
*color_strb
)
287 struct pipe_context
*pipe
= ctx
->st
->pipe
;
288 struct pipe_screen
*screen
= pipe
->screen
;
289 const GLubyte
*colormask
= ctx
->Color
.ColorMask
;
290 struct pipe_transfer
*acc_trans
, *color_trans
;
291 GLfloat
*abuf
, *cbuf
= NULL
;
294 abuf
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
296 acc_trans
= st_cond_flush_get_tex_transfer(st_context(ctx
),
297 acc_strb
->texture
, 0, 0, 0,
298 PIPE_TRANSFER_READ
, xpos
, ypos
,
301 color_trans
= st_cond_flush_get_tex_transfer(st_context(ctx
),
302 color_strb
->texture
, 0, 0, 0,
303 PIPE_TRANSFER_READ_WRITE
,
307 acc_get_tile_rgba(pipe
, acc_trans
, 0, 0, width
, height
, abuf
);
309 if (!colormask
[0] || !colormask
[1] || !colormask
[2] || !colormask
[3]) {
310 cbuf
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
311 pipe_get_tile_rgba(color_trans
, 0, 0, width
, height
, cbuf
);
314 for (i
= 0; i
< width
* height
; i
++) {
315 for (ch
= 0; ch
< 4; ch
++) {
317 GLfloat val
= abuf
[i
* 4 + ch
] * value
;
318 abuf
[i
* 4 + ch
] = CLAMP(val
, 0.0f
, 1.0f
);
321 abuf
[i
* 4 + ch
] = cbuf
[i
* 4 + ch
];
326 pipe_put_tile_rgba(color_trans
, 0, 0, width
, height
, abuf
);
331 screen
->tex_transfer_destroy(acc_trans
);
332 screen
->tex_transfer_destroy(color_trans
);
337 st_Accum(GLcontext
*ctx
, GLenum op
, GLfloat value
)
339 struct st_context
*st
= ctx
->st
;
340 struct st_renderbuffer
*acc_strb
341 = st_renderbuffer(ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
);
342 struct st_renderbuffer
*color_strb
343 = st_renderbuffer(ctx
->ReadBuffer
->_ColorReadBuffer
);
345 const GLint xpos
= ctx
->DrawBuffer
->_Xmin
;
346 const GLint ypos
= ctx
->DrawBuffer
->_Ymin
;
347 const GLint width
= ctx
->DrawBuffer
->_Xmax
- xpos
;
348 const GLint height
= ctx
->DrawBuffer
->_Ymax
- ypos
;
350 /* make sure color bufs aren't cached */
351 st_flush( st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
356 accum_mad(ctx
, 1.0, value
, xpos
, ypos
, width
, height
, acc_strb
);
361 accum_mad(ctx
, value
, 0.0, xpos
, ypos
, width
, height
, acc_strb
);
366 accum_accum(st
, value
, xpos
, ypos
, width
, height
, acc_strb
, color_strb
);
370 accum_load(st
, value
, xpos
, ypos
, width
, height
, acc_strb
, color_strb
);
373 accum_return(ctx
, value
, xpos
, ypos
, width
, height
, acc_strb
, color_strb
);
382 void st_init_accum_functions(struct dd_function_table
*functions
)
384 functions
->Accum
= st_Accum
;