/// Initialize all resources provided by this manager. /// /// @see Cleanup(), PostConfigUpdate() bool RenderResourceManager::Initialize() { // Release any existing resources. Cleanup(); // Get the renderer and graphics configuration. Renderer* pRenderer = Renderer::GetInstance(); if ( !pRenderer ) { return false; } Config* pConfig = Config::GetInstance(); if ( !HELIUM_VERIFY( pConfig ) ) { return false; } StrongPtr< GraphicsConfig > spGraphicsConfig( pConfig->GetConfigObject< GraphicsConfig >( Name( "GraphicsConfig" ) ) ); if ( !spGraphicsConfig ) { HELIUM_TRACE( TraceLevels::Error, "RenderResourceManager::Initialize(): Initialization failed; missing GraphicsConfig.\n" ); return false; } // Create the standard rasterizer states. RRasterizerState::Description rasterizerStateDesc; rasterizerStateDesc.fillMode = RENDERER_FILL_MODE_SOLID; rasterizerStateDesc.cullMode = RENDERER_CULL_MODE_BACK; rasterizerStateDesc.winding = RENDERER_WINDING_CLOCKWISE; rasterizerStateDesc.depthBias = 0; rasterizerStateDesc.slopeScaledDepthBias = 0.0f; m_rasterizerStates[RASTERIZER_STATE_DEFAULT] = pRenderer->CreateRasterizerState( rasterizerStateDesc ); HELIUM_ASSERT( m_rasterizerStates[RASTERIZER_STATE_DEFAULT] ); rasterizerStateDesc.cullMode = RENDERER_CULL_MODE_NONE; m_rasterizerStates[RASTERIZER_STATE_DOUBLE_SIDED] = pRenderer->CreateRasterizerState( rasterizerStateDesc ); HELIUM_ASSERT( m_rasterizerStates[RASTERIZER_STATE_DOUBLE_SIDED] ); rasterizerStateDesc.depthBias = 1; rasterizerStateDesc.slopeScaledDepthBias = 2.0f; m_rasterizerStates[RASTERIZER_STATE_SHADOW_DEPTH] = pRenderer->CreateRasterizerState( rasterizerStateDesc ); HELIUM_ASSERT( m_rasterizerStates[RASTERIZER_STATE_SHADOW_DEPTH] ); rasterizerStateDesc.depthBias = 0; rasterizerStateDesc.slopeScaledDepthBias = 0.0f; rasterizerStateDesc.fillMode = RENDERER_FILL_MODE_WIREFRAME; m_rasterizerStates[RASTERIZER_STATE_WIREFRAME_DOUBLE_SIDED] = pRenderer->CreateRasterizerState( rasterizerStateDesc ); HELIUM_ASSERT( m_rasterizerStates[RASTERIZER_STATE_WIREFRAME_DOUBLE_SIDED] ); rasterizerStateDesc.cullMode = RENDERER_CULL_MODE_BACK; m_rasterizerStates[RASTERIZER_STATE_WIREFRAME] = pRenderer->CreateRasterizerState( rasterizerStateDesc ); HELIUM_ASSERT( m_rasterizerStates[RASTERIZER_STATE_WIREFRAME] ); // Create the standard blend states. RBlendState::Description blendStateDesc; blendStateDesc.bBlendEnable = false; m_blendStates[BLEND_STATE_OPAQUE] = pRenderer->CreateBlendState( blendStateDesc ); HELIUM_ASSERT( m_blendStates[BLEND_STATE_OPAQUE] ); blendStateDesc.colorWriteMask = 0; m_blendStates[BLEND_STATE_NO_COLOR] = pRenderer->CreateBlendState( blendStateDesc ); HELIUM_ASSERT( m_blendStates[BLEND_STATE_NO_COLOR] ); blendStateDesc.colorWriteMask = RENDERER_COLOR_WRITE_MASK_FLAG_ALL; blendStateDesc.bBlendEnable = true; blendStateDesc.sourceFactor = RENDERER_BLEND_FACTOR_SRC_ALPHA; blendStateDesc.destinationFactor = RENDERER_BLEND_FACTOR_INV_SRC_ALPHA; blendStateDesc.function = RENDERER_BLEND_FUNCTION_ADD; m_blendStates[BLEND_STATE_TRANSPARENT] = pRenderer->CreateBlendState( blendStateDesc ); HELIUM_ASSERT( m_blendStates[BLEND_STATE_TRANSPARENT] ); blendStateDesc.sourceFactor = RENDERER_BLEND_FACTOR_ONE; blendStateDesc.destinationFactor = RENDERER_BLEND_FACTOR_ONE; m_blendStates[BLEND_STATE_ADDITIVE] = pRenderer->CreateBlendState( blendStateDesc ); HELIUM_ASSERT( m_blendStates[BLEND_STATE_ADDITIVE] ); blendStateDesc.function = RENDERER_BLEND_FUNCTION_REVERSE_SUBTRACT; m_blendStates[BLEND_STATE_SUBTRACTIVE] = pRenderer->CreateBlendState( blendStateDesc ); HELIUM_ASSERT( m_blendStates[BLEND_STATE_SUBTRACTIVE] ); blendStateDesc.sourceFactor = RENDERER_BLEND_FACTOR_DEST_COLOR; blendStateDesc.destinationFactor = RENDERER_BLEND_FACTOR_ZERO; blendStateDesc.function = RENDERER_BLEND_FUNCTION_ADD; m_blendStates[BLEND_STATE_MODULATE] = pRenderer->CreateBlendState( blendStateDesc ); HELIUM_ASSERT( m_blendStates[BLEND_STATE_MODULATE] ); // Create the standard depth/stencil states. RDepthStencilState::Description depthStateDesc; depthStateDesc.stencilWriteMask = 0; depthStateDesc.bStencilTestEnable = false; depthStateDesc.depthFunction = RENDERER_COMPARE_FUNCTION_LESS_EQUAL; depthStateDesc.bDepthTestEnable = true; depthStateDesc.bDepthWriteEnable = true; m_depthStencilStates[DEPTH_STENCIL_STATE_DEFAULT] = pRenderer->CreateDepthStencilState( depthStateDesc ); HELIUM_ASSERT( m_depthStencilStates[DEPTH_STENCIL_STATE_DEFAULT] ); depthStateDesc.bDepthWriteEnable = false; m_depthStencilStates[DEPTH_STENCIL_STATE_TEST_ONLY] = pRenderer->CreateDepthStencilState( depthStateDesc ); HELIUM_ASSERT( m_depthStencilStates[DEPTH_STENCIL_STATE_TEST_ONLY] ); depthStateDesc.bDepthTestEnable = false; m_depthStencilStates[DEPTH_STENCIL_STATE_NONE] = pRenderer->CreateDepthStencilState( depthStateDesc ); HELIUM_ASSERT( m_depthStencilStates[DEPTH_STENCIL_STATE_NONE] ); // Create the standard sampler states that are not dependent on configuration settings. RSamplerState::Description samplerStateDesc; samplerStateDesc.filter = RENDERER_TEXTURE_FILTER_MIN_POINT_MAG_POINT_MIP_POINT; samplerStateDesc.addressModeW = RENDERER_TEXTURE_ADDRESS_MODE_CLAMP; samplerStateDesc.mipLodBias = 0; samplerStateDesc.maxAnisotropy = spGraphicsConfig->GetMaxAnisotropy(); for ( size_t addressModeIndex = 0; addressModeIndex < RENDERER_TEXTURE_ADDRESS_MODE_MAX; ++addressModeIndex ) { ERendererTextureAddressMode addressMode = static_cast<ERendererTextureAddressMode>( addressModeIndex ); samplerStateDesc.addressModeU = addressMode; samplerStateDesc.addressModeV = addressMode; samplerStateDesc.addressModeW = addressMode; m_samplerStates[TEXTURE_FILTER_POINT][addressModeIndex] = pRenderer->CreateSamplerState( samplerStateDesc ); HELIUM_ASSERT( m_samplerStates[TEXTURE_FILTER_POINT][addressModeIndex] ); } // Create the standard set of mesh vertex descriptions. RVertexDescription::Element vertexElements[6]; vertexElements[0].type = RENDERER_VERTEX_DATA_TYPE_FLOAT32_3; vertexElements[0].semantic = RENDERER_VERTEX_SEMANTIC_POSITION; vertexElements[0].semanticIndex = 0; vertexElements[0].bufferIndex = 0; vertexElements[1].type = RENDERER_VERTEX_DATA_TYPE_UINT8_4_NORM; vertexElements[1].semantic = RENDERER_VERTEX_SEMANTIC_COLOR; vertexElements[1].semanticIndex = 0; vertexElements[1].bufferIndex = 0; vertexElements[2].type = RENDERER_VERTEX_DATA_TYPE_FLOAT16_2; vertexElements[2].semantic = RENDERER_VERTEX_SEMANTIC_TEXCOORD; vertexElements[2].semanticIndex = 0; vertexElements[2].bufferIndex = 0; vertexElements[3].type = RENDERER_VERTEX_DATA_TYPE_FLOAT32_2; vertexElements[3].semantic = RENDERER_VERTEX_SEMANTIC_TEXCOORD; vertexElements[3].semanticIndex = 1; vertexElements[3].bufferIndex = 0; m_spSimpleVertexDescription = pRenderer->CreateVertexDescription( vertexElements, 2 ); HELIUM_ASSERT( m_spSimpleVertexDescription ); m_spSimpleTexturedVertexDescription = pRenderer->CreateVertexDescription( vertexElements, 3 ); HELIUM_ASSERT( m_spSimpleTexturedVertexDescription ); m_spProjectedVertexDescription = pRenderer->CreateVertexDescription( vertexElements, 4 ); HELIUM_ASSERT( m_spProjectedVertexDescription ); vertexElements[1].type = RENDERER_VERTEX_DATA_TYPE_UINT8_4_NORM; vertexElements[1].semantic = RENDERER_VERTEX_SEMANTIC_NORMAL; vertexElements[1].semanticIndex = 0; vertexElements[1].bufferIndex = 0; vertexElements[2].type = RENDERER_VERTEX_DATA_TYPE_UINT8_4_NORM; vertexElements[2].semantic = RENDERER_VERTEX_SEMANTIC_TANGENT; vertexElements[2].semanticIndex = 0; vertexElements[2].bufferIndex = 0; vertexElements[3].type = RENDERER_VERTEX_DATA_TYPE_UINT8_4_NORM; vertexElements[3].semantic = RENDERER_VERTEX_SEMANTIC_COLOR; vertexElements[3].semanticIndex = 0; vertexElements[3].bufferIndex = 0; vertexElements[4].type = RENDERER_VERTEX_DATA_TYPE_FLOAT16_2; vertexElements[4].semantic = RENDERER_VERTEX_SEMANTIC_TEXCOORD; vertexElements[4].semanticIndex = 0; vertexElements[4].bufferIndex = 0; vertexElements[5].type = RENDERER_VERTEX_DATA_TYPE_FLOAT16_2; vertexElements[5].semantic = RENDERER_VERTEX_SEMANTIC_TEXCOORD; vertexElements[5].semanticIndex = 1; vertexElements[5].bufferIndex = 0; m_staticMeshVertexDescriptions[0] = pRenderer->CreateVertexDescription( vertexElements, 5 ); HELIUM_ASSERT( m_staticMeshVertexDescriptions[0] ); m_staticMeshVertexDescriptions[1] = pRenderer->CreateVertexDescription( vertexElements, 6 ); HELIUM_ASSERT( m_staticMeshVertexDescriptions[1] ); vertexElements[1].type = RENDERER_VERTEX_DATA_TYPE_UINT8_4_NORM; vertexElements[1].semantic = RENDERER_VERTEX_SEMANTIC_BLENDWEIGHT; vertexElements[1].semanticIndex = 0; vertexElements[1].bufferIndex = 0; vertexElements[2].type = RENDERER_VERTEX_DATA_TYPE_UINT8_4; vertexElements[2].semantic = RENDERER_VERTEX_SEMANTIC_BLENDINDICES; vertexElements[2].semanticIndex = 0; vertexElements[2].bufferIndex = 0; vertexElements[3].type = RENDERER_VERTEX_DATA_TYPE_UINT8_4_NORM; vertexElements[3].semantic = RENDERER_VERTEX_SEMANTIC_NORMAL; vertexElements[3].semanticIndex = 0; vertexElements[3].bufferIndex = 0; vertexElements[4].type = RENDERER_VERTEX_DATA_TYPE_UINT8_4_NORM; vertexElements[4].semantic = RENDERER_VERTEX_SEMANTIC_TANGENT; vertexElements[4].semanticIndex = 0; vertexElements[4].bufferIndex = 0; vertexElements[5].type = RENDERER_VERTEX_DATA_TYPE_FLOAT16_2; vertexElements[5].semantic = RENDERER_VERTEX_SEMANTIC_TEXCOORD; vertexElements[5].semanticIndex = 0; vertexElements[5].bufferIndex = 0; m_spSkinnedMeshVertexDescription = pRenderer->CreateVertexDescription( vertexElements, 6 ); HELIUM_ASSERT( m_spSkinnedMeshVertexDescription ); vertexElements[0].type = RENDERER_VERTEX_DATA_TYPE_FLOAT32_2; vertexElements[0].semantic = RENDERER_VERTEX_SEMANTIC_POSITION; vertexElements[0].semanticIndex = 0; vertexElements[0].bufferIndex = 0; vertexElements[1].type = RENDERER_VERTEX_DATA_TYPE_UINT8_4_NORM; vertexElements[1].semantic = RENDERER_VERTEX_SEMANTIC_COLOR; vertexElements[1].semanticIndex = 0; vertexElements[1].bufferIndex = 0; vertexElements[2].type = RENDERER_VERTEX_DATA_TYPE_FLOAT16_2; vertexElements[2].semantic = RENDERER_VERTEX_SEMANTIC_TEXCOORD; vertexElements[2].semanticIndex = 0; vertexElements[2].bufferIndex = 0; m_spScreenVertexDescription = pRenderer->CreateVertexDescription( vertexElements, 3 ); HELIUM_ASSERT( m_spScreenVertexDescription ); // Create configuration-dependent render resources. PostConfigUpdate(); // Attempt to load the depth-only pre-pass shader. // TODO: XXX TMC: Migrate to a more data-driven solution. AssetLoader* pAssetLoader = AssetLoader::GetInstance(); HELIUM_ASSERT( pAssetLoader ); #ifdef HELIUM_DIRECT3D AssetPath prePassShaderPath; HELIUM_VERIFY( prePassShaderPath.Set( HELIUM_PACKAGE_PATH_CHAR_STRING "Shaders" HELIUM_OBJECT_PATH_CHAR_STRING "PrePass.hlsl" ) ); AssetPtr spPrePassShader; HELIUM_VERIFY( pAssetLoader->LoadObject( prePassShaderPath, spPrePassShader ) ); Shader* pPrePassShader = Reflect::SafeCast< Shader >( spPrePassShader.Get() ); if ( HELIUM_VERIFY( pPrePassShader ) ) { size_t loadId = pPrePassShader->BeginLoadVariant( RShader::TYPE_VERTEX, 0 ); HELIUM_ASSERT( IsValid( loadId ) ); if ( IsValid( loadId ) ) { while ( !pPrePassShader->TryFinishLoadVariant( loadId, m_spPrePassVertexShader ) ) { pAssetLoader->Tick(); } } } // Attempt to load the simple world-space, simple screen-space, and screen-space text shaders. // TODO: XXX TMC: Migrate to a more data-driven solution. AssetPath shaderPath; HELIUM_VERIFY( shaderPath.Set( HELIUM_PACKAGE_PATH_CHAR_STRING "Shaders" HELIUM_OBJECT_PATH_CHAR_STRING "Simple.hlsl" ) ); AssetPtr spShader; HELIUM_VERIFY( pAssetLoader->LoadObject( shaderPath, spShader ) ); Shader* pShader = Reflect::SafeCast< Shader >( spShader.Get() ); if ( HELIUM_VERIFY( pShader ) ) { size_t loadId = pShader->BeginLoadVariant( RShader::TYPE_VERTEX, 0 ); HELIUM_ASSERT( IsValid( loadId ) ); if ( IsValid( loadId ) ) { while ( !pShader->TryFinishLoadVariant( loadId, m_spSimpleWorldSpaceVertexShader ) ) { pAssetLoader->Tick(); } } loadId = pShader->BeginLoadVariant( RShader::TYPE_PIXEL, 0 ); HELIUM_ASSERT( IsValid( loadId ) ); if ( IsValid( loadId ) ) { while ( !pShader->TryFinishLoadVariant( loadId, m_spSimpleWorldSpacePixelShader ) ) { pAssetLoader->Tick(); } } } HELIUM_VERIFY( shaderPath.Set( HELIUM_PACKAGE_PATH_CHAR_STRING "Shaders" HELIUM_OBJECT_PATH_CHAR_STRING "ScreenSpaceTexture.hlsl" ) ); HELIUM_VERIFY( pAssetLoader->LoadObject( shaderPath, spShader ) ); pShader = Reflect::SafeCast< Shader >( spShader.Get() ); if ( HELIUM_VERIFY( pShader ) ) { size_t loadId = pShader->BeginLoadVariant( RShader::TYPE_VERTEX, 0 ); HELIUM_ASSERT( IsValid( loadId ) ); if ( IsValid( loadId ) ) { while ( !pShader->TryFinishLoadVariant( loadId, m_spSimpleScreenSpaceVertexShader ) ) { pAssetLoader->Tick(); } } loadId = pShader->BeginLoadVariant( RShader::TYPE_PIXEL, 0 ); HELIUM_ASSERT( IsValid( loadId ) ); if ( IsValid( loadId ) ) { while ( !pShader->TryFinishLoadVariant( loadId, m_spSimpleScreenSpacePixelShader ) ) { pAssetLoader->Tick(); } } } HELIUM_VERIFY( shaderPath.Set( HELIUM_PACKAGE_PATH_CHAR_STRING "Shaders" HELIUM_OBJECT_PATH_CHAR_STRING "ScreenText.hlsl" ) ); HELIUM_VERIFY( pAssetLoader->LoadObject( shaderPath, spShader ) ); pShader = Reflect::SafeCast< Shader >( spShader.Get() ); if ( HELIUM_VERIFY( pShader ) ) { size_t loadId = pShader->BeginLoadVariant( RShader::TYPE_VERTEX, 0 ); HELIUM_ASSERT( IsValid( loadId ) ); if ( IsValid( loadId ) ) { while ( !pShader->TryFinishLoadVariant( loadId, m_spScreenTextVertexShader ) ) { pAssetLoader->Tick(); } } loadId = pShader->BeginLoadVariant( RShader::TYPE_PIXEL, 0 ); HELIUM_ASSERT( IsValid( loadId ) ); if ( IsValid( loadId ) ) { while ( !pShader->TryFinishLoadVariant( loadId, m_spScreenTextPixelShader ) ) { pAssetLoader->Tick(); } } } // Attempt to load the debug fonts. // TODO: XXX TMC: Migrate to a more data-driven solution. AssetPath fontPath; AssetPtr spFont; HELIUM_VERIFY( fontPath.Set( HELIUM_PACKAGE_PATH_CHAR_STRING "Fonts" HELIUM_OBJECT_PATH_CHAR_STRING "DebugSmall" ) ); HELIUM_VERIFY( pAssetLoader->LoadObject( fontPath, spFont ) ); m_debugFonts[DEBUG_FONT_SIZE_SMALL] = Reflect::SafeCast< Font >( spFont.Get() ); spFont.Release(); HELIUM_VERIFY( fontPath.Set( HELIUM_PACKAGE_PATH_CHAR_STRING "Fonts" HELIUM_OBJECT_PATH_CHAR_STRING "DebugMedium" ) ); HELIUM_VERIFY( pAssetLoader->LoadObject( fontPath, spFont ) ); m_debugFonts[DEBUG_FONT_SIZE_MEDIUM] = Reflect::SafeCast< Font >( spFont.Get() ); spFont.Release(); HELIUM_VERIFY( fontPath.Set( HELIUM_PACKAGE_PATH_CHAR_STRING "Fonts" HELIUM_OBJECT_PATH_CHAR_STRING "DebugLarge" ) ); HELIUM_VERIFY( pAssetLoader->LoadObject( fontPath, spFont ) ); m_debugFonts[DEBUG_FONT_SIZE_LARGE] = Reflect::SafeCast< Font >( spFont.Get() ); spFont.Release(); #endif return true; }
/// Create a new object. /// /// @param[out] rspObject Pointer to the newly created object if object creation was successful. Note that /// any object reference stored in this strong pointer prior to calling this function /// will always be cleared by this function, regardless of whether object creation is /// successful. /// @param[in] pType Type of object to create. /// @param[in] name Object name. /// @param[in] pOwner Object owner. /// @param[in] pTemplate Optional override template object. If null, the default template for the /// specified type will be used. /// @param[in] bAssignInstanceIndex True to assign an instance index to the object, false to leave the index /// invalid. /// /// @return True if object creation was successful, false if not. /// /// @see Create() bool Asset::CreateObject( AssetPtr& rspObject, const AssetType* pType, Name name, Asset* pOwner, Asset* pTemplate, bool bAssignInstanceIndex ) { HELIUM_ASSERT( pType ); HELIUM_TRACE( TraceLevels::Debug, TXT( "Asset::CreateObject(): Creating object named \"%s\" of type \"%s\" owned by \"%s\".\n"), *name, *pType->GetName(), !pOwner ? TXT("[none]") : *pOwner->GetPath().ToString()); rspObject.Release(); // Get the appropriate template object. Asset* pObjectTemplate = pTemplate; if( pObjectTemplate ) { if( pType->GetFlags() & AssetType::FLAG_NO_TEMPLATE && pType->GetTemplate() != pObjectTemplate ) { HELIUM_TRACE( TraceLevels::Error, TXT( "Asset::CreateObject(): Objects of type \"%s\" cannot be used as templates.\n" ), *pType->GetName() ); return false; } } else { pObjectTemplate = pType->GetTemplate(); HELIUM_ASSERT( pObjectTemplate ); } // Make sure the object template is of the correct type. if( !pObjectTemplate->IsInstanceOf( pType ) ) { HELIUM_TRACE( TraceLevels::Error, TXT( "Asset::CreateObject: Template object \"%s\" is not of type \"%s\".\n" ), *pTemplate->GetPath().ToString(), pType->GetName().Get() ); HELIUM_BREAK(); return false; } // Allocate memory for and create the object. DefaultAllocator allocator; size_t bufferSize = pObjectTemplate->GetInstanceSize(); void* pObjectMemory = allocator.AllocateAligned( HELIUM_SIMD_ALIGNMENT, bufferSize ); HELIUM_ASSERT( pObjectMemory ); Asset* pObject = pObjectTemplate->InPlaceConstruct( pObjectMemory, StandardCustomDestroy ); HELIUM_ASSERT( pObject == pObjectMemory ); rspObject = pObject; // Initialize the object based on its default. pObjectTemplate->CopyTo(pObject); pObject->m_spTemplate = pTemplate; // Attempt to register the object and set its name. RenameParameters nameParameters; nameParameters.name = name; nameParameters.spOwner = pOwner; if( bAssignInstanceIndex ) { nameParameters.instanceIndex = INSTANCE_INDEX_AUTO; } if ( !RegisterObject( pObject ) ) { HELIUM_TRACE( TraceLevels::Error, TXT( "Asset::CreateObject(): RegisterObject() failed for Asset \"%s\" owned by \"%s\".\n" ), *name, !pOwner ? TXT("[none]") : *pOwner->GetPath().ToString()); HELIUM_BREAK(); rspObject.Release(); return false; } if( !pObject->Rename( nameParameters ) ) { HELIUM_TRACE( TraceLevels::Error, TXT( "Asset::CreateObject(): Rename() failed for Asset \"%s\" owned by \"%s\".\n" ), *name, !pOwner ? TXT("[none]") : *pOwner->GetPath().ToString()); HELIUM_BREAK(); rspObject.Release(); return false; } return true; }