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