mesa: Add "shader/" path to #include statements in shader parser/lexer sources
[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 #include "util/u_rect.h"
43 #include "util/u_math.h"
44
45
46
47 /**
48 * When doing GL render to texture, we have to be sure that finalize_texture()
49 * didn't yank out the pipe_texture that we earlier created a surface for.
50 * Check for that here and create a new surface if needed.
51 */
52 static void
53 update_renderbuffer_surface(struct st_context *st,
54 struct st_renderbuffer *strb)
55 {
56 struct pipe_screen *screen = st->pipe->screen;
57 struct pipe_texture *texture = strb->rtt->pt;
58 int rtt_width = strb->Base.Width;
59 int rtt_height = strb->Base.Height;
60
61 if (!strb->surface ||
62 strb->surface->texture != texture ||
63 strb->surface->width != rtt_width ||
64 strb->surface->height != rtt_height) {
65 GLuint level;
66 /* find matching mipmap level size */
67 for (level = 0; level <= texture->last_level; level++) {
68 if (u_minify(texture->width0, level) == rtt_width &&
69 u_minify(texture->height0, level) == rtt_height) {
70
71 pipe_surface_reference(&strb->surface, NULL);
72
73 strb->surface = screen->get_tex_surface(screen,
74 texture,
75 strb->rtt_face,
76 level,
77 strb->rtt_slice,
78 PIPE_BUFFER_USAGE_GPU_READ |
79 PIPE_BUFFER_USAGE_GPU_WRITE);
80 #if 0
81 printf("-- alloc new surface %d x %d into tex %p\n",
82 strb->surface->width, strb->surface->height,
83 texture);
84 #endif
85 break;
86 }
87 }
88 }
89 }
90
91
92 /**
93 * Update framebuffer state (color, depth, stencil, etc. buffers)
94 */
95 static void
96 update_framebuffer_state( struct st_context *st )
97 {
98 struct pipe_framebuffer_state *framebuffer = &st->state.framebuffer;
99 struct gl_framebuffer *fb = st->ctx->DrawBuffer;
100 struct st_renderbuffer *strb;
101 GLuint i;
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 if (strb) {
116 /*printf("--------- framebuffer surface rtt %p\n", strb->rtt);*/
117 if (strb->rtt) {
118 /* rendering to a GL texture, may have to update surface */
119 update_renderbuffer_surface(st, strb);
120 }
121
122 if (strb->surface) {
123 pipe_surface_reference(&framebuffer->cbufs[framebuffer->nr_cbufs],
124 strb->surface);
125 framebuffer->nr_cbufs++;
126 }
127 strb->defined = GL_TRUE; /* we'll be drawing something */
128 }
129 }
130 for (i = framebuffer->nr_cbufs; i < PIPE_MAX_COLOR_BUFS; i++) {
131 pipe_surface_reference(&framebuffer->cbufs[i], NULL);
132 }
133
134 /*
135 * Depth/Stencil renderbuffer/surface.
136 */
137 strb = st_renderbuffer(fb->Attachment[BUFFER_DEPTH].Renderbuffer);
138 if (strb) {
139 strb = st_renderbuffer(strb->Base.Wrapped);
140 if (strb->rtt) {
141 /* rendering to a GL texture, may have to update surface */
142 update_renderbuffer_surface(st, strb);
143 }
144 pipe_surface_reference(&framebuffer->zsbuf, strb->surface);
145 }
146 else {
147 strb = st_renderbuffer(fb->Attachment[BUFFER_STENCIL].Renderbuffer);
148 if (strb) {
149 strb = st_renderbuffer(strb->Base.Wrapped);
150 assert(strb->surface);
151 pipe_surface_reference(&framebuffer->zsbuf, strb->surface);
152 }
153 else
154 pipe_surface_reference(&framebuffer->zsbuf, NULL);
155 }
156
157 cso_set_framebuffer(st->cso_context, framebuffer);
158
159 if (fb->_ColorDrawBufferIndexes[0] == BUFFER_FRONT_LEFT) {
160 if (st->frontbuffer_status == FRONT_STATUS_COPY_OF_BACK) {
161 /* copy back color buffer to front color buffer */
162 struct st_framebuffer *stfb = (struct st_framebuffer *) fb;
163 struct pipe_surface *surf_front, *surf_back;
164 (void) st_get_framebuffer_surface(stfb, ST_SURFACE_FRONT_LEFT, &surf_front);
165 (void) st_get_framebuffer_surface(stfb, ST_SURFACE_BACK_LEFT, &surf_back);
166
167 if (st->pipe->surface_copy) {
168 st->pipe->surface_copy(st->pipe,
169 surf_front, 0, 0, /* dest */
170 surf_back, 0, 0, /* src */
171 fb->Width, fb->Height);
172 } else {
173 util_surface_copy(st->pipe, FALSE,
174 surf_front, 0, 0,
175 surf_back, 0, 0,
176 fb->Width, fb->Height);
177 }
178 }
179 /* we're assuming we'll really draw to the front buffer */
180 st->frontbuffer_status = FRONT_STATUS_DIRTY;
181 }
182 }
183
184
185 const struct st_tracked_state st_update_framebuffer = {
186 "st_update_framebuffer", /* name */
187 { /* dirty */
188 _NEW_BUFFERS, /* mesa */
189 ST_NEW_FRAMEBUFFER, /* st */
190 },
191 update_framebuffer_state /* update */
192 };
193