/*---------------------------------------------------------------------------* Project: Horizon File: gr_RenderState.cpp Copyright (C)2010 Nintendo Co., Ltd. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. $Rev: 25760 $ *---------------------------------------------------------------------------*/ #include namespace nn { namespace gr { namespace CTR { u32* RenderState::Culling::MakeCommand( u32* command, bool isUpdateFBAccess ) const { u32 culling = 0; if ( isEnable ) { if ( ( frontFace == FRONT_FACE_CW && cullFace == CULL_FACE_FRONT ) || ( frontFace == FRONT_FACE_CCW && cullFace == CULL_FACE_BACK ) ) { culling = 2; } else { culling = 1; } } // 0x040 *command++ = culling; *command++ = PICA_CMD_HEADER_SINGLE( PICA_REG_CULL_FACE ); return isUpdateFBAccess ? m_RenderState.fbAccess.MakeCommand( command ) : command; } //------------------------------------------------------------------------------ u32* RenderState::Blend::MakeCommand( u32* command, bool isUpdateFBAccess ) const { if ( isEnable ) { // 0x100 *command++ = PICA_CMD_DATA_COLOR_OPERATION( PICA_DATA_FRAGOP_MODE_DMP, PICA_DATA_ENABLE_BLEND ); *command++ = PICA_CMD_HEADER_SINGLE_BE( PICA_REG_COLOR_OPERATION, 0x3 ); // 0x101 *command++ = PICA_CMD_DATA_BLEND_FUNC_SEPARATE( eqRgb, eqAlpha, srcRgb, dstRgb, srcAlpha, dstAlpha ); *command++ = PICA_CMD_HEADER_SINGLE( PICA_REG_BLEND_FUNC ); // 0x102 *command++ = PICA_CMD_DATA_LOGIC_OP( PICA_DATA_LOGIC_NOOP ); *command++ = PICA_CMD_HEADER_SINGLE( PICA_REG_LOGIC_OP ); // 0x103 *command++ = colorR | colorG << 8 | colorB << 16 | colorA << 24; *command++ = PICA_CMD_HEADER_SINGLE( PICA_REG_BLEND_COLOR ); } else { // 0x100 *command++ = PICA_CMD_DATA_COLOR_OPERATION( PICA_DATA_FRAGOP_MODE_DMP, PICA_DATA_ENABLE_BLEND ); *command++ = PICA_CMD_HEADER_SINGLE_BE( PICA_REG_COLOR_OPERATION, 0x3 ); // 0x101 *command++ = PICA_CMD_DATA_BLEND_FUNC( PICA_DATA_BLEND_EQUATION_ADD, PICA_DATA_BLEND_FUNC_ONE, PICA_DATA_BLEND_FUNC_ZERO ); *command++ = PICA_CMD_HEADER_SINGLE( PICA_REG_BLEND_FUNC ); } return isUpdateFBAccess ? m_RenderState.fbAccess.MakeCommand( command ) : command; } //------------------------------------------------------------------------------ u32* RenderState::LogicOp::MakeCommand( u32* command, bool isUpdateFBAccess ) const { if ( isEnable ) { // 0x100 *command++ = PICA_CMD_DATA_COLOR_OPERATION( PICA_DATA_FRAGOP_MODE_DMP, PICA_DATA_ENABLE_COLOR_LOGIC_OP ); *command++ = PICA_CMD_HEADER_SINGLE_BE( PICA_REG_COLOR_OPERATION, 0x3 ); // 0x101 *command++ = PICA_CMD_DATA_LOGIC_OP_ENABLE(); *command++ = PICA_CMD_HEADER_SINGLE( PICA_REG_BLEND_FUNC ); // 0x102 *command++ = PICA_CMD_DATA_LOGIC_OP( opCode ); *command++ = PICA_CMD_HEADER_SINGLE( PICA_REG_LOGIC_OP ); } return isUpdateFBAccess ? m_RenderState.fbAccess.MakeCommand( command ) : command; } //------------------------------------------------------------------------------ u32* RenderState::AlphaTest::MakeCommand( u32* command, bool isUpdateFBAccess ) const { // 0x104 *command++ = PICA_CMD_DATA_FRAGOP_ALPHA_TEST( isEnable, func, refValue ); *command++ = PICA_CMD_HEADER_SINGLE_BE( PICA_REG_FRAGOP_ALPHA_TEST, 0x3 ); return isUpdateFBAccess ? m_RenderState.fbAccess.MakeCommand( command ) : command; } //------------------------------------------------------------------------------ u32* RenderState::StencilTest::MakeCommand( u32* command, bool isUpdateFBAccess ) const { // 0x105 *command++ = PICA_CMD_DATA_STENCIL_TEST( isEnable, func, isEnableWrite, ref, mask ); *command++ = PICA_CMD_HEADER_SINGLE( PICA_REG_STENCIL_TEST ); // 0x106 *command++ = PICA_CMD_DATA_STENCIL_OP( opFail, opZFail, opZPass ); *command++ = PICA_CMD_HEADER_SINGLE( PICA_REG_STENCIL_OP ); return isUpdateFBAccess ? m_RenderState.fbAccess.MakeCommand( command ) : command; } //------------------------------------------------------------------------------ u32* RenderState::DepthTest::MakeCommand( u32* command, bool isUpdateFBAccess ) const { // 0x107の[ 0:7 ]と[12:12]がデプステストの設定で、[8:11]がカラーマスクの設定です。 *command++ = PICA_CMD_DATA_DEPTH_COLOR_MASK( isEnable, func, m_RenderState.colorMask & COLOR_MASK_R, m_RenderState.colorMask & COLOR_MASK_G, m_RenderState.colorMask & COLOR_MASK_B, m_RenderState.colorMask & COLOR_MASK_A, isEnableWrite ); *command++ = PICA_CMD_HEADER_SINGLE( PICA_REG_DEPTH_COLOR_MASK ); return isUpdateFBAccess ? m_RenderState.fbAccess.MakeCommand( command ) : command; } //------------------------------------------------------------------------------ u32* RenderState::WBuffer::MakeCommand( u32* command, bool isUpdateFBAccess ) const { if ( wScale == 0.f ) { // 0x6d *command++ = 1; *command++ = PICA_CMD_HEADER_SINGLE( PICA_REG_FRAGOP_WSCALE ); // 0x04d *command++ = Float32ToFloat24( depthRangeNear - depthRangeFar ); *command++ = PICA_CMD_HEADER_SINGLE( PICA_REG_FRAGOP_WSCALE_DATA1 ); // 0x04e f32 zNear = isEnablePolygonOffset ? depthRangeNear - (depthRangeNear - depthRangeFar) * polygonOffsetUnit / f32(1 << depthRangeBit - 1) : depthRangeNear; *command++ = Float32ToFloat24( zNear ); *command++ = PICA_CMD_HEADER_SINGLE( PICA_REG_FRAGOP_WSCALE_DATA2 ); } else { // 0x6d *command++ = 0; *command++ = PICA_CMD_HEADER_SINGLE( PICA_REG_FRAGOP_WSCALE ); // 0x04d *command++ = Float32ToFloat24( -wScale ); *command++ = PICA_CMD_HEADER_SINGLE( PICA_REG_FRAGOP_WSCALE_DATA1 ); // 0x04e *command++ = 0; *command++ = PICA_CMD_HEADER_SINGLE( PICA_REG_FRAGOP_WSCALE_DATA2 ); } return isUpdateFBAccess ? m_RenderState.fbAccess.MakeCommand( command ) : command; } //------------------------------------------------------------------------------ u32* RenderState::FBAccess::MakeCommand( u32* command ) const { // 0x111 *command++ = 1; *command++ = PICA_CMD_HEADER_SINGLE( PICA_REG_COLOR_DEPTH_BUFFER_CLEAR1 ); // 0x110 *command++ = 1; *command++ = PICA_CMD_HEADER_SINGLE( PICA_REG_COLOR_DEPTH_BUFFER_CLEAR0 ); // 0x112 *command++ = ( (m_RenderState.colorMask && m_RenderState.colorMask != 0xf) || (m_RenderState.colorMask && m_RenderState.blend.isEnable ) || (m_RenderState.colorMask && m_RenderState.logicOp.isEnable ) ) ? 0xf : 0; *command++ = PICA_CMD_HEADER_SINGLE_BE( PICA_REG_COLOR_BUFFER_READ, 0x1 ); // 0x113 *command++ = m_RenderState.colorMask ? 0xf : 0; *command++ = PICA_CMD_HEADER_SINGLE_BE( PICA_REG_COLOR_BUFFER_WRITE, 0x1 ); u32 depth_stencil_read = 0; u32 depth_stencil_write = 0; if ( m_RenderState.depthTest.isEnable ) { if ( m_RenderState.depthTest.isEnableWrite ) { depth_stencil_read |= 2; depth_stencil_write |= 2; } else if ( m_RenderState.colorMask ) { depth_stencil_read |= 2; } } if ( m_RenderState.stencilTest.isEnable ) { if ( m_RenderState.stencilTest.isEnableWrite ) { depth_stencil_read |= 1; depth_stencil_write |= 1; } else if ( m_RenderState.colorMask ) { depth_stencil_read |= 1; } } // 0x114 *command++ = depth_stencil_read; *command++ = PICA_CMD_HEADER_SINGLE_BE( PICA_REG_DEPTH_STENCIL_BUFFER_READ, 0x1 ); // 0x115 *command++ = depth_stencil_write; *command++ = PICA_CMD_HEADER_SINGLE_BE( PICA_REG_DEPTH_STENCIL_BUFFER_WRITE, 0x1 ); return command; } //------------------------------------------------------------------------------ u32* RenderState::RenderState::MakeCommand( u32* buffer ) const { u32* command = buffer; command = cullingTest.MakeCommand( command, false ); command = blend.MakeCommand( command, false ); command = logicOp.MakeCommand( command, false ); command = alphaTest.MakeCommand( command, false ); command = stencilTest.MakeCommand( command, false ); command = depthTest.MakeCommand( command, false ); command = wBuffer.MakeCommand( command, false ); command = fbAccess.MakeCommand( command ); return command; } //------------------------------------------------------------------------------ RenderState::Culling::Culling( const RenderState& renderState_ ) : isEnable( true ), frontFace( FRONT_FACE_CCW ), cullFace( CULL_FACE_BACK ), m_RenderState( renderState_ ) { } //------------------------------------------------------------------------------ RenderState::Blend::Blend( const RenderState& renderState_ ) : isEnable( false ), eqRgb ( PICA_DATA_BLEND_EQUATION_ADD ), eqAlpha ( PICA_DATA_BLEND_EQUATION_ADD ), srcRgb ( PICA_DATA_BLEND_FUNC_SRC_ALPHA ), srcAlpha( PICA_DATA_BLEND_FUNC_SRC_ALPHA ), dstRgb ( PICA_DATA_BLEND_FUNC_ONE_MINUS_SRC_ALPHA ), dstAlpha( PICA_DATA_BLEND_FUNC_ONE_MINUS_SRC_ALPHA ), colorR( 0xff), colorG( 0xff), colorB( 0xff), colorA( 0xff), m_RenderState( renderState_ ) { } //------------------------------------------------------------------------------ RenderState::LogicOp::LogicOp( const RenderState& renderState_ ) : isEnable( false ), opCode( PICA_DATA_LOGIC_NOOP ), m_RenderState( renderState_ ) { } //------------------------------------------------------------------------------ RenderState::AlphaTest::AlphaTest( const RenderState& renderState_ ) : isEnable( false ), refValue( 0 ), func ( PICA_DATA_ALPHA_TEST_NEVER ), m_RenderState( renderState_ ) { } //------------------------------------------------------------------------------ RenderState::StencilTest::StencilTest( const RenderState& renderState_ ) : isEnable ( false ), isEnableWrite( false ), func ( PICA_DATA_STENCIL_TEST_ALWAYS ), ref ( 0 ), mask ( 0xff ), opFail ( PICA_DATA_STENCIL_OP_KEEP ), opZFail ( PICA_DATA_STENCIL_OP_KEEP ), opZPass ( PICA_DATA_STENCIL_OP_KEEP ), m_RenderState ( renderState_ ) { } //------------------------------------------------------------------------------ RenderState::DepthTest::DepthTest( const RenderState& renderState_ ) : isEnable( true ), isEnableWrite( true ), func( PICA_DATA_DEPTH_TEST_LESS ), m_RenderState( renderState_ ) { } //------------------------------------------------------------------------------ RenderState::WBuffer::WBuffer( const RenderState& renderState_ ) : wScale( 0.f ), isEnablePolygonOffset( false ), polygonOffsetUnit ( 0.f ), depthRangeNear ( 0.0f ), depthRangeFar ( 1.0f ), depthRangeBit ( 24 ), m_RenderState ( renderState_ ) { } //------------------------------------------------------------------------------ RenderState::FBAccess::FBAccess( const RenderState& renderState_ ) : m_RenderState( renderState_ ) { } //------------------------------------------------------------------------------ RenderState::RenderState() : blend ( *this ), logicOp ( *this ), alphaTest ( *this ), stencilTest ( *this ), colorMask ( COLOR_MASK_RGBA ), depthTest ( *this ), cullingTest ( *this ), wBuffer ( *this ), fbAccess ( *this ) { } } // namespace CTR } // namespace gr } // namespace nn