Merge commit 'origin/gallium-master-merge'
[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_public.h"
38 #include "st_texture.h"
39 #include "pipe/p_context.h"
40 #include "pipe/p_inlines.h"
41 #include "cso_cache/cso_context.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_texture 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_texture *texture = 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 != texture ||
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 <= texture->last_level; level++) {
66 if (texture->width[level] == rtt_width &&
67 texture->height[level] == rtt_height) {
68
69 pipe_surface_reference(&strb->surface, NULL);
70
71 strb->surface = screen->get_tex_surface(screen,
72 texture,
73 strb->rtt_face,
74 level,
75 strb->rtt_slice,
76 PIPE_BUFFER_USAGE_GPU_READ |
77 PIPE_BUFFER_USAGE_GPU_WRITE);
78 #if 0
79 printf("-- alloc new surface %d x %d into tex %p\n",
80 strb->surface->width, strb->surface->height,
81 texture);
82 #endif
83 break;
84 }
85 }
86 }
87 }
88
89
90 /**
91 * Update framebuffer state (color, depth, stencil, etc. buffers)
92 */
93 static void
94 update_framebuffer_state( struct st_context *st )
95 {
96 struct pipe_framebuffer_state *framebuffer = &st->state.framebuffer;
97 struct gl_framebuffer *fb = st->ctx->DrawBuffer;
98 struct st_renderbuffer *strb;
99 GLuint i;
100
101 memset(framebuffer, 0, sizeof(*framebuffer));
102
103 framebuffer->width = fb->Width;
104 framebuffer->height = fb->Height;
105
106 /*printf("------ fb size %d x %d\n", fb->Width, fb->Height);*/
107
108 /* Examine Mesa's ctx->DrawBuffer->_ColorDrawBuffers state
109 * to determine which surfaces to draw to
110 */
111 framebuffer->nr_cbufs = 0;
112 for (i = 0; i < fb->_NumColorDrawBuffers; i++) {
113 strb = st_renderbuffer(fb->_ColorDrawBuffers[i]);
114
115 /*printf("--------- framebuffer surface rtt %p\n", strb->rtt);*/
116 if (strb->rtt) {
117 /* rendering to a GL texture, may have to update surface */
118 update_renderbuffer_surface(st, strb);
119 }
120
121 if (strb->surface) {
122 framebuffer->cbufs[framebuffer->nr_cbufs] = strb->surface;
123 framebuffer->nr_cbufs++;
124 }
125 }
126
127 strb = st_renderbuffer(fb->Attachment[BUFFER_DEPTH].Renderbuffer);
128 if (strb) {
129 strb = st_renderbuffer(strb->Base.Wrapped);
130 if (strb->rtt) {
131 /* rendering to a GL texture, may have to update surface */
132 update_renderbuffer_surface(st, strb);
133 }
134
135 framebuffer->zsbuf = strb->surface;
136 }
137 else {
138 strb = st_renderbuffer(fb->Attachment[BUFFER_STENCIL].Renderbuffer);
139 if (strb) {
140 strb = st_renderbuffer(strb->Base.Wrapped);
141 assert(strb->surface);
142 framebuffer->zsbuf = strb->surface;
143 }
144 }
145
146 cso_set_framebuffer(st->cso_context, framebuffer);
147
148 if (fb->_ColorDrawBufferIndexes[0] == BUFFER_FRONT_LEFT) {
149 if (st->frontbuffer_status == FRONT_STATUS_COPY_OF_BACK) {
150 /* copy back color buffer to front color buffer */
151 struct st_framebuffer *stfb = (struct st_framebuffer *) fb;
152 struct pipe_surface *surf_front, *surf_back;
153 (void) st_get_framebuffer_surface(stfb, ST_SURFACE_FRONT_LEFT, &surf_front);
154 (void) st_get_framebuffer_surface(stfb, ST_SURFACE_BACK_LEFT, &surf_back);
155
156 st->pipe->surface_copy(st->pipe,
157 FALSE,
158 surf_front, 0, 0, /* dest */
159 surf_back, 0, 0, /* src */
160 fb->Width, fb->Height);
161 }
162 /* we're assuming we'll really draw to the front buffer */
163 st->frontbuffer_status = FRONT_STATUS_DIRTY;
164 }
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