gallium: free bitmap fragment shaders, misc clean-up
[mesa.git] / src / mesa / state_tracker / st_framebuffer.c
1 /**************************************************************************
2 *
3 * Copyright 2003 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 #include "main/imports.h"
30 #include "main/buffers.h"
31 #include "main/context.h"
32 #include "main/framebuffer.h"
33 #include "main/matrix.h"
34 #include "main/renderbuffer.h"
35 #include "st_public.h"
36 #include "st_context.h"
37 #include "st_cb_fbo.h"
38 #include "pipe/p_defines.h"
39 #include "pipe/p_context.h"
40
41
42 struct st_framebuffer *
43 st_create_framebuffer( const __GLcontextModes *visual,
44 enum pipe_format colorFormat,
45 enum pipe_format depthFormat,
46 enum pipe_format stencilFormat,
47 uint width, uint height,
48 void *private)
49 {
50 struct st_framebuffer *stfb = CALLOC_STRUCT(st_framebuffer);
51 if (stfb) {
52 _mesa_initialize_framebuffer(&stfb->Base, visual);
53
54 {
55 /* fake frontbuffer */
56 /* XXX allocation should only happen in the unusual case
57 it's actually needed */
58 struct gl_renderbuffer *rb
59 = st_new_renderbuffer_fb(colorFormat);
60 _mesa_add_renderbuffer(&stfb->Base, BUFFER_FRONT_LEFT, rb);
61 }
62
63 if (visual->doubleBufferMode) {
64 struct gl_renderbuffer *rb
65 = st_new_renderbuffer_fb(colorFormat);
66 _mesa_add_renderbuffer(&stfb->Base, BUFFER_BACK_LEFT, rb);
67 }
68
69 if (visual->depthBits == 24 && visual->stencilBits == 8) {
70 /* combined depth/stencil buffer */
71 struct gl_renderbuffer *depthStencilRb
72 = st_new_renderbuffer_fb(depthFormat);
73 /* note: bind RB to two attachment points */
74 _mesa_add_renderbuffer(&stfb->Base, BUFFER_DEPTH, depthStencilRb);
75 _mesa_add_renderbuffer(&stfb->Base, BUFFER_STENCIL, depthStencilRb);
76 }
77 else {
78 /* separate depth and/or stencil */
79
80 if (visual->depthBits == 32) {
81 /* 32-bit depth buffer */
82 struct gl_renderbuffer *depthRb
83 = st_new_renderbuffer_fb(depthFormat);
84 _mesa_add_renderbuffer(&stfb->Base, BUFFER_DEPTH, depthRb);
85 }
86 else if (visual->depthBits == 24) {
87 /* 24-bit depth buffer, ignore stencil bits */
88 struct gl_renderbuffer *depthRb
89 = st_new_renderbuffer_fb(depthFormat);
90 _mesa_add_renderbuffer(&stfb->Base, BUFFER_DEPTH, depthRb);
91 }
92 else if (visual->depthBits > 0) {
93 /* 16-bit depth buffer */
94 struct gl_renderbuffer *depthRb
95 = st_new_renderbuffer_fb(depthFormat);
96 _mesa_add_renderbuffer(&stfb->Base, BUFFER_DEPTH, depthRb);
97 }
98
99 if (visual->stencilBits > 0) {
100 /* 8-bit stencil */
101 struct gl_renderbuffer *stencilRb
102 = st_new_renderbuffer_fb(stencilFormat);
103 _mesa_add_renderbuffer(&stfb->Base, BUFFER_STENCIL, stencilRb);
104 }
105 }
106
107 if (visual->accumRedBits > 0) {
108 /* 16-bit/channel accum */
109 struct gl_renderbuffer *accumRb
110 = st_new_renderbuffer_fb(PIPE_FORMAT_R16G16B16A16_SNORM);
111 _mesa_add_renderbuffer(&stfb->Base, BUFFER_ACCUM, accumRb);
112 }
113
114 stfb->Base.Initialized = GL_TRUE;
115 stfb->InitWidth = width;
116 stfb->InitHeight = height;
117 stfb->Private = private;
118 }
119 return stfb;
120 }
121
122
123 void st_resize_framebuffer( struct st_framebuffer *stfb,
124 uint width, uint height )
125 {
126 if (stfb->Base.Width != width || stfb->Base.Height != height) {
127 GET_CURRENT_CONTEXT(ctx);
128 if (ctx) {
129 if (stfb->InitWidth == 0 && stfb->InitHeight == 0) {
130 /* didn't have a valid size until now */
131 stfb->InitWidth = width;
132 stfb->InitHeight = height;
133 if (ctx->Viewport.Width <= 1) {
134 /* set context's initial viewport/scissor size */
135 _mesa_set_viewport(ctx, 0, 0, width, height);
136 _mesa_set_scissor(ctx, 0, 0, width, height);
137 }
138 }
139
140 _mesa_resize_framebuffer(ctx, &stfb->Base, width, height);
141
142 assert(stfb->Base.Width == width);
143 assert(stfb->Base.Height == height);
144 }
145 }
146 }
147
148
149 void st_unreference_framebuffer( struct st_framebuffer **stfb )
150 {
151 _mesa_unreference_framebuffer((struct gl_framebuffer **) stfb);
152 }
153
154
155
156 /**
157 * Return the pipe_surface for the given renderbuffer.
158 */
159 struct pipe_surface *
160 st_get_framebuffer_surface(struct st_framebuffer *stfb, uint surfIndex)
161 {
162 struct st_renderbuffer *strb;
163
164 assert(surfIndex <= ST_SURFACE_DEPTH);
165
166 /* sanity checks, ST tokens should match Mesa tokens */
167 assert(ST_SURFACE_FRONT_LEFT == BUFFER_FRONT_LEFT);
168 assert(ST_SURFACE_BACK_RIGHT == BUFFER_BACK_RIGHT);
169
170 strb = st_renderbuffer(stfb->Base.Attachment[surfIndex].Renderbuffer);
171 if (strb)
172 return strb->surface;
173 return NULL;
174 }
175
176
177 /**
178 * This function is to be called prior to SwapBuffers on the given
179 * framebuffer. It checks if the current context is bound to the framebuffer
180 * and flushes rendering if needed.
181 */
182 void
183 st_notify_swapbuffers(struct st_framebuffer *stfb)
184 {
185 GET_CURRENT_CONTEXT(ctx);
186
187 if (ctx && ctx->DrawBuffer == &stfb->Base) {
188 st_flush( ctx->st,
189 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_SWAPBUFFERS);
190 }
191 }
192
193
194 /**
195 * Quick hack - allows the winsys to inform the driver that surface
196 * states are now undefined after a glXSwapBuffers or similar.
197 */
198 void
199 st_notify_swapbuffers_complete(struct st_framebuffer *stfb)
200 {
201 GET_CURRENT_CONTEXT(ctx);
202
203 if (ctx && ctx->DrawBuffer == &stfb->Base) {
204 struct st_renderbuffer *strb;
205 int i;
206
207 for (i = 0; i < BUFFER_COUNT; i++) {
208 if (stfb->Base.Attachment[i].Renderbuffer) {
209 strb = st_renderbuffer(stfb->Base.Attachment[i].Renderbuffer);
210 strb->surface->status = PIPE_SURFACE_STATUS_UNDEFINED;
211 }
212 }
213 }
214 }
215
216
217 void *st_framebuffer_private( struct st_framebuffer *stfb )
218 {
219 return stfb->Private;
220 }
221