1 /*---------------------------------------------------------------------------* 2 Project: Horizon 3 File: demo_RenderSystem.cpp 4 5 Copyright (C)2009-2012 Nintendo Co., Ltd. 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 $Rev: 46365 $ 14 *---------------------------------------------------------------------------*/ 15 16 #include <nn/applet.h> 17 #include "demo/Render/demo_RenderSystem.h" 18 19 namespace demo 20 { 21 RenderSystem(void)22 RenderSystem::RenderSystem(void) : 23 m_InitializeFlag(false), m_InitializeLcdDisplayFlag(false), 24 m_TargetDisplay(NN_GX_DISPLAY0), 25 m_CurrentDisplayBuffersPtr(NULL), m_CurrentFrameBufferPtr(NULL), 26 m_DisplayBuffers0(), m_FrameBuffer0(), 27 m_DisplayBuffers1() 28 { 29 } 30 ~RenderSystem(void)31 RenderSystem::~RenderSystem(void) 32 { 33 Finalize(); 34 } 35 Initialize(const uptr fcramAddress,const size_t memorySize,const u32 commandBufferSize,const u32 requestNum,const bool serialRunMode,const DisplayBuffersDescription & displayBuffers0Desc,const DisplayBuffersDescription & displayBuffers1Desc,const FrameBufferDescription & frameBuffer0Desc,const DisplayBuffersDescription & displayBuffers0ExtDesc,const bool isFillBlackLCD)36 void RenderSystem::Initialize(const uptr fcramAddress, const size_t memorySize, 37 const u32 commandBufferSize, const u32 requestNum, 38 const bool serialRunMode, 39 const DisplayBuffersDescription& displayBuffers0Desc, 40 const DisplayBuffersDescription& displayBuffers1Desc, 41 const FrameBufferDescription& frameBuffer0Desc, 42 const DisplayBuffersDescription& displayBuffers0ExtDesc, 43 const bool isFillBlackLCD) 44 { 45 NN_UNUSED_VAR(displayBuffers0ExtDesc); 46 DEMO_FORCE_LINK_APPLET_LIB(); 47 48 if ( m_InitializeFlag ) 49 { 50 NN_TPANIC_("Initialize() was called twice.\n"); 51 } 52 53 demo::InitializeMemoryManager(fcramAddress, memorySize); 54 if ( nngxInitialize(demo::GetAllocator, demo::GetDeallocator) == GL_FALSE ) 55 { 56 NN_TPANIC_("nngxInitialize() failed.\n"); 57 } 58 59 m_CommandList.Initialize(commandBufferSize, requestNum, serialRunMode); 60 61 DisplayBuffers::Create(displayBuffers0Desc, m_DisplayBuffers0); 62 DEMO_ASSERT_GL_ERROR(); 63 64 DisplayBuffers::Create(displayBuffers1Desc, m_DisplayBuffers1); 65 DEMO_ASSERT_GL_ERROR(); 66 67 FrameBuffer::Create(frameBuffer0Desc, m_FrameBuffer0); 68 DEMO_ASSERT_GL_ERROR(); 69 70 m_InitializeFlag = true; 71 72 if ( isFillBlackLCD ) 73 InitializeLcdDisplay(); 74 75 DEMO_ASSERT_GL_ERROR(); 76 } 77 InitializeLcdDisplay(void)78 void RenderSystem::InitializeLcdDisplay(void) 79 { 80 // Initializes the upper and lower screen display buffers with black. 81 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 82 SetRenderTarget(NN_GX_DISPLAY0); 83 Clear(); 84 SwapBuffers(); 85 86 SetRenderTarget(NN_GX_DISPLAY1); 87 Clear(); 88 SwapBuffers(); 89 90 SetRenderTarget(NN_GX_DISPLAY0); 91 Clear(); 92 SwapBuffers(); 93 94 SetRenderTarget(NN_GX_DISPLAY1); 95 Clear(); 96 SwapBuffers(); 97 98 WaitVsync(NN_GX_DISPLAY_BOTH); 99 100 if (! m_InitializeLcdDisplayFlag ) 101 { 102 nngxStartLcdDisplay(); 103 m_InitializeLcdDisplayFlag = true; 104 } 105 } 106 Finalize(void)107 void RenderSystem::Finalize(void) 108 { 109 if ( ! m_InitializeFlag ) 110 { 111 return; 112 } 113 114 DEMO_ASSERT_GL_ERROR(); 115 116 DisplayBuffers::Destroy(m_DisplayBuffers0); 117 DisplayBuffers::Destroy(m_DisplayBuffers1); 118 FrameBuffer::Destroy(m_FrameBuffer0); 119 120 m_CommandList.Stop(); 121 DEMO_ASSERT_GL_ERROR(); 122 123 m_CommandList.Finalize(); 124 DEMO_ASSERT_GL_ERROR(); 125 126 nngxFinalize(); 127 DEMO_ASSERT_GL_ERROR(); 128 129 demo::FinalizeMemoryManager(); 130 DEMO_ASSERT_GL_ERROR(); 131 132 m_InitializeFlag = false; 133 } 134 SetRenderTarget(const GLenum targetDisplay)135 void RenderSystem::SetRenderTarget(const GLenum targetDisplay) 136 { 137 if (! m_InitializeFlag ) 138 { 139 NN_TPANIC_("Initialize() was not called.\n"); 140 } 141 142 if ( targetDisplay == NN_GX_DISPLAY0 ) 143 { 144 m_TargetDisplay = targetDisplay; 145 146 m_CurrentFrameBufferPtr = &m_FrameBuffer0; 147 m_CurrentDisplayBuffersPtr = &m_DisplayBuffers0; 148 } 149 else if ( targetDisplay == NN_GX_DISPLAY1 ) 150 { 151 m_TargetDisplay = targetDisplay; 152 153 m_CurrentFrameBufferPtr = &m_FrameBuffer0; 154 m_CurrentDisplayBuffersPtr = &m_DisplayBuffers1; 155 } 156 else 157 { 158 NN_TPANIC_("Invalid display name.\n"); 159 } 160 161 m_CurrentFrameBufferPtr->Bind(); 162 SetViewport(0, 0, m_CurrentDisplayBuffersPtr->GetWidth(), m_CurrentDisplayBuffersPtr->GetHeight()); 163 164 CheckRenderTarget(); 165 } 166 SetViewport(const GLint x,const GLint y,const GLsizei width,const GLsizei height)167 void RenderSystem::SetViewport(const GLint x, const GLint y, const GLsizei width, const GLsizei height) 168 { 169 glViewport(x, y, width, height); 170 } 171 Clear(const GLbitfield mask)172 void RenderSystem::Clear(const GLbitfield mask) 173 { 174 CheckRenderTarget(); 175 176 m_CurrentFrameBufferPtr->ClearBuffer(mask); 177 } 178 ClearColorDepthStencilBuffer(const GLclampf red,const GLclampf green,const GLclampf blue,const GLclampf alpha,const GLclampf depth,const GLclampf stencil)179 void RenderSystem::ClearColorDepthStencilBuffer(const GLclampf red, 180 const GLclampf green, const GLclampf blue, const GLclampf alpha, 181 const GLclampf depth, const GLclampf stencil) 182 { 183 CheckRenderTarget(); 184 185 m_CurrentFrameBufferPtr->ClearColorDepthStencilBuffer(red, green, blue, alpha, 186 depth, stencil); 187 CheckRenderTarget(); 188 } 189 ClearColorDepthBuffer(const GLclampf red,const GLclampf green,const GLclampf blue,const GLclampf alpha,const GLclampf depth)190 void RenderSystem::ClearColorDepthBuffer(const GLclampf red, 191 const GLclampf green, const GLclampf blue, const GLclampf alpha, 192 const GLclampf depth) 193 { 194 CheckRenderTarget(); 195 196 m_CurrentFrameBufferPtr->ClearColorDepthBuffer(red, green, blue, alpha, 197 depth); 198 CheckRenderTarget(); 199 } 200 ClearColorBuffer(const GLclampf red,const GLclampf green,const GLclampf blue,const GLclampf alpha)201 void RenderSystem::ClearColorBuffer(const GLclampf red, const GLclampf green, 202 const GLclampf blue, const GLclampf alpha) 203 { 204 CheckRenderTarget(); 205 206 m_CurrentFrameBufferPtr->ClearColorBuffer(red, green, blue, alpha); 207 CheckRenderTarget(); 208 } 209 ClearDepthStencilBuffer(const GLclampf depth,const GLclampf stencil)210 void RenderSystem::ClearDepthStencilBuffer(const GLclampf depth, const GLclampf stencil) 211 { 212 CheckRenderTarget(); 213 214 m_CurrentFrameBufferPtr->ClearDepthStencilBuffer(depth, stencil); 215 CheckRenderTarget(); 216 } 217 GetDisplayBufferObjectId(const GLenum targetDisplay,GLuint & displayBufferId0,GLuint & displayBufferId1)218 void RenderSystem::GetDisplayBufferObjectId(const GLenum targetDisplay, 219 GLuint& displayBufferId0, GLuint& displayBufferId1) 220 { 221 222 if (! m_InitializeFlag ) 223 { 224 NN_TPANIC_("Initialize() was not called.\n"); 225 } 226 227 if ( targetDisplay == NN_GX_DISPLAY0 ) 228 { 229 displayBufferId0 = m_DisplayBuffers0.GetDisplayBufferId(0); 230 displayBufferId1 = m_DisplayBuffers0.GetDisplayBufferId(1); 231 } 232 else if ( targetDisplay == NN_GX_DISPLAY1 ) 233 { 234 displayBufferId0 = m_DisplayBuffers1.GetDisplayBufferId(0); 235 displayBufferId1 = m_DisplayBuffers1.GetDisplayBufferId(1); 236 } 237 else 238 { 239 NN_TPANIC_("Invalid display name.\n"); 240 } 241 } 242 GetFrameBufferObjectId(const GLenum targetDisplay)243 GLuint RenderSystem::GetFrameBufferObjectId(const GLenum targetDisplay) 244 { 245 if (! m_InitializeFlag ) 246 { 247 NN_TPANIC_("Initialize() was not called.\n"); 248 } 249 250 if ( ( targetDisplay == NN_GX_DISPLAY0 ) || ( targetDisplay == NN_GX_DISPLAY1 ) ) 251 { 252 return m_FrameBuffer0.GetFrameBufferId(); 253 } 254 else 255 { 256 NN_TPANIC_("Invalid display name.\n"); 257 258 return 0; 259 } 260 } 261 GetRenderBufferObjectId(const GLenum targetDisplay,const GLenum renderBufferType)262 GLuint RenderSystem::GetRenderBufferObjectId(const GLenum targetDisplay, const GLenum renderBufferType) 263 { 264 if (! m_InitializeFlag ) 265 { 266 NN_TPANIC_("Initialize() was not called.\n"); 267 } 268 269 if ( ( targetDisplay == NN_GX_DISPLAY0 ) || ( targetDisplay == NN_GX_DISPLAY1 ) ) 270 { 271 if ( renderBufferType == GL_COLOR_ATTACHMENT0 ) 272 { 273 return m_FrameBuffer0.GetRenderBufferId(0); 274 } 275 else if ( renderBufferType == GL_DEPTH_STENCIL_ATTACHMENT ) 276 { 277 return m_FrameBuffer0.GetRenderBufferId(1); 278 } 279 else 280 { 281 NN_TPANIC_("Invalid renderBufferType.\n"); 282 283 return 0; 284 } 285 } 286 else 287 { 288 NN_TPANIC_("Invalid display name.\n"); 289 290 return 0; 291 } 292 } 293 SwapBuffers(void)294 void RenderSystem::SwapBuffers(void) 295 { 296 CheckRenderTarget(); 297 298 // Transfers from color buffer to display buffer 299 m_CurrentFrameBufferPtr->Bind(); 300 m_CurrentDisplayBuffersPtr->Bind(); 301 // Gets the ID of the transfer destination display buffer 302 GLuint displayBufferId = m_CurrentDisplayBuffersPtr->GetTargetDisplayBufferId(); 303 m_CurrentFrameBufferPtr->Transfer(displayBufferId, 304 NN_GX_ANTIALIASE_NOT_USED, GL_FALSE, 0, 0); 305 306 // Execute the command list 307 m_CommandList.Run(); 308 m_CommandList.Swap(); 309 310 nngxSwapBuffers(m_TargetDisplay); 311 312 m_CurrentDisplayBuffersPtr->IncreaseBufferIndex(); 313 } 314 WaitVsync(const GLenum targetDisplay)315 void RenderSystem::WaitVsync(const GLenum targetDisplay) 316 { 317 CheckRenderTarget(); 318 319 if ( ( targetDisplay == NN_GX_DISPLAY0 ) || ( targetDisplay == NN_GX_DISPLAY1 ) || 320 ( targetDisplay == NN_GX_DISPLAY_BOTH ) ) 321 { 322 nngxWaitVSync(targetDisplay); 323 } 324 else 325 { 326 NN_TPANIC_("Invalid display name.\n"); 327 } 328 } 329 SetSerialRun(const bool flag)330 void RenderSystem::SetSerialRun(const bool flag) 331 { 332 m_CommandList.SetSerialRun(flag); 333 } 334 CheckRenderTarget(void)335 void RenderSystem::CheckRenderTarget(void) 336 { 337 if (! m_InitializeFlag ) 338 { 339 NN_TPANIC_("Initialize() was not called.\n"); 340 } 341 342 if (! ( ( m_TargetDisplay == NN_GX_DISPLAY0 ) || ( m_TargetDisplay == NN_GX_DISPLAY1 ) ) ) 343 { 344 NN_TPANIC_("Invalid display name. Call SetRenderTarget().\n"); 345 } 346 347 if ( m_CurrentDisplayBuffersPtr == NULL ) 348 { 349 NN_TPANIC_("m_CurrentDisplayBufferPtr is NULL. Call SetRenderTarget().\n"); 350 } 351 352 if ( m_CurrentFrameBufferPtr == NULL ) 353 { 354 NN_TPANIC_("m_CurrentFrameBufferPtr is NULL. Call SetRenderTarget().\n"); 355 } 356 } 357 358 } 359