gallium: remove usage of winsys->surface_alloc_storage from state tracker
[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
81 /**
82 * gl_renderbuffer::AllocStorage()
83 * This is called to allocate the original drawing surface, and
84 * during window resize.
85 */
86 static GLboolean
87 st_renderbuffer_alloc_storage(GLcontext * ctx, struct gl_renderbuffer *rb,
88 GLenum internalFormat,
89 GLuint width, GLuint height)
90 {
91 struct pipe_context *pipe = ctx->st->pipe;
92 struct st_renderbuffer *strb = st_renderbuffer(rb);
93
94 struct pipe_texture template, *texture;
95
96 /* Free the old surface (and texture if we hold the last
97 * reference):
98 */
99 pipe_surface_reference( &strb->surface, NULL );
100
101 memset(&template, 0, sizeof(template));
102
103 if (strb->format != PIPE_FORMAT_NONE) {
104 template.format = strb->format;
105 }
106 else {
107 template.format = st_choose_renderbuffer_format(pipe, internalFormat);
108 }
109
110 strb->Base.Width = width;
111 strb->Base.Height = height;
112 init_renderbuffer_bits(strb, template.format);
113
114 template.compressed = 0;
115 template.cpp = pf_get_size(template.format);
116 template.width[0] = width;
117 template.height[0] = height;
118 template.depth[0] = 1;
119 template.last_level = 0;
120 template.usage = (PIPE_BUFFER_USAGE_CPU_WRITE |
121 PIPE_BUFFER_USAGE_CPU_READ |
122 PIPE_BUFFER_USAGE_GPU_WRITE |
123 PIPE_BUFFER_USAGE_GPU_READ);
124
125 texture = pipe->screen->texture_create( pipe->screen,
126 &template );
127
128 /* Special path for accum buffers.
129 *
130 * Try a different surface format. Since accum buffers are s/w
131 * only for now, the surface pixel format doesn't really matter,
132 * only that the buffer is large enough.
133 */
134 if (!texture && template.format == DEFAULT_ACCUM_PIPE_FORMAT)
135 {
136 /* Actually, just setting this usage value should be sufficient
137 * to tell the driver to go ahead and allocate the buffer, even
138 * if HW doesn't support the format.
139 */
140 template.usage = (PIPE_BUFFER_USAGE_CPU_READ |
141 PIPE_BUFFER_USAGE_CPU_WRITE);
142
143 texture = pipe->screen->texture_create( pipe->screen,
144 &template );
145 }
146
147 if (!texture)
148 return FALSE;
149
150 strb->surface = pipe->screen->get_tex_surface( pipe->screen,
151 texture,
152 0, 0, 0,
153 template.usage );
154
155 pipe_texture_reference( &texture, NULL );
156
157 assert(strb->surface->buffer);
158 assert(strb->surface->format);
159 assert(strb->surface->cpp);
160 assert(strb->surface->width == width);
161 assert(strb->surface->height == height);
162 assert(strb->surface->pitch);
163
164
165 return strb->surface != NULL;
166 }
167
168
169 /**
170 * gl_renderbuffer::Delete()
171 */
172 static void
173 st_renderbuffer_delete(struct gl_renderbuffer *rb)
174 {
175 struct st_renderbuffer *strb = st_renderbuffer(rb);
176 ASSERT(strb);
177 pipe_surface_reference(&strb->surface, NULL);
178 free(strb);
179 }
180
181
182 /**
183 * gl_renderbuffer::GetPointer()
184 */
185 static void *
186 null_get_pointer(GLcontext * ctx, struct gl_renderbuffer *rb,
187 GLint x, GLint y)
188 {
189 /* By returning NULL we force all software rendering to go through
190 * the span routines.
191 */
192 #if 0
193 assert(0); /* Should never get called with softpipe */
194 #endif
195 return NULL;
196 }
197
198
199 /**
200 * Called via ctx->Driver.NewFramebuffer()
201 */
202 static struct gl_framebuffer *
203 st_new_framebuffer(GLcontext *ctx, GLuint name)
204 {
205 /* XXX not sure we need to subclass gl_framebuffer for pipe */
206 return _mesa_new_framebuffer(ctx, name);
207 }
208
209
210 /**
211 * Called via ctx->Driver.NewRenderbuffer()
212 */
213 static struct gl_renderbuffer *
214 st_new_renderbuffer(GLcontext *ctx, GLuint name)
215 {
216 struct st_renderbuffer *strb = CALLOC_STRUCT(st_renderbuffer);
217 if (strb) {
218 _mesa_init_renderbuffer(&strb->Base, name);
219 strb->Base.Delete = st_renderbuffer_delete;
220 strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
221 strb->Base.GetPointer = null_get_pointer;
222 strb->format = PIPE_FORMAT_NONE;
223 return &strb->Base;
224 }
225 return NULL;
226 }
227
228
229 /**
230 * Allocate a renderbuffer for a an on-screen window (not a user-created
231 * renderbuffer). The window system code determines the format.
232 */
233 struct gl_renderbuffer *
234 st_new_renderbuffer_fb(enum pipe_format format)
235 {
236 struct st_renderbuffer *strb;
237
238 strb = CALLOC_STRUCT(st_renderbuffer);
239 if (!strb) {
240 _mesa_error(NULL, GL_OUT_OF_MEMORY, "creating renderbuffer");
241 return NULL;
242 }
243
244 _mesa_init_renderbuffer(&strb->Base, 0);
245 strb->Base.ClassID = 0x4242; /* just a unique value */
246 strb->format = format;
247
248 switch (format) {
249 case PIPE_FORMAT_A8R8G8B8_UNORM:
250 case PIPE_FORMAT_B8G8R8A8_UNORM:
251 case PIPE_FORMAT_X8R8G8B8_UNORM:
252 case PIPE_FORMAT_B8G8R8X8_UNORM:
253 case PIPE_FORMAT_A1R5G5B5_UNORM:
254 case PIPE_FORMAT_A4R4G4B4_UNORM:
255 case PIPE_FORMAT_R5G6B5_UNORM:
256 strb->Base.InternalFormat = GL_RGBA;
257 strb->Base._BaseFormat = GL_RGBA;
258 break;
259 case PIPE_FORMAT_Z16_UNORM:
260 strb->Base.InternalFormat = GL_DEPTH_COMPONENT16;
261 strb->Base._BaseFormat = GL_DEPTH_COMPONENT;
262 break;
263 case PIPE_FORMAT_Z32_UNORM:
264 strb->Base.InternalFormat = GL_DEPTH_COMPONENT32;
265 strb->Base._BaseFormat = GL_DEPTH_COMPONENT;
266 break;
267 case PIPE_FORMAT_S8Z24_UNORM:
268 case PIPE_FORMAT_Z24S8_UNORM:
269 case PIPE_FORMAT_X8Z24_UNORM:
270 case PIPE_FORMAT_Z24X8_UNORM:
271 strb->Base.InternalFormat = GL_DEPTH24_STENCIL8_EXT;
272 strb->Base._BaseFormat = GL_DEPTH_STENCIL_EXT;
273 break;
274 case PIPE_FORMAT_S8_UNORM:
275 strb->Base.InternalFormat = GL_STENCIL_INDEX8_EXT;
276 strb->Base._BaseFormat = GL_STENCIL_INDEX;
277 break;
278 case DEFAULT_ACCUM_PIPE_FORMAT: /*PIPE_FORMAT_R16G16B16A16_SNORM*/
279 strb->Base.InternalFormat = GL_RGBA16;
280 strb->Base._BaseFormat = GL_RGBA;
281 break;
282 default:
283 _mesa_problem(NULL,
284 "Unexpected format in st_new_renderbuffer_fb");
285 return NULL;
286 }
287
288 /* st-specific methods */
289 strb->Base.Delete = st_renderbuffer_delete;
290 strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
291 strb->Base.GetPointer = null_get_pointer;
292
293 /* surface is allocated in st_renderbuffer_alloc_storage() */
294 strb->surface = NULL;
295
296 return &strb->Base;
297 }
298
299
300
301
302 /**
303 * Called via ctx->Driver.BindFramebufferEXT().
304 */
305 static void
306 st_bind_framebuffer(GLcontext *ctx, GLenum target,
307 struct gl_framebuffer *fb, struct gl_framebuffer *fbread)
308 {
309
310 }
311
312 /**
313 * Called by ctx->Driver.FramebufferRenderbuffer
314 */
315 static void
316 st_framebuffer_renderbuffer(GLcontext *ctx,
317 struct gl_framebuffer *fb,
318 GLenum attachment,
319 struct gl_renderbuffer *rb)
320 {
321 /* XXX no need for derivation? */
322 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb);
323 }
324
325
326 /**
327 * Called by ctx->Driver.RenderTexture
328 */
329 static void
330 st_render_texture(GLcontext *ctx,
331 struct gl_framebuffer *fb,
332 struct gl_renderbuffer_attachment *att)
333 {
334 struct st_context *st = ctx->st;
335 struct st_renderbuffer *strb;
336 struct gl_renderbuffer *rb;
337 struct pipe_context *pipe = st->pipe;
338 struct pipe_screen *screen = pipe->screen;
339 struct pipe_texture *pt;
340
341 assert(!att->Renderbuffer);
342
343 /* create new renderbuffer which wraps the texture image */
344 rb = st_new_renderbuffer(ctx, 0);
345 if (!rb) {
346 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glFramebufferTexture()");
347 return;
348 }
349
350 _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
351 assert(rb->RefCount == 1);
352 rb->AllocStorage = NULL; /* should not get called */
353 strb = st_renderbuffer(rb);
354
355 /* get the texture for the texture object */
356 pt = st_get_texobj_texture(att->Texture);
357 assert(pt);
358 assert(pt->width[att->TextureLevel]);
359
360 rb->Width = pt->width[att->TextureLevel];
361 rb->Height = pt->height[att->TextureLevel];
362
363 /* the renderbuffer's surface is inside the texture */
364 strb->surface = screen->get_tex_surface(screen, pt,
365 att->CubeMapFace,
366 att->TextureLevel,
367 att->Zoffset,
368 PIPE_BUFFER_USAGE_CPU_READ |
369 PIPE_BUFFER_USAGE_CPU_WRITE |
370 PIPE_BUFFER_USAGE_GPU_READ |
371 PIPE_BUFFER_USAGE_GPU_WRITE);
372 assert(strb->surface);
373 assert(screen->is_format_supported(screen, strb->surface->format, PIPE_TEXTURE));
374 assert(screen->is_format_supported(screen, strb->surface->format, PIPE_SURFACE));
375
376 init_renderbuffer_bits(strb, pt->format);
377
378 /*
379 printf("RENDER TO TEXTURE obj=%p pt=%p surf=%p %d x %d\n",
380 att->Texture, pt, strb->surface, rb->Width, rb->Height);
381 */
382
383 /* Invalidate buffer state so that the pipe's framebuffer state
384 * gets updated.
385 * That's where the new renderbuffer (which we just created) gets
386 * passed to the pipe as a (color/depth) render target.
387 */
388 st_invalidate_state(ctx, _NEW_BUFFERS);
389 }
390
391
392 /**
393 * Called via ctx->Driver.FinishRenderTexture.
394 */
395 static void
396 st_finish_render_texture(GLcontext *ctx,
397 struct gl_renderbuffer_attachment *att)
398 {
399 struct pipe_screen *screen = ctx->st->pipe->screen;
400 struct st_renderbuffer *strb = st_renderbuffer(att->Renderbuffer);
401
402 assert(strb);
403
404 ctx->st->pipe->flush(ctx->st->pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
405
406 screen->tex_surface_release( screen, &strb->surface );
407
408 /*
409 printf("FINISH RENDER TO TEXTURE surf=%p\n", strb->surface);
410 */
411
412 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL);
413
414 /* restore previous framebuffer state */
415 st_invalidate_state(ctx, _NEW_BUFFERS);
416 }
417
418
419
420 void st_init_fbo_functions(struct dd_function_table *functions)
421 {
422 functions->NewFramebuffer = st_new_framebuffer;
423 functions->NewRenderbuffer = st_new_renderbuffer;
424 functions->BindFramebuffer = st_bind_framebuffer;
425 functions->FramebufferRenderbuffer = st_framebuffer_renderbuffer;
426 functions->RenderTexture = st_render_texture;
427 functions->FinishRenderTexture = st_finish_render_texture;
428 /* no longer needed by core Mesa, drivers handle resizes...
429 functions->ResizeBuffers = st_resize_buffers;
430 */
431 }