st/mesa: add support for layered framebuffers and consolidate code
[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_bitmap.h"
37 #include "st_cb_fbo.h"
38 #include "st_texture.h"
39 #include "pipe/p_context.h"
40 #include "cso_cache/cso_context.h"
41 #include "util/u_math.h"
42 #include "util/u_inlines.h"
43 #include "util/u_format.h"
44
45
46 /**
47 * Update framebuffer state (color, depth, stencil, etc. buffers)
48 */
49 static void
50 update_framebuffer_state( struct st_context *st )
51 {
52 struct pipe_framebuffer_state *framebuffer = &st->state.framebuffer;
53 struct gl_framebuffer *fb = st->ctx->DrawBuffer;
54 struct st_renderbuffer *strb;
55 GLuint i;
56
57 st_flush_bitmap_cache(st);
58
59 st->state.fb_orientation = st_fb_orientation(fb);
60 framebuffer->width = fb->Width;
61 framebuffer->height = fb->Height;
62
63 /*printf("------ fb size %d x %d\n", fb->Width, fb->Height);*/
64
65 /* Examine Mesa's ctx->DrawBuffer->_ColorDrawBuffers state
66 * to determine which surfaces to draw to
67 */
68 framebuffer->nr_cbufs = 0;
69 for (i = 0; i < fb->_NumColorDrawBuffers; i++) {
70 strb = st_renderbuffer(fb->_ColorDrawBuffers[i]);
71
72 if (strb) {
73 /*printf("--------- framebuffer surface rtt %p\n", strb->rtt);*/
74 if (strb->is_rtt ||
75 (strb->texture && util_format_is_srgb(strb->texture->format))) {
76 /* rendering to a GL texture, may have to update surface */
77 st_update_renderbuffer_surface(st, strb);
78 }
79
80 if (strb->surface) {
81 pipe_surface_reference(&framebuffer->cbufs[framebuffer->nr_cbufs],
82 strb->surface);
83 framebuffer->nr_cbufs++;
84 }
85 strb->defined = GL_TRUE; /* we'll be drawing something */
86 }
87 }
88 for (i = framebuffer->nr_cbufs; i < PIPE_MAX_COLOR_BUFS; i++) {
89 pipe_surface_reference(&framebuffer->cbufs[i], NULL);
90 }
91
92 /*
93 * Depth/Stencil renderbuffer/surface.
94 */
95 strb = st_renderbuffer(fb->Attachment[BUFFER_DEPTH].Renderbuffer);
96 if (strb) {
97 if (strb->is_rtt) {
98 /* rendering to a GL texture, may have to update surface */
99 st_update_renderbuffer_surface(st, strb);
100 }
101 pipe_surface_reference(&framebuffer->zsbuf, strb->surface);
102 }
103 else {
104 strb = st_renderbuffer(fb->Attachment[BUFFER_STENCIL].Renderbuffer);
105 if (strb) {
106 assert(strb->surface);
107 pipe_surface_reference(&framebuffer->zsbuf, strb->surface);
108 }
109 else
110 pipe_surface_reference(&framebuffer->zsbuf, NULL);
111 }
112
113 #ifdef DEBUG
114 /* Make sure the resource binding flags were set properly */
115 for (i = 0; i < framebuffer->nr_cbufs; i++) {
116 assert(framebuffer->cbufs[i]->texture->bind & PIPE_BIND_RENDER_TARGET);
117 }
118 if (framebuffer->zsbuf) {
119 assert(framebuffer->zsbuf->texture->bind & PIPE_BIND_DEPTH_STENCIL);
120 }
121 #endif
122
123 cso_set_framebuffer(st->cso_context, framebuffer);
124 }
125
126
127 const struct st_tracked_state st_update_framebuffer = {
128 "st_update_framebuffer", /* name */
129 { /* dirty */
130 _NEW_BUFFERS, /* mesa */
131 ST_NEW_FRAMEBUFFER, /* st */
132 },
133 update_framebuffer_state /* update */
134 };
135