Merge branch 'gallium-msaa'
[mesa.git] / src / mesa / state_tracker / st_atom_framebuffer.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 * Authors:
30 * Keith Whitwell <keith@tungstengraphics.com>
31 * Brian Paul
32 */
33
34 #include "st_context.h"
35 #include "st_atom.h"
36 #include "st_cb_fbo.h"
37 #include "st_texture.h"
38 #include "pipe/p_context.h"
39 #include "cso_cache/cso_context.h"
40 #include "util/u_math.h"
41 #include "util/u_inlines.h"
42
43
44
45 /**
46 * When doing GL render to texture, we have to be sure that finalize_texture()
47 * didn't yank out the pipe_resource that we earlier created a surface for.
48 * Check for that here and create a new surface if needed.
49 */
50 static void
51 update_renderbuffer_surface(struct st_context *st,
52 struct st_renderbuffer *strb)
53 {
54 struct pipe_screen *screen = st->pipe->screen;
55 struct pipe_resource *resource = strb->rtt->pt;
56 int rtt_width = strb->Base.Width;
57 int rtt_height = strb->Base.Height;
58
59 if (!strb->surface ||
60 strb->surface->texture != resource ||
61 strb->surface->width != rtt_width ||
62 strb->surface->height != rtt_height) {
63 GLuint level;
64 /* find matching mipmap level size */
65 for (level = 0; level <= resource->last_level; level++) {
66 if (u_minify(resource->width0, level) == rtt_width &&
67 u_minify(resource->height0, level) == rtt_height) {
68
69 pipe_surface_reference(&strb->surface, NULL);
70
71 strb->surface = screen->get_tex_surface(screen,
72 resource,
73 strb->rtt_face,
74 level,
75 strb->rtt_slice,
76 PIPE_BIND_RENDER_TARGET);
77 #if 0
78 printf("-- alloc new surface %d x %d into tex %p\n",
79 strb->surface->width, strb->surface->height,
80 texture);
81 #endif
82 break;
83 }
84 }
85 }
86 }
87
88
89 /**
90 * Update framebuffer state (color, depth, stencil, etc. buffers)
91 */
92 static void
93 update_framebuffer_state( struct st_context *st )
94 {
95 struct pipe_framebuffer_state *framebuffer = &st->state.framebuffer;
96 struct gl_framebuffer *fb = st->ctx->DrawBuffer;
97 struct st_renderbuffer *strb;
98 GLuint i;
99
100 framebuffer->width = fb->Width;
101 framebuffer->height = fb->Height;
102
103 /*printf("------ fb size %d x %d\n", fb->Width, fb->Height);*/
104
105 /* Examine Mesa's ctx->DrawBuffer->_ColorDrawBuffers state
106 * to determine which surfaces to draw to
107 */
108 framebuffer->nr_cbufs = 0;
109 for (i = 0; i < fb->_NumColorDrawBuffers; i++) {
110 strb = st_renderbuffer(fb->_ColorDrawBuffers[i]);
111
112 if (strb) {
113 /*printf("--------- framebuffer surface rtt %p\n", strb->rtt);*/
114 if (strb->rtt) {
115 /* rendering to a GL texture, may have to update surface */
116 update_renderbuffer_surface(st, strb);
117 }
118
119 if (strb->surface) {
120 pipe_surface_reference(&framebuffer->cbufs[framebuffer->nr_cbufs],
121 strb->surface);
122 framebuffer->nr_cbufs++;
123 }
124 strb->defined = GL_TRUE; /* we'll be drawing something */
125 }
126 }
127 for (i = framebuffer->nr_cbufs; i < PIPE_MAX_COLOR_BUFS; i++) {
128 pipe_surface_reference(&framebuffer->cbufs[i], NULL);
129 }
130
131 /*
132 * Depth/Stencil renderbuffer/surface.
133 */
134 strb = st_renderbuffer(fb->Attachment[BUFFER_DEPTH].Renderbuffer);
135 if (strb) {
136 strb = st_renderbuffer(strb->Base.Wrapped);
137 if (strb->rtt) {
138 /* rendering to a GL texture, may have to update surface */
139 update_renderbuffer_surface(st, strb);
140 }
141 pipe_surface_reference(&framebuffer->zsbuf, strb->surface);
142 }
143 else {
144 strb = st_renderbuffer(fb->Attachment[BUFFER_STENCIL].Renderbuffer);
145 if (strb) {
146 strb = st_renderbuffer(strb->Base.Wrapped);
147 assert(strb->surface);
148 pipe_surface_reference(&framebuffer->zsbuf, strb->surface);
149 }
150 else
151 pipe_surface_reference(&framebuffer->zsbuf, NULL);
152 }
153
154 #ifdef DEBUG
155 /* Make sure the resource binding flags were set properly */
156 for (i = 0; i < framebuffer->nr_cbufs; i++) {
157 assert(framebuffer->cbufs[i]->texture->bind & PIPE_BIND_RENDER_TARGET);
158 }
159 if (framebuffer->zsbuf) {
160 assert(framebuffer->zsbuf->texture->bind & PIPE_BIND_DEPTH_STENCIL);
161 }
162 #endif
163
164 cso_set_framebuffer(st->cso_context, framebuffer);
165 }
166
167
168 const struct st_tracked_state st_update_framebuffer = {
169 "st_update_framebuffer", /* name */
170 { /* dirty */
171 _NEW_BUFFERS, /* mesa */
172 ST_NEW_FRAMEBUFFER, /* st */
173 },
174 update_framebuffer_state /* update */
175 };
176