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_format.h"
42 #include "pipe/p_context.h"
43 #include "pipe/p_defines.h"
44 #include "pipe/p_inlines.h"
45 #include "util/p_tile.h"
48 #define UNCLAMPED_FLOAT_TO_SHORT(us, f) \
49 us = ( (short) ( CLAMP((f), -1.0, 1.0) * 32767.0F) )
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 * Wrapper for pipe_get_tile_rgba(). Do format/cpp override to make the
61 * tile util function think the surface is 16bit/channel, even if it's not.
62 * See also: st_renderbuffer_alloc_storage()
65 acc_get_tile_rgba(struct pipe_context
*pipe
, struct pipe_surface
*acc_ps
,
66 uint x
, uint y
, uint w
, uint h
, float *p
)
68 const enum pipe_format f
= acc_ps
->format
;
69 const int cpp
= acc_ps
->cpp
;
71 acc_ps
->format
= DEFAULT_ACCUM_PIPE_FORMAT
;
74 pipe_get_tile_rgba(pipe
, acc_ps
, x
, y
, w
, h
, p
);
82 * Wrapper for pipe_put_tile_rgba(). Do format/cpp override to make the
83 * tile util function think the surface is 16bit/channel, even if it's not.
84 * See also: st_renderbuffer_alloc_storage()
87 acc_put_tile_rgba(struct pipe_context
*pipe
, struct pipe_surface
*acc_ps
,
88 uint x
, uint y
, uint w
, uint h
, const float *p
)
90 enum pipe_format f
= acc_ps
->format
;
91 const int cpp
= acc_ps
->cpp
;
93 acc_ps
->format
= DEFAULT_ACCUM_PIPE_FORMAT
;
96 pipe_put_tile_rgba(pipe
, acc_ps
, x
, y
, w
, h
, p
);
105 st_clear_accum_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
107 struct st_renderbuffer
*acc_strb
= st_renderbuffer(rb
);
108 struct pipe_surface
*acc_ps
;
109 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
110 const GLint xpos
= ctx
->DrawBuffer
->_Xmin
;
111 const GLint ypos
= ctx
->DrawBuffer
->_Ymin
;
112 const GLint width
= ctx
->DrawBuffer
->_Xmax
- xpos
;
113 const GLint height
= ctx
->DrawBuffer
->_Ymax
- ypos
;
116 acc_ps
= screen
->get_tex_surface(screen
, acc_strb
->texture
, 0, 0, 0,
117 PIPE_BUFFER_USAGE_CPU_WRITE
);
118 map
= screen
->surface_map(screen
, acc_ps
,
119 PIPE_BUFFER_USAGE_CPU_WRITE
);
121 /* note acc_strb->format might not equal acc_ps->format */
122 switch (acc_strb
->format
) {
123 case PIPE_FORMAT_R16G16B16A16_SNORM
:
125 GLshort r
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[0]);
126 GLshort g
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[1]);
127 GLshort b
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[2]);
128 GLshort a
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[3]);
130 for (i
= 0; i
< height
; i
++) {
131 GLshort
*dst
= ((GLshort
*) map
132 + ((ypos
+ i
) * acc_ps
->pitch
+ xpos
) * 4);
133 for (j
= 0; j
< width
; j
++) {
144 _mesa_problem(ctx
, "unexpected format in st_clear_accum_buffer()");
147 screen
->surface_unmap(screen
, acc_ps
);
148 pipe_surface_reference(&acc_ps
, NULL
);
154 accum_mad(GLcontext
*ctx
, GLfloat scale
, GLfloat bias
,
155 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
156 struct st_renderbuffer
*acc_strb
)
158 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
159 struct pipe_surface
*acc_ps
= acc_strb
->surface
;
162 map
= screen
->surface_map(screen
, acc_ps
,
163 PIPE_BUFFER_USAGE_CPU_WRITE
);
165 /* note acc_strb->format might not equal acc_ps->format */
166 switch (acc_strb
->format
) {
167 case PIPE_FORMAT_R16G16B16A16_SNORM
:
170 for (i
= 0; i
< height
; i
++) {
171 GLshort
*acc
= ((GLshort
*) map
172 + ((ypos
+ i
) * acc_ps
->pitch
+ xpos
) * 4);
173 for (j
= 0; j
< width
* 4; j
++) {
174 float val
= SHORT_TO_FLOAT(acc
[j
]) * scale
+ bias
;
175 acc
[j
] = FLOAT_TO_SHORT(val
);
181 _mesa_problem(NULL
, "unexpected format in st_clear_accum_buffer()");
184 screen
->surface_unmap(screen
, acc_ps
);
189 accum_accum(struct pipe_context
*pipe
, GLfloat value
,
190 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
191 struct st_renderbuffer
*acc_strb
,
192 struct st_renderbuffer
*color_strb
)
194 struct pipe_screen
*screen
= pipe
->screen
;
195 struct pipe_surface
*acc_surf
, *color_surf
;
196 GLfloat
*colorBuf
, *accBuf
;
199 acc_surf
= screen
->get_tex_surface(screen
, acc_strb
->texture
, 0, 0, 0,
200 (PIPE_BUFFER_USAGE_CPU_WRITE
|
201 PIPE_BUFFER_USAGE_CPU_READ
));
203 color_surf
= screen
->get_tex_surface(screen
, color_strb
->texture
, 0, 0, 0,
204 PIPE_BUFFER_USAGE_CPU_READ
);
206 colorBuf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
207 accBuf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
209 pipe_get_tile_rgba(pipe
, color_surf
, xpos
, ypos
, width
, height
, colorBuf
);
210 acc_get_tile_rgba(pipe
, acc_surf
, xpos
, ypos
, width
, height
, accBuf
);
212 for (i
= 0; i
< 4 * width
* height
; i
++) {
213 accBuf
[i
] = accBuf
[i
] + colorBuf
[i
] * value
;
216 acc_put_tile_rgba(pipe
, acc_surf
, xpos
, ypos
, width
, height
, accBuf
);
220 pipe_surface_reference(&acc_surf
, NULL
);
221 pipe_surface_reference(&color_surf
, NULL
);
226 accum_load(struct pipe_context
*pipe
, GLfloat value
,
227 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
228 struct st_renderbuffer
*acc_strb
,
229 struct st_renderbuffer
*color_strb
)
231 struct pipe_screen
*screen
= pipe
->screen
;
232 struct pipe_surface
*acc_surf
, *color_surf
;
236 acc_surf
= screen
->get_tex_surface(screen
, acc_strb
->texture
, 0, 0, 0,
237 PIPE_BUFFER_USAGE_CPU_WRITE
);
239 color_surf
= screen
->get_tex_surface(screen
, color_strb
->texture
, 0, 0, 0,
240 PIPE_BUFFER_USAGE_CPU_READ
);
242 buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
244 pipe_get_tile_rgba(pipe
, color_surf
, xpos
, ypos
, width
, height
, buf
);
246 for (i
= 0; i
< 4 * width
* height
; i
++) {
247 buf
[i
] = buf
[i
] * value
;
250 acc_put_tile_rgba(pipe
, acc_surf
, xpos
, ypos
, width
, height
, buf
);
253 pipe_surface_reference(&acc_surf
, NULL
);
254 pipe_surface_reference(&color_surf
, NULL
);
259 accum_return(GLcontext
*ctx
, GLfloat value
,
260 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
261 struct st_renderbuffer
*acc_strb
,
262 struct st_renderbuffer
*color_strb
)
264 struct pipe_context
*pipe
= ctx
->st
->pipe
;
265 struct pipe_screen
*screen
= pipe
->screen
;
266 const GLubyte
*colormask
= ctx
->Color
.ColorMask
;
267 struct pipe_surface
*acc_surf
, *color_surf
;
268 GLfloat
*abuf
, *cbuf
= NULL
;
271 abuf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
273 acc_surf
= screen
->get_tex_surface(screen
, acc_strb
->texture
, 0, 0, 0,
274 PIPE_BUFFER_USAGE_CPU_READ
);
276 color_surf
= screen
->get_tex_surface(screen
, color_strb
->texture
, 0, 0, 0,
277 (PIPE_BUFFER_USAGE_CPU_READ
|
278 PIPE_BUFFER_USAGE_CPU_WRITE
));
280 acc_get_tile_rgba(pipe
, acc_surf
, xpos
, ypos
, width
, height
, abuf
);
282 if (!colormask
[0] || !colormask
[1] || !colormask
[2] || !colormask
[3]) {
283 cbuf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
284 pipe_get_tile_rgba(pipe
, color_surf
, xpos
, ypos
, width
, height
, cbuf
);
287 for (i
= 0; i
< width
* height
; i
++) {
288 for (ch
= 0; ch
< 4; ch
++) {
290 GLfloat val
= abuf
[i
* 4 + ch
] * value
;
291 abuf
[i
* 4 + ch
] = CLAMP(val
, 0.0, 1.0);
294 abuf
[i
* 4 + ch
] = cbuf
[i
* 4 + ch
];
299 pipe_put_tile_rgba(pipe
, color_surf
, xpos
, ypos
, width
, height
, abuf
);
304 pipe_surface_reference(&acc_surf
, NULL
);
305 pipe_surface_reference(&color_surf
, NULL
);
310 st_Accum(GLcontext
*ctx
, GLenum op
, GLfloat value
)
312 struct st_context
*st
= ctx
->st
;
313 struct pipe_context
*pipe
= st
->pipe
;
314 struct st_renderbuffer
*acc_strb
315 = st_renderbuffer(ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
);
316 struct st_renderbuffer
*color_strb
317 = st_renderbuffer(ctx
->ReadBuffer
->_ColorReadBuffer
);
319 const GLint xpos
= ctx
->DrawBuffer
->_Xmin
;
320 const GLint ypos
= ctx
->DrawBuffer
->_Ymin
;
321 const GLint width
= ctx
->DrawBuffer
->_Xmax
- xpos
;
322 const GLint height
= ctx
->DrawBuffer
->_Ymax
- ypos
;
324 /* make sure color bufs aren't cached */
325 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
330 accum_mad(ctx
, 1.0, value
, xpos
, ypos
, width
, height
, acc_strb
);
335 accum_mad(ctx
, value
, 0.0, xpos
, ypos
, width
, height
, acc_strb
);
340 accum_accum(pipe
, value
, xpos
, ypos
, width
, height
, acc_strb
, color_strb
);
344 accum_load(pipe
, value
, xpos
, ypos
, width
, height
, acc_strb
, color_strb
);
347 accum_return(ctx
, value
, xpos
, ypos
, width
, height
, acc_strb
, color_strb
);
356 void st_init_accum_functions(struct dd_function_table
*functions
)
358 functions
->Accum
= st_Accum
;