1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     gfx_OnScreenBuffer.cpp
4 
5   Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Revision: 26052 $
14  *---------------------------------------------------------------------------*/
15 
16 #include "precompiled.h"
17 
18 #include <nw/gfx/gfx_OnScreenBuffer.h>
19 
20 #include <nw/os/os_Memory.h>
21 
22 #include <nn/gx.h>
23 
24 namespace nw
25 {
26 namespace gfx
27 {
28 NW_UT_RUNTIME_TYPEINFO_DEFINITION(OnScreenBuffer, IRenderTarget);
29 
30 //----------------------------------------
OnScreenBuffer(os::IAllocator * pAllocator,const Description & description)31 OnScreenBuffer::OnScreenBuffer(os::IAllocator* pAllocator, const Description& description)
32 : IRenderTarget(pAllocator),
33   m_ColorBuffer(0),
34   m_DepthBuffer(0),
35   m_Description(description)
36 {
37 
38     GLuint fboID;
39 
40     glGenFramebuffers(1, &fboID);
41 
42     glBindFramebuffer(GL_FRAMEBUFFER, fboID);
43 
44     glGenRenderbuffers(1, &m_ColorBuffer);
45     glGenRenderbuffers(1, &m_DepthBuffer);
46 
47     NW_ASSERT(MEMORY_AREA_FCRAM < m_Description.colorArea && m_Description.colorArea < GRAPHICS_MEMORY_AREA_COUNT);
48     NW_ASSERT(MEMORY_AREA_FCRAM < m_Description.depthArea && m_Description.depthArea < GRAPHICS_MEMORY_AREA_COUNT);
49 
50     glBindRenderbuffer(GL_RENDERBUFFER, m_ColorBuffer);
51     glRenderbufferStorage(
52         GL_RENDERBUFFER | m_Description.colorArea,
53         m_Description.colorFormat,
54         m_Description.width,
55         m_Description.height);
56     glFramebufferRenderbuffer(
57         GL_FRAMEBUFFER,
58         GL_COLOR_ATTACHMENT0,
59         GL_RENDERBUFFER,
60         m_ColorBuffer);
61 
62     glBindRenderbuffer(GL_RENDERBUFFER, m_DepthBuffer);
63     glRenderbufferStorage(
64         GL_RENDERBUFFER | m_Description.depthArea,
65         m_Description.depthFormat,
66         m_Description.width,
67         m_Description.height);
68 
69     if (m_Description.depthFormat == RENDER_DEPTH_FORMAT_24_STENCIL8)
70     {
71         glFramebufferRenderbuffer(
72             GL_FRAMEBUFFER,
73             GL_DEPTH_STENCIL_ATTACHMENT,
74             GL_RENDERBUFFER,
75             m_DepthBuffer);
76     }
77     else
78     {
79         glFramebufferRenderbuffer(
80             GL_FRAMEBUFFER,
81             GL_DEPTH_ATTACHMENT,
82             GL_RENDERBUFFER,
83             m_DepthBuffer);
84     }
85 
86     m_BackBufferObject.SetFboID( fboID );
87     m_BackBufferObject.SetWidth( m_Description.width );
88     m_BackBufferObject.SetHeight( m_Description.height );
89     m_BackBufferObject.SetColorFormat( m_Description.colorFormat );
90     m_BackBufferObject.SetDepthFormat( m_Description.depthFormat );
91 
92     glBindFramebuffer(GL_FRAMEBUFFER, 0);
93 }
94 
95 //----------------------------------------
~OnScreenBuffer()96 OnScreenBuffer::~OnScreenBuffer()
97 {
98     if (m_DepthBuffer != 0)
99     {
100         glDeleteRenderbuffers(1, &m_DepthBuffer);
101     }
102 
103     if (m_ColorBuffer != 0)
104     {
105         glDeleteRenderbuffers(1, &m_ColorBuffer);
106     }
107 
108     if (m_BackBufferObject.GetFboID() != 0)
109     {
110         GLuint fboID = m_BackBufferObject.GetFboID();
111         glDeleteFramebuffers(1, &fboID);
112     }
113 }
114 
115 } // namespace gfx
116 } // namespace nw
117