1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
|
/*
* Mesa 3-D graphics library
* Version: 6.5
*
* Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* Functions for allocating/managing framebuffers and renderbuffers.
* Also, routines for reading/writing renderbuffer data as ubytes,
* ushorts, uints, etc.
*/
#include "glheader.h"
#include "imports.h"
#include "context.h"
#include "mtypes.h"
#include "fbobject.h"
#include "framebuffer.h"
#include "renderbuffer.h"
/**
* Compute/set the _DepthMax field for the given framebuffer.
* This value depends on the Z buffer resolution.
*/
static void
compute_depth_max(struct gl_framebuffer *fb)
{
if (fb->Visual.depthBits == 0) {
/* Special case. Even if we don't have a depth buffer we need
* good values for DepthMax for Z vertex transformation purposes
* and for per-fragment fog computation.
*/
fb->_DepthMax = (1 << 16) - 1;
}
else if (fb->Visual.depthBits < 32) {
fb->_DepthMax = (1 << fb->Visual.depthBits) - 1;
}
else {
/* Special case since shift values greater than or equal to the
* number of bits in the left hand expression's type are undefined.
*/
fb->_DepthMax = 0xffffffff;
}
fb->_DepthMaxF = (GLfloat) fb->_DepthMax;
fb->_MRD = 1.0; /* Minimum resolvable depth value, for polygon offset */
}
/**
* Create and initialize a gl_framebuffer object.
* This is intended for creating _window_system_ framebuffers, not generic
* framebuffer objects ala GL_EXT_framebuffer_object.
*
* \sa _mesa_new_framebuffer
*/
struct gl_framebuffer *
_mesa_create_framebuffer(const GLvisual *visual)
{
struct gl_framebuffer *fb = CALLOC_STRUCT(gl_framebuffer);
assert(visual);
if (fb) {
_mesa_initialize_framebuffer(fb, visual);
}
return fb;
}
/**
* Allocate a new gl_framebuffer object.
* This is the default function for ctx->Driver.NewFramebuffer().
* This is for allocating user-created framebuffers, not window-system
* framebuffers!
* \sa _mesa_create_framebuffer
*/
struct gl_framebuffer *
_mesa_new_framebuffer(GLcontext *ctx, GLuint name)
{
struct gl_framebuffer *fb;
assert(name != 0);
fb = CALLOC_STRUCT(gl_framebuffer);
if (fb) {
fb->Name = name;
fb->RefCount = 1;
fb->ColorDrawBuffer[0] = GL_COLOR_ATTACHMENT0_EXT;
fb->_ColorDrawBufferMask[0] = BUFFER_BIT_COLOR0;
fb->ColorReadBuffer = GL_COLOR_ATTACHMENT0_EXT;
fb->_ColorReadBufferMask = BUFFER_BIT_COLOR0;
fb->Delete = _mesa_destroy_framebuffer;
}
return fb;
}
/**
* Initialize a gl_framebuffer object. Typically used to initialize
* window system-created framebuffers, not user-created framebuffers.
* \sa _mesa_create_framebuffer
*/
void
_mesa_initialize_framebuffer(struct gl_framebuffer *fb, const GLvisual *visual)
{
assert(fb);
assert(visual);
_mesa_bzero(fb, sizeof(struct gl_framebuffer));
/* save the visual */
fb->Visual = *visual;
/* Init glRead/DrawBuffer state */
if (visual->doubleBufferMode) {
fb->ColorDrawBuffer[0] = GL_BACK;
fb->_ColorDrawBufferMask[0] = BUFFER_BIT_BACK_LEFT;
fb->ColorReadBuffer = GL_BACK;
fb->_ColorReadBufferMask = BUFFER_BIT_BACK_LEFT;
}
else {
fb->ColorDrawBuffer[0] = GL_FRONT;
fb->_ColorDrawBufferMask[0] = BUFFER_BIT_FRONT_LEFT;
fb->ColorReadBuffer = GL_FRONT;
fb->_ColorReadBufferMask = BUFFER_BIT_FRONT_LEFT;
}
fb->Delete = _mesa_destroy_framebuffer;
compute_depth_max(fb);
}
/**
* Deallocate buffer and everything attached to it.
* Typically called via the gl_framebuffer->Delete() method.
*/
void
_mesa_destroy_framebuffer(struct gl_framebuffer *fb)
{
if (fb) {
_mesa_free_framebuffer_data(fb);
FREE(fb);
}
}
/**
* Free all the data hanging off the given gl_framebuffer, but don't free
* the gl_framebuffer object itself.
*/
void
_mesa_free_framebuffer_data(struct gl_framebuffer *fb)
{
GLuint i;
assert(fb);
for (i = 0; i < BUFFER_COUNT; i++) {
struct gl_renderbuffer_attachment *att = &fb->Attachment[i];
if (att->Type == GL_RENDERBUFFER_EXT && att->Renderbuffer) {
struct gl_renderbuffer *rb = att->Renderbuffer;
rb->RefCount--;
if (rb->RefCount == 0) {
rb->Delete(rb);
}
}
att->Type = GL_NONE;
att->Renderbuffer = NULL;
}
}
/**
* Resize the given framebuffer's renderbuffers to the new width and height.
* This should only be used for window-system framebuffers, not
* user-created renderbuffers (i.e. made with GL_EXT_framebuffer_object).
* This will typically be called via ctx->Driver.ResizeBuffers()
*/
void
_mesa_resize_framebuffer(GLcontext *ctx, struct gl_framebuffer *fb,
GLuint width, GLuint height)
{
GLuint i;
/* For window system framebuffers, Name is zero */
assert(fb->Name == 0);
for (i = 0; i < BUFFER_COUNT; i++) {
struct gl_renderbuffer_attachment *att = &fb->Attachment[i];
if (att->Type == GL_RENDERBUFFER_EXT && att->Renderbuffer) {
struct gl_renderbuffer *rb = att->Renderbuffer;
/* only resize if size is changing */
if (rb->Width != width || rb->Height != height) {
if (rb->AllocStorage(ctx, rb, rb->InternalFormat, width, height)) {
rb->Width = width;
rb->Height = height;
}
else {
_mesa_error(ctx, GL_OUT_OF_MEMORY, "Resizing framebuffer");
}
}
}
}
fb->Width = width;
fb->Height = height;
}
/**
* Examine all the framebuffer's renderbuffers to update the Width/Height
* fields of the framebuffer. If we have renderbuffers with different
* sizes, set the framebuffer's width and height to zero.
* Note: this is only intended for user-created framebuffers, not
* window-system framebuffes.
*/
static void
update_framebuffer_size(struct gl_framebuffer *fb)
{
GLboolean haveSize = GL_FALSE;
GLuint i;
/* user-created framebuffers only */
assert(fb->Name);
for (i = 0; i < BUFFER_COUNT; i++) {
struct gl_renderbuffer_attachment *att = &fb->Attachment[i];
const struct gl_renderbuffer *rb = att->Renderbuffer;
if (rb) {
if (haveSize) {
if (rb->Width != fb->Width && rb->Height != fb->Height) {
/* size mismatch! */
fb->Width = 0;
fb->Height = 0;
return;
}
}
else {
fb->Width = rb->Width;
fb->Height = rb->Height;
haveSize = GL_TRUE;
}
}
}
}
/**
* Update the context's current drawing buffer's Xmin, Xmax, Ymin, Ymax fields.
* These values are computed from the buffer's width and height and
* the scissor box, if it's enabled.
* \param ctx the GL context.
*/
void
_mesa_update_draw_buffer_bounds(GLcontext *ctx)
{
struct gl_framebuffer *buffer = ctx->DrawBuffer;
if (buffer->Name) {
/* user-created framebuffer size depends on the renderbuffers */
update_framebuffer_size(buffer);
}
buffer->_Xmin = 0;
buffer->_Ymin = 0;
buffer->_Xmax = buffer->Width;
buffer->_Ymax = buffer->Height;
if (ctx->Scissor.Enabled) {
if (ctx->Scissor.X > buffer->_Xmin) {
buffer->_Xmin = ctx->Scissor.X;
}
if (ctx->Scissor.Y > buffer->_Ymin) {
buffer->_Ymin = ctx->Scissor.Y;
}
if (ctx->Scissor.X + ctx->Scissor.Width < buffer->_Xmax) {
buffer->_Xmax = ctx->Scissor.X + ctx->Scissor.Width;
}
if (ctx->Scissor.Y + ctx->Scissor.Height < buffer->_Ymax) {
buffer->_Ymax = ctx->Scissor.Y + ctx->Scissor.Height;
}
/* finally, check for empty region */
if (buffer->_Xmin > buffer->_Xmax) {
buffer->_Xmin = buffer->_Xmax;
}
if (buffer->_Ymin > buffer->_Ymax) {
buffer->_Ymin = buffer->_Ymax;
}
}
ASSERT(buffer->_Xmin <= buffer->_Xmax);
ASSERT(buffer->_Ymin <= buffer->_Ymax);
}
/**
* The glGet queries of the framebuffer red/green/blue size, stencil size,
* etc. are satisfied by the fields of ctx->DrawBuffer->Visual. These can
* change depending on the renderbuffer bindings. This function updates
* the given framebuffer's Visual from the current renderbuffer bindings.
* This is only intended for user-created framebuffers.
*/
void
_mesa_update_framebuffer_visual(struct gl_framebuffer *fb)
{
assert(fb->Name != 0);
_mesa_bzero(&fb->Visual, sizeof(fb->Visual));
fb->Visual.rgbMode = GL_TRUE;
if (fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer) {
fb->Visual.redBits
= fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer->ComponentSizes[0];
fb->Visual.greenBits
= fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer->ComponentSizes[1];
fb->Visual.blueBits
= fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer->ComponentSizes[2];
fb->Visual.alphaBits
= fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer->ComponentSizes[3];
fb->Visual.rgbBits
= fb->Visual.redBits + fb->Visual.greenBits + fb->Visual.blueBits;
fb->Visual.floatMode = GL_FALSE;
}
if (fb->Attachment[BUFFER_DEPTH].Renderbuffer) {
fb->Visual.haveDepthBuffer = GL_TRUE;
fb->Visual.depthBits
= fb->Attachment[BUFFER_DEPTH].Renderbuffer->ComponentSizes[0];
}
if (fb->Attachment[BUFFER_STENCIL].Renderbuffer) {
fb->Visual.haveStencilBuffer = GL_TRUE;
fb->Visual.stencilBits
= fb->Attachment[BUFFER_STENCIL].Renderbuffer->ComponentSizes[0];
}
compute_depth_max(fb);
}
/**
* Given a framebuffer and a buffer bit (like BUFFER_BIT_FRONT_LEFT), return
* the corresponding renderbuffer.
*/
static struct gl_renderbuffer *
get_renderbuffer(struct gl_framebuffer *fb, GLbitfield bufferBit)
{
GLuint index;
for (index = 0; index < BUFFER_COUNT; index++) {
if ((1 << index) == bufferBit) {
return fb->Attachment[index].Renderbuffer;
}
}
_mesa_problem(NULL, "Bad bufferBit in get_renderbuffer");
return NULL;
}
/**
* Update state related to the current draw/read framebuffers.
* Specifically, update these framebuffer fields:
* _ColorDrawBuffers
* _NumColorDrawBuffers
* _ColorReadBuffer
* If the current framebuffer is user-created, make sure it's complete.
* The following functions can effect this state: glReadBuffer,
* glDrawBuffer, glDrawBuffersARB, glFramebufferRenderbufferEXT.
*/
void
_mesa_update_framebuffer(GLcontext *ctx)
{
struct gl_framebuffer *fb = ctx->DrawBuffer;
GLuint output;
/* Completeness only matters for user-created framebuffers */
if (fb->Name != 0)
_mesa_test_framebuffer_completeness(ctx, fb);
/*
* Update the list of drawing renderbuffer pointers.
* Later, when we're rendering we'll loop from 0 to _NumColorDrawBuffers
* writing colors. We need the inner loop here because
* glDrawBuffer(GL_FRONT_AND_BACK) can specify writing to two or four
* color buffers (for example).
*/
for (output = 0; output < ctx->Const.MaxDrawBuffers; output++) {
GLbitfield bufferMask = fb->_ColorDrawBufferMask[output];
GLuint count = 0;
GLuint bufferBit;
/* for each bit that's set in the bufferMask... */
for (bufferBit = 1; bufferMask; bufferBit <<= 1) {
if (bufferBit & bufferMask) {
struct gl_renderbuffer *rb = get_renderbuffer(fb, bufferBit);
if (rb) {
fb->_ColorDrawBuffers[output][count] = rb;
count++;
}
else {
_mesa_warning(ctx, "DrawBuffer names a missing buffer!");
}
bufferMask &= ~bufferBit;
}
}
fb->_NumColorDrawBuffers[output] = count;
}
/*
* Update the read renderbuffer pointer.
* Unlike the DrawBuffer, we can only read from one (or zero) color buffers.
*/
if (fb->_ColorReadBufferMask == 0x0)
fb->_ColorReadBuffer = NULL; /* legal! */
else
fb->_ColorReadBuffer = get_renderbuffer(fb, fb->_ColorReadBufferMask);
compute_depth_max(fb);
}
|