2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * Code to allow the softpipe code to write to X windows/buffers.
29 * This is a bit of a hack for now. We've basically got two different
30 * abstractions for color buffers: gl_renderbuffer and softpipe_surface.
31 * They'll need to get merged someday...
32 * For now, they're separate things that point to each other.
36 #include "glxheader.h"
42 #include "framebuffer.h"
43 #include "renderbuffer.h"
45 #include "pipe/p_context.h"
46 #include "pipe/p_defines.h"
47 #include "pipe/softpipe/sp_context.h"
48 #include "pipe/softpipe/sp_clear.h"
49 #include "state_tracker/st_context.h"
54 if (x + w > ps->width) \
56 if (y + h > ps->height) \
61 static INLINE
struct xmesa_surface
*
62 xmesa_surf(struct softpipe_surface
*sps
)
64 return (struct xmesa_surface
*) sps
;
68 static INLINE
struct xmesa_renderbuffer
*
69 xmesa_rb(struct softpipe_surface
*sps
)
71 struct xmesa_surface
*xms
= xmesa_surf(sps
);
76 #define FLIP(Y) Y = xrb->St.Base.Height - (Y) - 1;
80 * quad reading/writing
81 * These functions are just wrappers around the existing renderbuffer
83 * Note that Y=0 is the top of the surface.
87 read_quad_f(struct softpipe_surface
*sps
, GLint x
, GLint y
,
88 GLfloat (*rgba
)[NUM_CHANNELS
])
90 struct xmesa_renderbuffer
*xrb
= xmesa_rb(sps
);
92 GLfloat
*dst
= (GLfloat
*) rgba
;
94 GET_CURRENT_CONTEXT(ctx
);
96 xrb
->St
.Base
.GetRow(ctx
, &xrb
->St
.Base
, 2, x
, y
, temp
);
97 xrb
->St
.Base
.GetRow(ctx
, &xrb
->St
.Base
, 2, x
, y
- 1, temp
+ 8);
98 for (i
= 0; i
< 16; i
++) {
99 dst
[i
] = UBYTE_TO_FLOAT(temp
[i
]);
104 read_quad_f_swz(struct softpipe_surface
*sps
, GLint x
, GLint y
,
105 GLfloat (*rrrr
)[QUAD_SIZE
])
107 struct xmesa_renderbuffer
*xrb
= xmesa_rb(sps
);
109 GLfloat
*dst
= (GLfloat
*) rrrr
;
111 GET_CURRENT_CONTEXT(ctx
);
113 xrb
->St
.Base
.GetRow(ctx
, &xrb
->St
.Base
, 2, x
, y
, temp
);
114 xrb
->St
.Base
.GetRow(ctx
, &xrb
->St
.Base
, 2, x
, y
- 1, temp
+ 8);
115 for (i
= 0; i
< 4; i
++) {
116 for (j
= 0; j
< 4; j
++) {
117 dst
[j
* 4 + i
] = UBYTE_TO_FLOAT(temp
[i
* 4 + j
]);
123 write_quad_f(struct softpipe_surface
*sps
, GLint x
, GLint y
,
124 GLfloat (*rgba
)[NUM_CHANNELS
])
126 struct xmesa_renderbuffer
*xrb
= xmesa_rb(sps
);
128 const GLfloat
*src
= (const GLfloat
*) rgba
;
130 GET_CURRENT_CONTEXT(ctx
);
132 for (i
= 0; i
< 16; i
++) {
133 UNCLAMPED_FLOAT_TO_UBYTE(temp
[i
], src
[i
]);
135 xrb
->St
.Base
.PutRow(ctx
, &xrb
->St
.Base
, 2, x
, y
, temp
, NULL
);
136 xrb
->St
.Base
.PutRow(ctx
, &xrb
->St
.Base
, 2, x
, y
- 1, temp
+ 8, NULL
);
140 write_quad_f_swz(struct softpipe_surface
*sps
, GLint x
, GLint y
,
141 GLfloat (*rrrr
)[QUAD_SIZE
])
143 struct xmesa_renderbuffer
*xrb
= xmesa_rb(sps
);
145 const GLfloat
*src
= (const GLfloat
*) rrrr
;
147 GET_CURRENT_CONTEXT(ctx
);
149 for (i
= 0; i
< 4; i
++) {
150 for (j
= 0; j
< 4; j
++) {
151 UNCLAMPED_FLOAT_TO_UBYTE(temp
[j
* 4 + i
], src
[i
* 4 + j
]);
154 xrb
->St
.Base
.PutRow(ctx
, &xrb
->St
.Base
, 2, x
, y
, temp
, NULL
);
155 xrb
->St
.Base
.PutRow(ctx
, &xrb
->St
.Base
, 2, x
, y
- 1, temp
+ 8, NULL
);
159 read_quad_ub(struct softpipe_surface
*sps
, GLint x
, GLint y
,
160 GLubyte (*rgba
)[NUM_CHANNELS
])
162 struct xmesa_renderbuffer
*xrb
= xmesa_rb(sps
);
163 GET_CURRENT_CONTEXT(ctx
);
165 xrb
->St
.Base
.GetRow(ctx
, &xrb
->St
.Base
, 2, x
, y
, rgba
);
166 xrb
->St
.Base
.GetRow(ctx
, &xrb
->St
.Base
, 2, x
, y
- 1, rgba
+ 2);
170 write_quad_ub(struct softpipe_surface
*sps
, GLint x
, GLint y
,
171 GLubyte (*rgba
)[NUM_CHANNELS
])
173 struct xmesa_renderbuffer
*xrb
= xmesa_rb(sps
);
174 GET_CURRENT_CONTEXT(ctx
);
176 xrb
->St
.Base
.GetRow(ctx
, &xrb
->St
.Base
, 2, x
, y
, rgba
);
177 xrb
->St
.Base
.GetRow(ctx
, &xrb
->St
.Base
, 2, x
, y
- 1, rgba
+ 2);
182 get_tile(struct pipe_surface
*ps
,
183 GLuint x
, GLuint y
, GLuint w
, GLuint h
, GLfloat
*p
)
185 struct xmesa_renderbuffer
*xrb
= xmesa_rb((struct softpipe_surface
*) ps
);
186 GLubyte tmp
[MAX_WIDTH
* 4];
189 GET_CURRENT_CONTEXT(ctx
);
194 for (i
= 0; i
< h
; i
++) {
195 xrb
->St
.Base
.GetRow(ctx
, &xrb
->St
.Base
, w
, x
, y
- i
, tmp
);
196 for (j
= 0; j
< w
* 4; j
++) {
197 p
[j
] = UBYTE_TO_FLOAT(tmp
[j
]);
205 put_tile(struct pipe_surface
*ps
,
206 GLuint x
, GLuint y
, GLuint w
, GLuint h
, const GLfloat
*p
)
208 struct xmesa_renderbuffer
*xrb
= xmesa_rb((struct softpipe_surface
*) ps
);
209 GLubyte tmp
[MAX_WIDTH
* 4];
212 GET_CURRENT_CONTEXT(ctx
);
215 for (i
= 0; i
< h
; i
++) {
216 for (j
= 0; j
< w
* 4; j
++) {
217 UNCLAMPED_FLOAT_TO_UBYTE(tmp
[j
], p
[j
]);
219 xrb
->St
.Base
.PutRow(ctx
, &xrb
->St
.Base
, w
, x
, y
- i
, tmp
, NULL
);
227 * Called to create a pipe_surface for each X renderbuffer.
228 * Note: this is being used instead of pipe->surface_alloc() since we
229 * have special/unique quad read/write functions for X.
231 struct pipe_surface
*
232 xmesa_new_color_surface(struct pipe_context
*pipe
, GLuint pipeFormat
)
234 struct xmesa_surface
*xms
= CALLOC_STRUCT(xmesa_surface
);
238 xms
->surface
.surface
.format
= pipeFormat
;
239 xms
->surface
.surface
.refcount
= 1;
241 /* some of the functions plugged in by this call will get overridden */
242 softpipe_init_surface_funcs(&xms
->surface
);
244 switch (pipeFormat
) {
245 case PIPE_FORMAT_U_A8_R8_G8_B8
:
246 xms
->surface
.read_quad_f_swz
= read_quad_f
;
247 xms
->surface
.read_quad_f
= read_quad_f
;
248 xms
->surface
.read_quad_f_swz
= read_quad_f_swz
;
249 xms
->surface
.read_quad_ub
= read_quad_ub
;
250 xms
->surface
.write_quad_f
= write_quad_f
;
251 xms
->surface
.write_quad_f_swz
= write_quad_f_swz
;
252 xms
->surface
.write_quad_ub
= write_quad_ub
;
253 xms
->surface
.surface
.get_tile
= get_tile
;
254 xms
->surface
.surface
.put_tile
= put_tile
;
256 case PIPE_FORMAT_S8_Z24
:
258 xms->surface.read_quad_z = 1;
259 xms->surface.write_quad_z = 1;
266 /* Note, the region we allocate doesn't actually have any storage
267 * since we're drawing into an XImage or Pixmap.
268 * The region's size will get set in the xmesa_alloc_front/back_storage()
272 xms
->surface
.surface
.region
= pipe
->region_alloc(pipe
, 1, 0, 0, 0x0);
274 return &xms
->surface
.surface
;
279 * Called via pipe->surface_alloc() to create new surfaces (textures,
280 * renderbuffers, etc.
282 struct pipe_surface
*
283 xmesa_surface_alloc(struct pipe_context
*pipe
, GLuint pipeFormat
)
285 struct xmesa_surface
*xms
= CALLOC_STRUCT(xmesa_surface
);
289 xms
->surface
.surface
.format
= pipeFormat
;
290 xms
->surface
.surface
.refcount
= 1;
292 * This is really just a softpipe surface, not an XImage/Pixmap surface.
294 softpipe_init_surface_funcs(&xms
->surface
);
297 xms
->surface
.surface
.region
= pipe
->region_alloc(pipe
, 1, 1, 1, 0x0);
299 return &xms
->surface
.surface
;
304 xmesa_supported_formats(struct pipe_context
*pipe
, GLuint
*numFormats
)
306 static const GLuint formats
[] = {
307 PIPE_FORMAT_U_A8_R8_G8_B8
,
308 PIPE_FORMAT_S_R16_G16_B16_A16
,
312 *numFormats
= sizeof(formats
) / sizeof(formats
[0]);
319 * Called via pipe->clear()
322 xmesa_clear(struct pipe_context
*pipe
, struct pipe_surface
*ps
, GLuint value
)
324 struct xmesa_renderbuffer
*xrb
= xmesa_rb((struct softpipe_surface
*) ps
);
326 if (xrb
&& xrb
->ximage
) {
327 /* clearing back color buffer */
328 GET_CURRENT_CONTEXT(ctx
);
329 xmesa_clear_buffers(ctx
, BUFFER_BIT_BACK_LEFT
);
331 else if (xrb
&& xrb
->pixmap
) {
332 /* clearing front color buffer */
333 GET_CURRENT_CONTEXT(ctx
);
334 xmesa_clear_buffers(ctx
, BUFFER_BIT_FRONT_LEFT
);
337 /* clearing other buffer */
338 softpipe_clear(pipe
, ps
, value
);