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 "pipe/p_context.h"
45 #include "pipe/p_defines.h"
46 #include "pipe/p_inlines.h"
47 #include "util/u_tile.h"
50 #define UNCLAMPED_FLOAT_TO_SHORT(us, f) \
51 us = ( (short) ( CLAMP((f), -1.0, 1.0) * 32767.0F) )
55 * For hardware that supports deep color buffers, we could accelerate
56 * most/all the accum operations with blending/texturing.
57 * For now, just use the get/put_tile() functions and do things in software.
62 * Wrapper for pipe_get_tile_rgba(). Do format/cpp override to make the
63 * tile util function think the surface is 16bit/channel, even if it's not.
64 * See also: st_renderbuffer_alloc_storage()
67 acc_get_tile_rgba(struct pipe_context
*pipe
, struct pipe_transfer
*acc_pt
,
68 uint x
, uint y
, uint w
, uint h
, float *p
)
70 const enum pipe_format f
= acc_pt
->format
;
71 const struct pipe_format_block b
= acc_pt
->block
;
73 acc_pt
->format
= DEFAULT_ACCUM_PIPE_FORMAT
;
74 acc_pt
->block
.size
= 8;
75 acc_pt
->block
.width
= 1;
76 acc_pt
->block
.height
= 1;
78 pipe_get_tile_rgba(acc_pt
, x
, y
, w
, h
, p
);
86 * Wrapper for pipe_put_tile_rgba(). Do format/cpp override to make the
87 * tile util function think the surface is 16bit/channel, even if it's not.
88 * See also: st_renderbuffer_alloc_storage()
91 acc_put_tile_rgba(struct pipe_context
*pipe
, struct pipe_transfer
*acc_pt
,
92 uint x
, uint y
, uint w
, uint h
, const float *p
)
94 enum pipe_format f
= acc_pt
->format
;
95 const struct pipe_format_block b
= acc_pt
->block
;
97 acc_pt
->format
= DEFAULT_ACCUM_PIPE_FORMAT
;
98 acc_pt
->block
.size
= 8;
99 acc_pt
->block
.width
= 1;
100 acc_pt
->block
.height
= 1;
102 pipe_put_tile_rgba(acc_pt
, x
, y
, w
, h
, p
);
111 st_clear_accum_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
113 struct st_renderbuffer
*acc_strb
= st_renderbuffer(rb
);
114 struct pipe_transfer
*acc_pt
;
115 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
116 const GLint xpos
= ctx
->DrawBuffer
->_Xmin
;
117 const GLint ypos
= ctx
->DrawBuffer
->_Ymin
;
118 const GLint width
= ctx
->DrawBuffer
->_Xmax
- xpos
;
119 const GLint height
= ctx
->DrawBuffer
->_Ymax
- ypos
;
122 st_teximage_flush_before_map(ctx
->st
, acc_strb
->texture
, 0, 0,
123 PIPE_TRANSFER_WRITE
);
125 acc_pt
= screen
->get_tex_transfer(screen
, acc_strb
->texture
, 0, 0, 0,
126 PIPE_TRANSFER_WRITE
, xpos
, ypos
,
128 map
= screen
->transfer_map(screen
, acc_pt
);
130 /* note acc_strb->format might not equal acc_pt->format */
131 switch (acc_strb
->format
) {
132 case PIPE_FORMAT_R16G16B16A16_SNORM
:
134 GLshort r
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[0]);
135 GLshort g
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[1]);
136 GLshort b
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[2]);
137 GLshort a
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[3]);
139 for (i
= 0; i
< height
; i
++) {
140 GLshort
*dst
= (GLshort
*) (map
+ i
* acc_pt
->stride
+ xpos
* 8);
141 for (j
= 0; j
< width
; j
++) {
152 _mesa_problem(ctx
, "unexpected format in st_clear_accum_buffer()");
155 screen
->transfer_unmap(screen
, acc_pt
);
156 screen
->tex_transfer_destroy(acc_pt
);
162 accum_mad(GLcontext
*ctx
, GLfloat scale
, GLfloat bias
,
163 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
164 struct st_renderbuffer
*acc_strb
)
166 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
167 struct pipe_transfer
*acc_pt
;
170 st_teximage_flush_before_map(ctx
->st
, acc_strb
->texture
, 0, 0,
171 PIPE_TRANSFER_READ_WRITE
);
173 acc_pt
= screen
->get_tex_transfer(screen
, acc_strb
->texture
, 0, 0, 0,
174 PIPE_TRANSFER_READ_WRITE
, xpos
, ypos
,
176 map
= screen
->transfer_map(screen
, acc_pt
);
178 /* note acc_strb->format might not equal acc_pt->format */
179 switch (acc_strb
->format
) {
180 case PIPE_FORMAT_R16G16B16A16_SNORM
:
183 for (i
= 0; i
< height
; i
++) {
184 GLshort
*acc
= (GLshort
*) (map
+ (ypos
+ i
) * acc_pt
->stride
+ xpos
* 8);
185 for (j
= 0; j
< width
* 4; j
++) {
186 float val
= SHORT_TO_FLOAT(acc
[j
]) * scale
+ bias
;
187 acc
[j
] = FLOAT_TO_SHORT(val
);
193 _mesa_problem(NULL
, "unexpected format in st_clear_accum_buffer()");
196 screen
->transfer_unmap(screen
, acc_pt
);
197 screen
->tex_transfer_destroy(acc_pt
);
202 accum_accum(struct st_context
*st
, GLfloat value
,
203 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
204 struct st_renderbuffer
*acc_strb
,
205 struct st_renderbuffer
*color_strb
)
207 struct pipe_context
*pipe
= st
->pipe
;
208 struct pipe_screen
*screen
= pipe
->screen
;
209 struct pipe_transfer
*acc_trans
, *color_trans
;
210 GLfloat
*colorBuf
, *accBuf
;
213 st_teximage_flush_before_map(st
, acc_strb
->texture
, 0, 0,
216 acc_trans
= screen
->get_tex_transfer(screen
, acc_strb
->texture
, 0, 0, 0,
217 PIPE_TRANSFER_READ
, xpos
, ypos
,
220 st_teximage_flush_before_map(st
, color_strb
->texture
, 0, 0,
223 color_trans
= screen
->get_tex_transfer(screen
, color_strb
->texture
, 0, 0, 0,
224 PIPE_TRANSFER_READ
, xpos
, ypos
,
227 colorBuf
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
228 accBuf
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
230 pipe_get_tile_rgba(color_trans
, 0, 0, width
, height
, colorBuf
);
231 acc_get_tile_rgba(pipe
, acc_trans
, 0, 0, width
, height
, accBuf
);
233 for (i
= 0; i
< 4 * width
* height
; i
++) {
234 accBuf
[i
] = accBuf
[i
] + colorBuf
[i
] * value
;
237 screen
->tex_transfer_destroy(acc_trans
);
238 acc_trans
= screen
->get_tex_transfer(screen
, acc_strb
->texture
, 0, 0, 0,
239 PIPE_TRANSFER_WRITE
, xpos
, ypos
,
242 acc_put_tile_rgba(pipe
, acc_trans
, 0, 0, width
, height
, accBuf
);
244 _mesa_free(colorBuf
);
246 screen
->tex_transfer_destroy(acc_trans
);
247 screen
->tex_transfer_destroy(color_trans
);
252 accum_load(struct st_context
*st
, GLfloat value
,
253 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
254 struct st_renderbuffer
*acc_strb
,
255 struct st_renderbuffer
*color_strb
)
257 struct pipe_context
*pipe
= st
->pipe
;
258 struct pipe_screen
*screen
= pipe
->screen
;
259 struct pipe_transfer
*acc_trans
, *color_trans
;
263 st_teximage_flush_before_map(st
, acc_strb
->texture
, 0, 0,
264 PIPE_TRANSFER_WRITE
);
266 acc_trans
= screen
->get_tex_transfer(screen
, acc_strb
->texture
, 0, 0, 0,
267 PIPE_TRANSFER_WRITE
, xpos
, ypos
,
270 st_teximage_flush_before_map(st
, color_strb
->texture
, 0, 0,
273 color_trans
= screen
->get_tex_transfer(screen
, color_strb
->texture
, 0, 0, 0,
274 PIPE_TRANSFER_READ
, xpos
, ypos
,
277 buf
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
279 pipe_get_tile_rgba(color_trans
, 0, 0, width
, height
, buf
);
281 for (i
= 0; i
< 4 * width
* height
; i
++) {
282 buf
[i
] = buf
[i
] * value
;
285 acc_put_tile_rgba(pipe
, acc_trans
, 0, 0, width
, height
, buf
);
288 screen
->tex_transfer_destroy(acc_trans
);
289 screen
->tex_transfer_destroy(color_trans
);
294 accum_return(GLcontext
*ctx
, GLfloat value
,
295 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
296 struct st_renderbuffer
*acc_strb
,
297 struct st_renderbuffer
*color_strb
)
299 struct pipe_context
*pipe
= ctx
->st
->pipe
;
300 struct pipe_screen
*screen
= pipe
->screen
;
301 const GLubyte
*colormask
= ctx
->Color
.ColorMask
;
302 struct pipe_transfer
*acc_trans
, *color_trans
;
303 GLfloat
*abuf
, *cbuf
= NULL
;
306 abuf
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
308 st_teximage_flush_before_map(ctx
->st
, acc_strb
->texture
, 0, 0,
311 acc_trans
= screen
->get_tex_transfer(screen
, acc_strb
->texture
, 0, 0, 0,
312 PIPE_TRANSFER_READ
, xpos
, ypos
,
315 st_teximage_flush_before_map(ctx
->st
, color_strb
->texture
, 0, 0,
316 PIPE_TRANSFER_READ_WRITE
);
318 color_trans
= screen
->get_tex_transfer(screen
, color_strb
->texture
, 0, 0, 0,
319 PIPE_TRANSFER_READ_WRITE
, xpos
, ypos
,
322 acc_get_tile_rgba(pipe
, acc_trans
, 0, 0, width
, height
, abuf
);
324 if (!colormask
[0] || !colormask
[1] || !colormask
[2] || !colormask
[3]) {
325 cbuf
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
326 pipe_get_tile_rgba(color_trans
, 0, 0, width
, height
, cbuf
);
329 for (i
= 0; i
< width
* height
; i
++) {
330 for (ch
= 0; ch
< 4; ch
++) {
332 GLfloat val
= abuf
[i
* 4 + ch
] * value
;
333 abuf
[i
* 4 + ch
] = CLAMP(val
, 0.0f
, 1.0f
);
336 abuf
[i
* 4 + ch
] = cbuf
[i
* 4 + ch
];
341 pipe_put_tile_rgba(color_trans
, 0, 0, width
, height
, abuf
);
346 screen
->tex_transfer_destroy(acc_trans
);
347 screen
->tex_transfer_destroy(color_trans
);
352 st_Accum(GLcontext
*ctx
, GLenum op
, GLfloat value
)
354 struct st_context
*st
= ctx
->st
;
355 struct st_renderbuffer
*acc_strb
356 = st_renderbuffer(ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
);
357 struct st_renderbuffer
*color_strb
358 = st_renderbuffer(ctx
->ReadBuffer
->_ColorReadBuffer
);
360 const GLint xpos
= ctx
->DrawBuffer
->_Xmin
;
361 const GLint ypos
= ctx
->DrawBuffer
->_Ymin
;
362 const GLint width
= ctx
->DrawBuffer
->_Xmax
- xpos
;
363 const GLint height
= ctx
->DrawBuffer
->_Ymax
- ypos
;
365 /* make sure color bufs aren't cached */
366 st_flush( st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
371 accum_mad(ctx
, 1.0, value
, xpos
, ypos
, width
, height
, acc_strb
);
376 accum_mad(ctx
, value
, 0.0, xpos
, ypos
, width
, height
, acc_strb
);
381 accum_accum(st
, value
, xpos
, ypos
, width
, height
, acc_strb
, color_strb
);
385 accum_load(st
, value
, xpos
, ypos
, width
, height
, acc_strb
, color_strb
);
388 accum_return(ctx
, value
, xpos
, ypos
, width
, height
, acc_strb
, color_strb
);
397 void st_init_accum_functions(struct dd_function_table
*functions
)
399 functions
->Accum
= st_Accum
;