gallium: create drawing surfaces as GPU_READ/WRITE only
[mesa.git] / src / mesa / state_tracker / st_cb_fbo.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28
29 /**
30 * Framebuffer/renderbuffer functions.
31 *
32 * \author Brian Paul
33 */
34
35
36 #include "main/imports.h"
37 #include "main/context.h"
38 #include "main/fbobject.h"
39 #include "main/framebuffer.h"
40 #include "main/renderbuffer.h"
41
42 #include "pipe/p_context.h"
43 #include "pipe/p_defines.h"
44 #include "pipe/p_inlines.h"
45 #include "pipe/p_winsys.h"
46 #include "st_context.h"
47 #include "st_cb_fbo.h"
48 #include "st_cb_texture.h"
49 #include "st_format.h"
50 #include "st_public.h"
51 #include "st_texture.h"
52
53
54
55 /**
56 * Compute the renderbuffer's Red/Green/EtcBit fields from the pipe format.
57 */
58 static int
59 init_renderbuffer_bits(struct st_renderbuffer *strb,
60 enum pipe_format pipeFormat)
61 {
62 struct pipe_format_info info;
63
64 if (!st_get_format_info( pipeFormat, &info )) {
65 assert( 0 );
66 }
67
68 strb->Base._ActualFormat = info.base_format;
69 strb->Base.RedBits = info.red_bits;
70 strb->Base.GreenBits = info.green_bits;
71 strb->Base.BlueBits = info.blue_bits;
72 strb->Base.AlphaBits = info.alpha_bits;
73 strb->Base.DepthBits = info.depth_bits;
74 strb->Base.StencilBits = info.stencil_bits;
75 strb->Base.DataType = st_format_datatype(pipeFormat);
76
77 return info.size;
78 }
79
80 static INLINE GLboolean pf_is_depth_stencil( enum pipe_format format )
81 {
82 return (pf_get_component_bits( format, PIPE_FORMAT_COMP_Z ) +
83 pf_get_component_bits( format, PIPE_FORMAT_COMP_S )) != 0;
84 }
85
86 /**
87 * gl_renderbuffer::AllocStorage()
88 * This is called to allocate the original drawing surface, and
89 * during window resize.
90 */
91 static GLboolean
92 st_renderbuffer_alloc_storage(GLcontext * ctx, struct gl_renderbuffer *rb,
93 GLenum internalFormat,
94 GLuint width, GLuint height)
95 {
96 struct pipe_context *pipe = ctx->st->pipe;
97 struct st_renderbuffer *strb = st_renderbuffer(rb);
98 struct pipe_texture template;
99 unsigned surface_usage;
100
101 /* Free the old surface (and texture if we hold the last
102 * reference):
103 */
104 pipe_surface_reference( &strb->surface, NULL );
105
106 memset(&template, 0, sizeof(template));
107
108 if (strb->format != PIPE_FORMAT_NONE) {
109 template.format = strb->format;
110 }
111 else {
112 template.format = st_choose_renderbuffer_format(pipe, internalFormat);
113 }
114
115 strb->Base.Width = width;
116 strb->Base.Height = height;
117 init_renderbuffer_bits(strb, template.format);
118
119 template.target = PIPE_TEXTURE_2D;
120 template.compressed = 0;
121 template.cpp = pf_get_size(template.format);
122 template.width[0] = width;
123 template.height[0] = height;
124 template.depth[0] = 1;
125 template.last_level = 0;
126
127 if (pf_is_depth_stencil(template.format)) {
128 template.tex_usage = PIPE_TEXTURE_USAGE_DEPTH_STENCIL;
129 }
130 else {
131 template.tex_usage = (PIPE_TEXTURE_USAGE_DISPLAY_TARGET |
132 PIPE_TEXTURE_USAGE_RENDER_TARGET);
133 }
134
135
136 /* Probably need dedicated flags for surface usage too:
137 */
138 surface_usage = (PIPE_BUFFER_USAGE_GPU_READ |
139 PIPE_BUFFER_USAGE_GPU_WRITE);
140 #if 0
141 PIPE_BUFFER_USAGE_CPU_READ |
142 PIPE_BUFFER_USAGE_CPU_WRITE);
143 #endif
144
145 strb->texture = pipe->screen->texture_create( pipe->screen,
146 &template );
147
148 /* Special path for accum buffers.
149 *
150 * Try a different surface format. Since accum buffers are s/w
151 * only for now, the surface pixel format doesn't really matter,
152 * only that the buffer is large enough.
153 */
154 if (!strb->texture && template.format == DEFAULT_ACCUM_PIPE_FORMAT)
155 {
156 /* Actually, just setting this usage value should be sufficient
157 * to tell the driver to go ahead and allocate the buffer, even
158 * if HW doesn't support the format.
159 */
160 template.tex_usage = 0;
161 surface_usage = (PIPE_BUFFER_USAGE_CPU_READ |
162 PIPE_BUFFER_USAGE_CPU_WRITE);
163
164 strb->texture = pipe->screen->texture_create( pipe->screen,
165 &template );
166
167 }
168
169 if (!strb->texture)
170 return FALSE;
171
172 strb->surface = pipe->screen->get_tex_surface( pipe->screen,
173 strb->texture,
174 0, 0, 0,
175 surface_usage );
176
177 assert(strb->surface->buffer);
178 assert(strb->surface->format);
179 assert(strb->surface->cpp);
180 assert(strb->surface->width == width);
181 assert(strb->surface->height == height);
182 assert(strb->surface->pitch);
183
184
185 return strb->surface != NULL;
186 }
187
188
189 /**
190 * gl_renderbuffer::Delete()
191 */
192 static void
193 st_renderbuffer_delete(struct gl_renderbuffer *rb)
194 {
195 struct st_renderbuffer *strb = st_renderbuffer(rb);
196 ASSERT(strb);
197 pipe_surface_reference(&strb->surface, NULL);
198 pipe_texture_reference(&strb->texture, NULL);
199 free(strb);
200 }
201
202
203 /**
204 * gl_renderbuffer::GetPointer()
205 */
206 static void *
207 null_get_pointer(GLcontext * ctx, struct gl_renderbuffer *rb,
208 GLint x, GLint y)
209 {
210 /* By returning NULL we force all software rendering to go through
211 * the span routines.
212 */
213 #if 0
214 assert(0); /* Should never get called with softpipe */
215 #endif
216 return NULL;
217 }
218
219
220 /**
221 * Called via ctx->Driver.NewFramebuffer()
222 */
223 static struct gl_framebuffer *
224 st_new_framebuffer(GLcontext *ctx, GLuint name)
225 {
226 /* XXX not sure we need to subclass gl_framebuffer for pipe */
227 return _mesa_new_framebuffer(ctx, name);
228 }
229
230
231 /**
232 * Called via ctx->Driver.NewRenderbuffer()
233 */
234 static struct gl_renderbuffer *
235 st_new_renderbuffer(GLcontext *ctx, GLuint name)
236 {
237 struct st_renderbuffer *strb = CALLOC_STRUCT(st_renderbuffer);
238 if (strb) {
239 _mesa_init_renderbuffer(&strb->Base, name);
240 strb->Base.Delete = st_renderbuffer_delete;
241 strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
242 strb->Base.GetPointer = null_get_pointer;
243 strb->format = PIPE_FORMAT_NONE;
244 return &strb->Base;
245 }
246 return NULL;
247 }
248
249
250 /**
251 * Allocate a renderbuffer for a an on-screen window (not a user-created
252 * renderbuffer). The window system code determines the format.
253 */
254 struct gl_renderbuffer *
255 st_new_renderbuffer_fb(enum pipe_format format)
256 {
257 struct st_renderbuffer *strb;
258
259 strb = CALLOC_STRUCT(st_renderbuffer);
260 if (!strb) {
261 _mesa_error(NULL, GL_OUT_OF_MEMORY, "creating renderbuffer");
262 return NULL;
263 }
264
265 _mesa_init_renderbuffer(&strb->Base, 0);
266 strb->Base.ClassID = 0x4242; /* just a unique value */
267 strb->format = format;
268
269 switch (format) {
270 case PIPE_FORMAT_A8R8G8B8_UNORM:
271 case PIPE_FORMAT_B8G8R8A8_UNORM:
272 case PIPE_FORMAT_X8R8G8B8_UNORM:
273 case PIPE_FORMAT_B8G8R8X8_UNORM:
274 case PIPE_FORMAT_A1R5G5B5_UNORM:
275 case PIPE_FORMAT_A4R4G4B4_UNORM:
276 case PIPE_FORMAT_R5G6B5_UNORM:
277 strb->Base.InternalFormat = GL_RGBA;
278 strb->Base._BaseFormat = GL_RGBA;
279 break;
280 case PIPE_FORMAT_Z16_UNORM:
281 strb->Base.InternalFormat = GL_DEPTH_COMPONENT16;
282 strb->Base._BaseFormat = GL_DEPTH_COMPONENT;
283 break;
284 case PIPE_FORMAT_Z32_UNORM:
285 strb->Base.InternalFormat = GL_DEPTH_COMPONENT32;
286 strb->Base._BaseFormat = GL_DEPTH_COMPONENT;
287 break;
288 case PIPE_FORMAT_S8Z24_UNORM:
289 case PIPE_FORMAT_Z24S8_UNORM:
290 case PIPE_FORMAT_X8Z24_UNORM:
291 case PIPE_FORMAT_Z24X8_UNORM:
292 strb->Base.InternalFormat = GL_DEPTH24_STENCIL8_EXT;
293 strb->Base._BaseFormat = GL_DEPTH_STENCIL_EXT;
294 break;
295 case PIPE_FORMAT_S8_UNORM:
296 strb->Base.InternalFormat = GL_STENCIL_INDEX8_EXT;
297 strb->Base._BaseFormat = GL_STENCIL_INDEX;
298 break;
299 case DEFAULT_ACCUM_PIPE_FORMAT: /*PIPE_FORMAT_R16G16B16A16_SNORM*/
300 strb->Base.InternalFormat = GL_RGBA16;
301 strb->Base._BaseFormat = GL_RGBA;
302 break;
303 default:
304 _mesa_problem(NULL,
305 "Unexpected format in st_new_renderbuffer_fb");
306 return NULL;
307 }
308
309 /* st-specific methods */
310 strb->Base.Delete = st_renderbuffer_delete;
311 strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
312 strb->Base.GetPointer = null_get_pointer;
313
314 /* surface is allocated in st_renderbuffer_alloc_storage() */
315 strb->surface = NULL;
316
317 return &strb->Base;
318 }
319
320
321
322
323 /**
324 * Called via ctx->Driver.BindFramebufferEXT().
325 */
326 static void
327 st_bind_framebuffer(GLcontext *ctx, GLenum target,
328 struct gl_framebuffer *fb, struct gl_framebuffer *fbread)
329 {
330
331 }
332
333 /**
334 * Called by ctx->Driver.FramebufferRenderbuffer
335 */
336 static void
337 st_framebuffer_renderbuffer(GLcontext *ctx,
338 struct gl_framebuffer *fb,
339 GLenum attachment,
340 struct gl_renderbuffer *rb)
341 {
342 /* XXX no need for derivation? */
343 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb);
344 }
345
346
347 /**
348 * Called by ctx->Driver.RenderTexture
349 */
350 static void
351 st_render_texture(GLcontext *ctx,
352 struct gl_framebuffer *fb,
353 struct gl_renderbuffer_attachment *att)
354 {
355 struct st_context *st = ctx->st;
356 struct st_renderbuffer *strb;
357 struct gl_renderbuffer *rb;
358 struct pipe_context *pipe = st->pipe;
359 struct pipe_screen *screen = pipe->screen;
360 struct pipe_texture *pt;
361
362 assert(!att->Renderbuffer);
363
364 /* create new renderbuffer which wraps the texture image */
365 rb = st_new_renderbuffer(ctx, 0);
366 if (!rb) {
367 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glFramebufferTexture()");
368 return;
369 }
370
371 _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
372 assert(rb->RefCount == 1);
373 rb->AllocStorage = NULL; /* should not get called */
374 strb = st_renderbuffer(rb);
375
376 /* get the texture for the texture object */
377 pt = st_get_texobj_texture(att->Texture);
378 assert(pt);
379 assert(pt->width[att->TextureLevel]);
380
381 rb->Width = pt->width[att->TextureLevel];
382 rb->Height = pt->height[att->TextureLevel];
383
384 pipe_texture_reference( &strb->texture, pt );
385
386 /* the renderbuffer's surface is inside the texture */
387 strb->surface = screen->get_tex_surface(screen, pt,
388 att->CubeMapFace,
389 att->TextureLevel,
390 att->Zoffset,
391 PIPE_BUFFER_USAGE_GPU_READ |
392 PIPE_BUFFER_USAGE_GPU_WRITE);
393 assert(strb->surface);
394 assert(screen->is_format_supported(screen, strb->surface->format, PIPE_TEXTURE));
395 assert(screen->is_format_supported(screen, strb->surface->format, PIPE_SURFACE));
396
397 init_renderbuffer_bits(strb, pt->format);
398
399 /*
400 printf("RENDER TO TEXTURE obj=%p pt=%p surf=%p %d x %d\n",
401 att->Texture, pt, strb->surface, rb->Width, rb->Height);
402 */
403
404 /* Invalidate buffer state so that the pipe's framebuffer state
405 * gets updated.
406 * That's where the new renderbuffer (which we just created) gets
407 * passed to the pipe as a (color/depth) render target.
408 */
409 st_invalidate_state(ctx, _NEW_BUFFERS);
410 }
411
412
413 /**
414 * Called via ctx->Driver.FinishRenderTexture.
415 */
416 static void
417 st_finish_render_texture(GLcontext *ctx,
418 struct gl_renderbuffer_attachment *att)
419 {
420 struct pipe_screen *screen = ctx->st->pipe->screen;
421 struct st_renderbuffer *strb = st_renderbuffer(att->Renderbuffer);
422
423 assert(strb);
424
425 ctx->st->pipe->flush(ctx->st->pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
426
427 screen->tex_surface_release( screen, &strb->surface );
428
429 /*
430 printf("FINISH RENDER TO TEXTURE surf=%p\n", strb->surface);
431 */
432
433 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL);
434
435 /* restore previous framebuffer state */
436 st_invalidate_state(ctx, _NEW_BUFFERS);
437 }
438
439
440
441 void st_init_fbo_functions(struct dd_function_table *functions)
442 {
443 functions->NewFramebuffer = st_new_framebuffer;
444 functions->NewRenderbuffer = st_new_renderbuffer;
445 functions->BindFramebuffer = st_bind_framebuffer;
446 functions->FramebufferRenderbuffer = st_framebuffer_renderbuffer;
447 functions->RenderTexture = st_render_texture;
448 functions->FinishRenderTexture = st_finish_render_texture;
449 /* no longer needed by core Mesa, drivers handle resizes...
450 functions->ResizeBuffers = st_resize_buffers;
451 */
452 }