1fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian/**************************************************************************
2fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian *
3fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian * All Rights Reserved.
5fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian *
6fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian * Permission is hereby granted, free of charge, to any person obtaining a
7fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian * copy of this software and associated documentation files (the
8fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian * "Software"), to deal in the Software without restriction, including
9fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian * without limitation the rights to use, copy, modify, merge, publish,
10fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian * distribute, sub license, and/or sell copies of the Software, and to
11fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian * permit persons to whom the Software is furnished to do so, subject to
12fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian * the following conditions:
13fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian *
14fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian * The above copyright notice and this permission notice (including the
15fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian * next paragraph) shall be included in all copies or substantial portions
16fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian * of the Software.
17fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian *
18fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian *
26fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian **************************************************************************/
27fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian
28fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian /*
29fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian  * Authors:
30fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian  *   Keith Whitwell <keith@tungstengraphics.com>
31fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian  *   Brian Paul
32fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian  */
33fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian
34fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian#include "st_context.h"
35fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian#include "st_atom.h"
36162b3ad94d52b28d83462202952987012387e12fMarek Olšák#include "st_cb_bitmap.h"
37f5713c7d2e7ba8e1170fd9b1dd95379662ab6117Brian#include "st_cb_fbo.h"
381a82d9648b3db780e58e4966924157542d148c58Brian Paul#include "st_texture.h"
39fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian#include "pipe/p_context.h"
407d95efde0a0e13e13c59444703bc47eb13926385Brian#include "cso_cache/cso_context.h"
41683e35f726a182ed9fc6b6d5cb07146eebe14deaKeith Whitwell#include "util/u_math.h"
4228486880ca3ec39419ccee0cb1a3bedc9ef7117cJosé Fonseca#include "util/u_inlines.h"
4321b0996dfcbe62c55dd31cc316cd47f2b8531d2eDave Airlie#include "util/u_format.h"
44fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian
451a82d9648b3db780e58e4966924157542d148c58Brian Paul
461a82d9648b3db780e58e4966924157542d148c58Brian Paul/**
471a82d9648b3db780e58e4966924157542d148c58Brian Paul * When doing GL render to texture, we have to be sure that finalize_texture()
48287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell * didn't yank out the pipe_resource that we earlier created a surface for.
491a82d9648b3db780e58e4966924157542d148c58Brian Paul * Check for that here and create a new surface if needed.
501a82d9648b3db780e58e4966924157542d148c58Brian Paul */
511a82d9648b3db780e58e4966924157542d148c58Brian Paulstatic void
521a82d9648b3db780e58e4966924157542d148c58Brian Paulupdate_renderbuffer_surface(struct st_context *st,
531a82d9648b3db780e58e4966924157542d148c58Brian Paul                            struct st_renderbuffer *strb)
541a82d9648b3db780e58e4966924157542d148c58Brian Paul{
554c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger   struct pipe_context *pipe = st->pipe;
5651e41a0d894109249448ecc6a6bfc09e095acadaChristoph Bumiller   struct pipe_resource *resource = strb->rtt ? strb->rtt->pt : strb->texture;
571a82d9648b3db780e58e4966924157542d148c58Brian Paul   int rtt_width = strb->Base.Width;
581a82d9648b3db780e58e4966924157542d148c58Brian Paul   int rtt_height = strb->Base.Height;
5921b0996dfcbe62c55dd31cc316cd47f2b8531d2eDave Airlie   enum pipe_format format = st->ctx->Color.sRGBEnabled ? resource->format : util_format_linear(resource->format);
601a82d9648b3db780e58e4966924157542d148c58Brian Paul
611a82d9648b3db780e58e4966924157542d148c58Brian Paul   if (!strb->surface ||
6221b0996dfcbe62c55dd31cc316cd47f2b8531d2eDave Airlie       strb->surface->format != format ||
63287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell       strb->surface->texture != resource ||
641a82d9648b3db780e58e4966924157542d148c58Brian Paul       strb->surface->width != rtt_width ||
651a82d9648b3db780e58e4966924157542d148c58Brian Paul       strb->surface->height != rtt_height) {
666c534b830c6f5427c391c5225c34561141c201baMichal Krol      GLuint level;
671a82d9648b3db780e58e4966924157542d148c58Brian Paul      /* find matching mipmap level size */
68287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell      for (level = 0; level <= resource->last_level; level++) {
69287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell         if (u_minify(resource->width0, level) == rtt_width &&
70287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell             u_minify(resource->height0, level) == rtt_height) {
714c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger            struct pipe_surface surf_tmpl;
724c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger            memset(&surf_tmpl, 0, sizeof(surf_tmpl));
7321b0996dfcbe62c55dd31cc316cd47f2b8531d2eDave Airlie            surf_tmpl.format = format;
744c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger            surf_tmpl.usage = PIPE_BIND_RENDER_TARGET;
754c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger            surf_tmpl.u.tex.level = level;
764c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger            surf_tmpl.u.tex.first_layer = strb->rtt_face + strb->rtt_slice;
774c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger            surf_tmpl.u.tex.last_layer = strb->rtt_face + strb->rtt_slice;
781a82d9648b3db780e58e4966924157542d148c58Brian Paul
791a82d9648b3db780e58e4966924157542d148c58Brian Paul            pipe_surface_reference(&strb->surface, NULL);
801a82d9648b3db780e58e4966924157542d148c58Brian Paul
814c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger            strb->surface = pipe->create_surface(pipe,
824c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger                                                 resource,
834c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger                                                 &surf_tmpl);
841a82d9648b3db780e58e4966924157542d148c58Brian Paul#if 0
851a82d9648b3db780e58e4966924157542d148c58Brian Paul            printf("-- alloc new surface %d x %d into tex %p\n",
861a82d9648b3db780e58e4966924157542d148c58Brian Paul                   strb->surface->width, strb->surface->height,
871a82d9648b3db780e58e4966924157542d148c58Brian Paul                   texture);
881a82d9648b3db780e58e4966924157542d148c58Brian Paul#endif
891a82d9648b3db780e58e4966924157542d148c58Brian Paul            break;
901a82d9648b3db780e58e4966924157542d148c58Brian Paul         }
911a82d9648b3db780e58e4966924157542d148c58Brian Paul      }
921a82d9648b3db780e58e4966924157542d148c58Brian Paul   }
931a82d9648b3db780e58e4966924157542d148c58Brian Paul}
941a82d9648b3db780e58e4966924157542d148c58Brian Paul
951a82d9648b3db780e58e4966924157542d148c58Brian Paul
96fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian/**
97fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian * Update framebuffer state (color, depth, stencil, etc. buffers)
98fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian */
99fe1f94f26c5cb43934513a38f60bf00aa542e64cBrianstatic void
100fe1f94f26c5cb43934513a38f60bf00aa542e64cBrianupdate_framebuffer_state( struct st_context *st )
101fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian{
10211c34dc644fe58b8178ab9142929a6685e3c0848Brian Paul   struct pipe_framebuffer_state *framebuffer = &st->state.framebuffer;
103fd3876e9e3fb17df61dbf45d400796d682a8fbe0Brian   struct gl_framebuffer *fb = st->ctx->DrawBuffer;
104f5713c7d2e7ba8e1170fd9b1dd95379662ab6117Brian   struct st_renderbuffer *strb;
105b66ee9e32fa044b39588df67c00858bbab64f13dKeith Whitwell   GLuint i;
106fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian
107162b3ad94d52b28d83462202952987012387e12fMarek Olšák   st_flush_bitmap_cache(st);
108162b3ad94d52b28d83462202952987012387e12fMarek Olšák
109162b3ad94d52b28d83462202952987012387e12fMarek Olšák   st->state.fb_orientation = st_fb_orientation(fb);
1103a4ece89a799a7c699be229a6d965d4820af0196Brian   framebuffer->width = fb->Width;
1113a4ece89a799a7c699be229a6d965d4820af0196Brian   framebuffer->height = fb->Height;
1123a4ece89a799a7c699be229a6d965d4820af0196Brian
1131a82d9648b3db780e58e4966924157542d148c58Brian Paul   /*printf("------ fb size %d x %d\n", fb->Width, fb->Height);*/
1141a82d9648b3db780e58e4966924157542d148c58Brian Paul
115fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian   /* Examine Mesa's ctx->DrawBuffer->_ColorDrawBuffers state
116fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian    * to determine which surfaces to draw to
117fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian    */
1182299f21f8da816fc4588492965e7dac422da1a96Zack Rusin   framebuffer->nr_cbufs = 0;
119b66ee9e32fa044b39588df67c00858bbab64f13dKeith Whitwell   for (i = 0; i < fb->_NumColorDrawBuffers; i++) {
120b66ee9e32fa044b39588df67c00858bbab64f13dKeith Whitwell      strb = st_renderbuffer(fb->_ColorDrawBuffers[i]);
1211a82d9648b3db780e58e4966924157542d148c58Brian Paul
122264c3d218aa2fd8d06b0e91c1079e43e58d3ee7eAlan Hourihane      if (strb) {
123264c3d218aa2fd8d06b0e91c1079e43e58d3ee7eAlan Hourihane         /*printf("--------- framebuffer surface rtt %p\n", strb->rtt);*/
12451e41a0d894109249448ecc6a6bfc09e095acadaChristoph Bumiller         if (strb->rtt ||
12551e41a0d894109249448ecc6a6bfc09e095acadaChristoph Bumiller             (strb->texture && util_format_is_srgb(strb->texture->format))) {
126264c3d218aa2fd8d06b0e91c1079e43e58d3ee7eAlan Hourihane            /* rendering to a GL texture, may have to update surface */
127264c3d218aa2fd8d06b0e91c1079e43e58d3ee7eAlan Hourihane            update_renderbuffer_surface(st, strb);
128264c3d218aa2fd8d06b0e91c1079e43e58d3ee7eAlan Hourihane         }
12980a718a63bf2fa817e346f0f5731ee9ef2e0e68bBrian Paul
130264c3d218aa2fd8d06b0e91c1079e43e58d3ee7eAlan Hourihane         if (strb->surface) {
131f104e4d666dfccda6f5ad817693216733ddede44Brian Paul            pipe_surface_reference(&framebuffer->cbufs[framebuffer->nr_cbufs],
132f104e4d666dfccda6f5ad817693216733ddede44Brian Paul                                   strb->surface);
133264c3d218aa2fd8d06b0e91c1079e43e58d3ee7eAlan Hourihane            framebuffer->nr_cbufs++;
134264c3d218aa2fd8d06b0e91c1079e43e58d3ee7eAlan Hourihane         }
135b85b315ebbe25efbd118887bdc87a562d4334fccBrian Paul         strb->defined = GL_TRUE; /* we'll be drawing something */
136e4b3c13d7a7dbd716bdf4b4d2dda8c6e579bd2d1Brian      }
137fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian   }
138f104e4d666dfccda6f5ad817693216733ddede44Brian Paul   for (i = framebuffer->nr_cbufs; i < PIPE_MAX_COLOR_BUFS; i++) {
139f104e4d666dfccda6f5ad817693216733ddede44Brian Paul      pipe_surface_reference(&framebuffer->cbufs[i], NULL);
140f104e4d666dfccda6f5ad817693216733ddede44Brian Paul   }
141fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian
142f104e4d666dfccda6f5ad817693216733ddede44Brian Paul   /*
143f104e4d666dfccda6f5ad817693216733ddede44Brian Paul    * Depth/Stencil renderbuffer/surface.
144f104e4d666dfccda6f5ad817693216733ddede44Brian Paul    */
145fd3876e9e3fb17df61dbf45d400796d682a8fbe0Brian   strb = st_renderbuffer(fb->Attachment[BUFFER_DEPTH].Renderbuffer);
146f5713c7d2e7ba8e1170fd9b1dd95379662ab6117Brian   if (strb) {
1472f07e1caa02c9dcdddc673e61eb91b83b82d283bBrian Paul      if (strb->rtt) {
1482f07e1caa02c9dcdddc673e61eb91b83b82d283bBrian Paul         /* rendering to a GL texture, may have to update surface */
1492f07e1caa02c9dcdddc673e61eb91b83b82d283bBrian Paul         update_renderbuffer_surface(st, strb);
1502f07e1caa02c9dcdddc673e61eb91b83b82d283bBrian Paul      }
151f104e4d666dfccda6f5ad817693216733ddede44Brian Paul      pipe_surface_reference(&framebuffer->zsbuf, strb->surface);
152ecfa794037e8be351ecfec0229d1e3b1677ae369Brian   }
1538de2331e432e2ea6f978acb6c80666da99c6c4a1Brian   else {
1548de2331e432e2ea6f978acb6c80666da99c6c4a1Brian      strb = st_renderbuffer(fb->Attachment[BUFFER_STENCIL].Renderbuffer);
1558de2331e432e2ea6f978acb6c80666da99c6c4a1Brian      if (strb) {
1568de2331e432e2ea6f978acb6c80666da99c6c4a1Brian         assert(strb->surface);
157f104e4d666dfccda6f5ad817693216733ddede44Brian Paul         pipe_surface_reference(&framebuffer->zsbuf, strb->surface);
1588de2331e432e2ea6f978acb6c80666da99c6c4a1Brian      }
159e2a8ef4430e153589a9d1a284c8f2005a4a98410José Fonseca      else
160e2a8ef4430e153589a9d1a284c8f2005a4a98410José Fonseca         pipe_surface_reference(&framebuffer->zsbuf, NULL);
161ecfa794037e8be351ecfec0229d1e3b1677ae369Brian   }
162ecfa794037e8be351ecfec0229d1e3b1677ae369Brian
16317249ae8e0e459dea250733a0b3e45036cdb67bdBrian Paul#ifdef DEBUG
16417249ae8e0e459dea250733a0b3e45036cdb67bdBrian Paul   /* Make sure the resource binding flags were set properly */
16517249ae8e0e459dea250733a0b3e45036cdb67bdBrian Paul   for (i = 0; i < framebuffer->nr_cbufs; i++) {
16617249ae8e0e459dea250733a0b3e45036cdb67bdBrian Paul      assert(framebuffer->cbufs[i]->texture->bind & PIPE_BIND_RENDER_TARGET);
16717249ae8e0e459dea250733a0b3e45036cdb67bdBrian Paul   }
16817249ae8e0e459dea250733a0b3e45036cdb67bdBrian Paul   if (framebuffer->zsbuf) {
16917249ae8e0e459dea250733a0b3e45036cdb67bdBrian Paul      assert(framebuffer->zsbuf->texture->bind & PIPE_BIND_DEPTH_STENCIL);
17017249ae8e0e459dea250733a0b3e45036cdb67bdBrian Paul   }
17117249ae8e0e459dea250733a0b3e45036cdb67bdBrian Paul#endif
17217249ae8e0e459dea250733a0b3e45036cdb67bdBrian Paul
17311c34dc644fe58b8178ab9142929a6685e3c0848Brian Paul   cso_set_framebuffer(st->cso_context, framebuffer);
174fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian}
175fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian
176fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian
177fe1f94f26c5cb43934513a38f60bf00aa542e64cBrianconst struct st_tracked_state st_update_framebuffer = {
17854507125e735ffa595e252282eaabf38095c21e1Alan Hourihane   "st_update_framebuffer",				/* name */
17954507125e735ffa595e252282eaabf38095c21e1Alan Hourihane   {							/* dirty */
18054507125e735ffa595e252282eaabf38095c21e1Alan Hourihane      _NEW_BUFFERS,					/* mesa */
1811a82d9648b3db780e58e4966924157542d148c58Brian Paul      ST_NEW_FRAMEBUFFER,				/* st */
182fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian   },
18354507125e735ffa595e252282eaabf38095c21e1Alan Hourihane   update_framebuffer_state				/* update */
184fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian};
185fe1f94f26c5cb43934513a38f60bf00aa542e64cBrian
186