Reflect::ObjectPtr Helium::Cache::ReadCacheObjectFromBuffer( const DynArray< uint8_t > &_buffer ) { if (_buffer.GetSize() == 0) { Reflect::ObjectPtr null_object; return null_object; } return ReadCacheObjectFromBuffer(_buffer.GetData(), 0, _buffer.GetSize()); }
void SimpleObjectSortedMapData< KeyT, CompareKeyT, AllocatorT >::Deserialize( ArchiveT& archive ) { DynArray< ObjectPtr > components; archive.DeserializeArray( components, ArchiveFlags::Sparse ); size_t componentCount = components.GetSize(); if ( componentCount % 2 != 0 ) { throw Reflect::DataFormatException( TXT( "Unmatched map objects" ) ); } // if we are referring to a real field, clear its contents m_Data->Clear(); m_Data->Reserve( componentCount / 2 ); DynArray< ObjectPtr >::Iterator itr = components.Begin(); DynArray< ObjectPtr >::Iterator end = components.End(); while ( itr != end ) { Data* key = SafeCast< Data >( *itr ); ++itr; Object* value = *itr; ++itr; if ( key ) // The object value can be null, so don't check it here. { KeyT k; Data::GetValue( key, k ); (*m_Data)[ k ] = value; } } }
void StructureDynArrayData::Deserialize( ArchiveT& archive ) { const Structure *structure = GetInternalStructure(); DynArray< ObjectPtr > components; archive.DeserializeArray(components); // if we are referring to a real field, clear its contents structure->m_DynArrayAdapter->Resize(GetInternalPtr(structure), components.GetSize()); for (size_t i = 0; i < components.GetSize(); ++i) { Data* data = SafeCast<Data>(components[i]); structure->m_DynArrayAdapter->SetItem(GetInternalPtr(structure), i, data, m_Instance, m_Field); } }
/// Constructor. /// /// @param[in] rBuffer Buffer from which the data should be de-serialized. /// @param[in] startOffset Byte offset within the buffer from which to start de-serialization. DirectDeserializer::DirectDeserializer( const DynArray< uint8_t >& rBuffer, size_t startOffset ) : m_rBuffer( rBuffer ) { // Check if we have attempted to start past the end of the stream. size_t bufferSize = rBuffer.GetSize(); HELIUM_ASSERT( startOffset <= bufferSize ); m_bEndOfStream = ( startOffset > bufferSize ); if( m_bEndOfStream ) { startOffset = bufferSize; } m_offset = startOffset; }
void StructureDynArrayData::Serialize( ArchiveT& archive ) { const Structure *structure = GetInternalStructure(); DynArray< ObjectPtr > components; components.Resize( structure->m_DynArrayAdapter->GetSize(GetInternalPtr(structure)) ); for (size_t i = 0; i < components.GetSize(); ++i) { components[i] = structure->m_DynArrayAdapter->GetItem(GetInternalPtr(structure), i, m_Instance, m_Field); } archive.SerializeArray( components ); }
void SimpleSortedSetData< KeyT, CompareKeyT, AllocatorT >::Deserialize( ArchiveT& archive ) { DynArray< ObjectPtr > components; archive.DeserializeArray( components ); // if we are referring to a real field, clear its contents m_Data->Clear(); m_Data->Reserve( components.GetSize() ); DynArray< ObjectPtr >::Iterator itr = components.Begin(); DynArray< ObjectPtr >::Iterator end = components.End(); for ( ; itr != end; ++itr ) { Data* data = SafeCast< Data >( *itr ); if ( !data ) { throw LogisticException( TXT( "SortedSet value type has changed, this is unpossible" ) ); } KeyT k; Data::GetValue( data, k ); m_Data->Insert( k ); } }
/// @copydoc ResourceHandler::CacheResource() bool FontResourceHandler::CacheResource( ObjectPreprocessor* pObjectPreprocessor, Resource* pResource, const String& rSourceFilePath ) { HELIUM_ASSERT( pObjectPreprocessor ); HELIUM_ASSERT( pResource ); Font* pFont = Reflect::AssertCast< Font >( pResource ); // Load the font into memory ourselves in order to make sure we properly support Unicode file names. FileStream* pFileStream = File::Open( rSourceFilePath, FileStream::MODE_READ ); if( !pFileStream ) { HELIUM_TRACE( TRACE_ERROR, TXT( "FontResourceHandler: Source file for font resource \"%s\" failed to open properly.\n" ), *rSourceFilePath ); return false; } uint64_t fileSize64 = static_cast< uint64_t >( pFileStream->GetSize() ); if( fileSize64 > SIZE_MAX ) { HELIUM_TRACE( TRACE_ERROR, ( TXT( "FontResourceHandler: Font file \"%s\" exceeds the maximum addressable size of data in memory for " ) TXT( "this platform and will not be cached.\n" ) ), *rSourceFilePath ); delete pFileStream; return false; } size_t fileSize = static_cast< size_t >( fileSize64 ); uint8_t* pFileData = new uint8_t [ fileSize ]; if( !pFileData ) { HELIUM_TRACE( TRACE_ERROR, ( TXT( "FontResourceHandler: Failed to allocate %" ) TPRIuSZ TXT( " bytes for resource data for font " ) TXT( "\"%s\".\n" ) ), fileSize, *rSourceFilePath ); delete pFileStream; return false; } size_t bytesRead = pFileStream->Read( pFileData, 1, fileSize ); delete pFileStream; if( bytesRead != fileSize ) { HELIUM_TRACE( TRACE_WARNING, ( TXT( "FontResourceHandler: Attempted to read %" ) TPRIuSZ TXT( " bytes from font resource file \"%s\", " ) TXT( "but only %" ) TPRIuSZ TXT( " bytes were read successfully.\n" ) ), fileSize, *rSourceFilePath, bytesRead ); } // Create the font face. FT_Library pLibrary = GetStaticLibrary(); HELIUM_ASSERT( pLibrary ); FT_Face pFace = NULL; FT_Error error = FT_New_Memory_Face( pLibrary, pFileData, static_cast< FT_Long >( bytesRead ), 0, &pFace ); if( error != 0 ) { HELIUM_TRACE( TRACE_ERROR, TXT( "FontResourceHandler: Failed to create font face from resource file \"%s\".\n" ), *rSourceFilePath ); delete [] pFileData; return false; } // Set the appropriate font size. int32_t pointSize = Font::Float32ToFixed26x6( pFont->GetPointSize() ); uint32_t dpi = pFont->GetDpi(); error = FT_Set_Char_Size( pFace, pointSize, pointSize, dpi, dpi ); if( error != 0 ) { HELIUM_TRACE( TRACE_ERROR, TXT( "FontResourceHandler: Failed to set size of font resource \"%s\".\n" ), *rSourceFilePath ); FT_Done_Face( pFace ); delete [] pFileData; return false; } // Get the general font size information. FT_Size pSize = pFace->size; HELIUM_ASSERT( pSize ); int32_t ascender = pSize->metrics.ascender; int32_t descender = pSize->metrics.descender; int32_t height = pSize->metrics.height; int32_t maxAdvance = pSize->metrics.max_advance; // Make sure that all characters in the font will fit on a single texture sheet (note that we also need at least a // pixel on each side in order to pad each glyph). uint16_t textureSheetWidth = Max< uint16_t >( pFont->GetTextureSheetWidth(), 1 ); uint16_t textureSheetHeight = Max< uint16_t >( pFont->GetTextureSheetHeight(), 1 ); int32_t integerHeight = ( height + ( 1 << 6 ) - 1 ) >> 6; if( integerHeight + 2 > textureSheetHeight ) { HELIUM_TRACE( TRACE_ERROR, ( TXT( "FontResourceHandler: Font height (%" ) TPRId32 TXT( ") exceeds the texture sheet height (%" ) TPRIu16 TXT( ") for font resource \"%s\".\n" ) ), integerHeight, textureSheetHeight, *pResource->GetPath().ToString() ); FT_Done_Face( pFace ); delete [] pFileData; return false; } int32_t integerMaxAdvance = ( maxAdvance + ( 1 << 6 ) - 1 ) >> 6; if( integerMaxAdvance + 2 > textureSheetWidth ) { HELIUM_TRACE( TRACE_ERROR, ( TXT( "FontResourceHandler: Maximum character advance (%" ) TPRId32 TXT( ") exceeds the texture sheet " ) TXT( "width (%" ) TPRIu16 TXT( ") for font resource \"%s\".\n" ) ), integerMaxAdvance, textureSheetWidth, *pResource->GetPath().ToString() ); FT_Done_Face( pFace ); delete [] pFileData; return false; } // Allocate a buffer for building our texture sheets. uint_fast32_t texturePixelCount = static_cast< uint_fast32_t >( textureSheetWidth ) * static_cast< uint_fast32_t >( textureSheetHeight ); uint8_t* pTextureBuffer = new uint8_t [ texturePixelCount ]; HELIUM_ASSERT( pTextureBuffer ); if( !pTextureBuffer ) { HELIUM_TRACE( TRACE_ERROR, ( TXT( "FontResourceHandler: Failed to allocate %" ) TPRIuFAST32 TXT( " bytes for texture resource " ) TXT( "buffer data while caching font resource \"%s\".\n" ) ), texturePixelCount, *pResource->GetPath().ToString() ); FT_Done_Face( pFace ); delete [] pFileData; return false; } MemoryZero( pTextureBuffer, texturePixelCount ); // Build the texture sheets for our glyphs. Font::ECompression textureCompression = pFont->GetTextureCompression(); bool bAntialiased = pFont->GetAntialiased(); DynArray< DynArray< uint8_t > > textureSheets; DynArray< Font::Character > characters; uint16_t penX = 1; uint16_t penY = 1; uint16_t lineHeight = 0; FT_Int32 glyphLoadFlags = FT_LOAD_RENDER; if( !bAntialiased ) { glyphLoadFlags |= FT_LOAD_TARGET_MONO; } for( uint_fast32_t codePoint = 0; codePoint <= UNICODE_CODE_POINT_MAX; ++codePoint ) { // Check whether the current code point is contained within the font. FT_UInt characterIndex = FT_Get_Char_Index( pFace, static_cast< FT_ULong >( codePoint ) ); if( characterIndex == 0 ) { continue; } // Load and render the glyph for the current character. HELIUM_VERIFY( FT_Load_Glyph( pFace, characterIndex, glyphLoadFlags ) == 0 ); FT_GlyphSlot pGlyph = pFace->glyph; HELIUM_ASSERT( pGlyph ); // Proceed to the next line in the texture sheet or the next sheet itself if we don't have enough room in the // current line/sheet. HELIUM_ASSERT( pGlyph->bitmap.rows >= 0 ); HELIUM_ASSERT( pGlyph->bitmap.width >= 0 ); uint_fast32_t glyphRowCount = static_cast< uint32_t >( pGlyph->bitmap.rows ); uint_fast32_t glyphWidth = static_cast< uint32_t >( pGlyph->bitmap.width ); if( penX + glyphWidth + 1 >= textureSheetWidth ) { penX = 1; if( penY + glyphRowCount + 1 >= textureSheetHeight ) { CompressTexture( pTextureBuffer, textureSheetWidth, textureSheetHeight, textureCompression, textureSheets ); MemoryZero( pTextureBuffer, texturePixelCount ); penY = 1; } else { penY += lineHeight + 1; } lineHeight = 0; } // Copy the character data from the glyph bitmap to the texture sheet. int_fast32_t glyphPitch = pGlyph->bitmap.pitch; const uint8_t* pGlyphBuffer = pGlyph->bitmap.buffer; HELIUM_ASSERT( pGlyphBuffer || glyphRowCount == 0 ); uint8_t* pTexturePixel = pTextureBuffer + static_cast< size_t >( penY ) * static_cast< size_t >( textureSheetWidth ) + penX; if( bAntialiased ) { // Anti-aliased fonts are rendered as 8-bit grayscale images, so just copy the data as-is. for( uint_fast32_t rowIndex = 0; rowIndex < glyphRowCount; ++rowIndex ) { MemoryCopy( pTexturePixel, pGlyphBuffer, glyphWidth ); pGlyphBuffer += glyphPitch; pTexturePixel += textureSheetWidth; } } else { // Fonts without anti-aliasing are rendered as 1-bit monochrome images, so we need to manually convert each // row to 8-bit grayscale. for( uint_fast32_t rowIndex = 0; rowIndex < glyphRowCount; ++rowIndex ) { const uint8_t* pGlyphPixelBlock = pGlyphBuffer; pGlyphBuffer += glyphPitch; uint8_t* pCurrentTexturePixel = pTexturePixel; pTexturePixel += textureSheetWidth; uint_fast32_t remainingPixelCount = glyphWidth; while( remainingPixelCount >= 8 ) { remainingPixelCount -= 8; uint8_t pixelBlock = *pGlyphPixelBlock; ++pGlyphPixelBlock; *( pCurrentTexturePixel++ ) = ( ( pixelBlock & ( 1 << 7 ) ) ? 255 : 0 ); *( pCurrentTexturePixel++ ) = ( ( pixelBlock & ( 1 << 6 ) ) ? 255 : 0 ); *( pCurrentTexturePixel++ ) = ( ( pixelBlock & ( 1 << 5 ) ) ? 255 : 0 ); *( pCurrentTexturePixel++ ) = ( ( pixelBlock & ( 1 << 4 ) ) ? 255 : 0 ); *( pCurrentTexturePixel++ ) = ( ( pixelBlock & ( 1 << 3 ) ) ? 255 : 0 ); *( pCurrentTexturePixel++ ) = ( ( pixelBlock & ( 1 << 2 ) ) ? 255 : 0 ); *( pCurrentTexturePixel++ ) = ( ( pixelBlock & ( 1 << 1 ) ) ? 255 : 0 ); *( pCurrentTexturePixel++ ) = ( ( pixelBlock & ( 1 << 0 ) ) ? 255 : 0 ); } uint8_t pixelBlock = *pGlyphPixelBlock; uint8_t mask = ( 1 << 7 ); while( remainingPixelCount != 0 ) { *( pCurrentTexturePixel++ ) = ( ( pixelBlock & mask ) ? 255 : 0 ); mask >>= 1; --remainingPixelCount; } } } // Store the character information in our character array. Font::Character* pCharacter = characters.New(); HELIUM_ASSERT( pCharacter ); pCharacter->codePoint = static_cast< uint32_t >( codePoint ); pCharacter->imageX = penX; pCharacter->imageY = penY; pCharacter->imageWidth = static_cast< uint16_t >( glyphWidth ); pCharacter->imageHeight = static_cast< uint16_t >( glyphRowCount ); pCharacter->width = pGlyph->metrics.width; pCharacter->height = pGlyph->metrics.height; pCharacter->bearingX = pGlyph->metrics.horiBearingX; pCharacter->bearingY = pGlyph->metrics.horiBearingY; pCharacter->advance = pGlyph->metrics.horiAdvance; HELIUM_ASSERT( textureSheets.GetSize() < UINT8_MAX ); pCharacter->texture = static_cast< uint8_t >( static_cast< uint8_t >( textureSheets.GetSize() ) ); // Update the pen location as well as the maximum line height as appropriate based on the current line height. penX += static_cast< uint16_t >( glyphWidth ) + 1; HELIUM_ASSERT( glyphRowCount <= UINT16_MAX ); lineHeight = Max< uint16_t >( lineHeight, static_cast< uint16_t >( glyphRowCount ) ); } // Compress and store the last texture in the sheet. if( !characters.IsEmpty() ) { CompressTexture( pTextureBuffer, textureSheetWidth, textureSheetHeight, textureCompression, textureSheets ); } // Done processing the font itself, so free some resources. delete [] pTextureBuffer; FT_Done_Face( pFace ); delete [] pFileData; // Cache the font data. size_t characterCountActual = characters.GetSize(); HELIUM_ASSERT( characterCountActual <= UINT32_MAX ); uint32_t characterCount = static_cast< uint32_t >( characterCountActual ); size_t textureCountActual = textureSheets.GetSize(); HELIUM_ASSERT( textureCountActual < UINT8_MAX ); uint8_t textureCount = static_cast< uint8_t >( textureCountActual ); BinarySerializer persistentDataSerializer; for( size_t platformIndex = 0; platformIndex < static_cast< size_t >( Cache::PLATFORM_MAX ); ++platformIndex ) { PlatformPreprocessor* pPreprocessor = pObjectPreprocessor->GetPlatformPreprocessor( static_cast< Cache::EPlatform >( platformIndex ) ); if( !pPreprocessor ) { continue; } persistentDataSerializer.SetByteSwapping( pPreprocessor->SwapBytes() ); persistentDataSerializer.BeginSerialize(); persistentDataSerializer << ascender; persistentDataSerializer << descender; persistentDataSerializer << height; persistentDataSerializer << maxAdvance; persistentDataSerializer << characterCount; persistentDataSerializer << textureCount; for( size_t characterIndex = 0; characterIndex < characterCountActual; ++characterIndex ) { characters[ characterIndex ].Serialize( persistentDataSerializer ); } persistentDataSerializer.EndSerialize(); Resource::PreprocessedData& rPreprocessedData = pResource->GetPreprocessedData( static_cast< Cache::EPlatform >( platformIndex ) ); rPreprocessedData.persistentDataBuffer = persistentDataSerializer.GetPropertyStreamBuffer(); rPreprocessedData.subDataBuffers = textureSheets; rPreprocessedData.bLoaded = true; } return true; }
/// Helper function for compiling a shader for a specific profile. /// /// @param[in] pVariant Shader variant for which we are compiling. /// @param[in] pPreprocessor Platform preprocessor to use for compiling. /// @param[in] platformIndex Platform index. /// @param[in] shaderProfileIndex Index of the target shader profile. /// @param[in] shaderType Type of shader to compile. /// @param[in] pShaderSourceData Buffer in which the shader source code is stored. /// @param[in] shaderSourceSize Size of the shader source buffer, in bytes. /// @param[in] rTokens Array specifying preprocessor tokens to pass to the shader compiler. /// @param[out] rCompiledCodeBuffer Buffer in which the compiled code will be stored. /// /// @return True if compiling was successful, false if not. bool ShaderVariantResourceHandler::CompileShader( ShaderVariant* pVariant, PlatformPreprocessor* pPreprocessor, size_t platformIndex, size_t shaderProfileIndex, RShader::EType shaderType, const void* pShaderSourceData, size_t shaderSourceSize, const DynArray< PlatformPreprocessor::ShaderToken >& rTokens, DynArray< uint8_t >& rCompiledCodeBuffer ) { HELIUM_ASSERT( pVariant ); HELIUM_ASSERT( pPreprocessor ); HELIUM_ASSERT( static_cast< size_t >( shaderType ) < static_cast< size_t >( RShader::TYPE_MAX ) ); HELIUM_ASSERT( pShaderSourceData || shaderSourceSize == 0 ); HELIUM_UNREF( platformIndex ); // Variable is used for logging only. rCompiledCodeBuffer.Resize( 0 ); #if HELIUM_ENABLE_TRACE DynArray< String > errorMessages; #endif Path shaderFilePath; if ( !File::GetDataDirectory( shaderFilePath ) ) { HELIUM_TRACE( TRACE_ERROR, TXT( "ShaderVariantResourceHandler: Failed to obtain data directory." ) ); return false; } shaderFilePath += pVariant->GetPath().GetParent().ToFilePathString().GetData(); bool bCompileResult = pPreprocessor->CompileShader( shaderFilePath, shaderProfileIndex, shaderType, pShaderSourceData, shaderSourceSize, rTokens.GetData(), rTokens.GetSize(), rCompiledCodeBuffer #if HELIUM_ENABLE_TRACE , &errorMessages #else , NULL #endif ); if( !bCompileResult ) { rCompiledCodeBuffer.Resize( 0 ); #if HELIUM_ENABLE_TRACE String tokenList; #if HELIUM_UNICODE String convertedToken; #endif size_t tokenCount = rTokens.GetSize(); for( size_t tokenIndex = 0; tokenIndex < tokenCount; ++tokenIndex ) { tokenList += TXT( ' ' ); #if HELIUM_UNICODE StringConverter< char, tchar_t >::Convert( convertedToken, rTokens[ tokenIndex ].name ); tokenList += convertedToken; #else tokenList += rTokens[ tokenIndex ].name; #endif } size_t errorCount = errorMessages.GetSize(); HELIUM_TRACE( TRACE_ERROR, ( TXT( "ShaderVariantResourceHandler: Failed to compile \"%s\" for platform %" ) TPRIuSZ TXT( ", profile %" ) TPRIuSZ TXT( "; %" ) TPRIuSZ TXT( " errors (tokens:%s):\n" ) ), *pVariant->GetPath().ToString(), platformIndex, shaderProfileIndex, errorCount, *tokenList ); for( size_t errorIndex = 0; errorIndex < errorCount; ++errorIndex ) { HELIUM_TRACE( TRACE_ERROR, TXT( "- %s\n" ), *errorMessages[ errorIndex ] ); } #endif // HELIUM_ENABLE_TRACE } return bCompileResult; }
/// @copydoc ResourceHandler::CacheResource() bool ShaderVariantResourceHandler::CacheResource( ObjectPreprocessor* pObjectPreprocessor, Resource* pResource, const String& rSourceFilePath ) { HELIUM_ASSERT( pObjectPreprocessor ); HELIUM_ASSERT( pResource ); ShaderVariant* pVariant = Reflect::AssertCast< ShaderVariant >( pResource ); // Parse the shader type and user option index from the variant name. Name variantName = pVariant->GetName(); const tchar_t* pVariantNameString = *variantName; HELIUM_ASSERT( pVariantNameString ); tchar_t shaderTypeCharacter = pVariantNameString[ 0 ]; HELIUM_ASSERT( shaderTypeCharacter != TXT( '\0' ) ); RShader::EType shaderType; switch( shaderTypeCharacter ) { case TXT( 'v' ): { shaderType = RShader::TYPE_VERTEX; break; } case TXT( 'p' ): { shaderType = RShader::TYPE_PIXEL; break; } default: { HELIUM_TRACE( TRACE_ERROR, ( TXT( "ShaderVariantResourceHandler: Failed to determine shader type from the name of object " ) TXT( "\"%s\".\n" ) ), *pVariant->GetPath().ToString() ); return false; } } uint32_t userOptionIndex = 0; ++pVariantNameString; int parseResult; #if HELIUM_UNICODE #if HELIUM_CC_CL parseResult = swscanf_s( pVariantNameString, TXT( "%" ) TSCNu32, &userOptionIndex ); #else parseResult = swscanf( pVariantNameString, TXT( "%" ) TSCNu32, &userOptionIndex ); #endif #else #if HELIUM_CC_CL parseResult = sscanf_s( pVariantNameString, TXT( "%" ) TSCNu32, &userOptionIndex ); #else parseResult = sscanf( pVariantNameString, TXT( "%" ) TSCNu32, &userOptionIndex ); #endif #endif if( parseResult != 1 ) { HELIUM_TRACE( TRACE_ERROR, ( TXT( "ShaderVariantResourceHandler: Failed to parse user shader option set index from the name of " ) TXT( "option \"%s\".\n" ) ), *pVariant->GetPath().ToString() ); return false; } // Get the parent shader. Shader* pShader = Reflect::AssertCast< Shader >( pVariant->GetOwner() ); HELIUM_ASSERT( pShader ); HELIUM_ASSERT( pShader->GetAnyFlagSet( GameObject::FLAG_PRECACHED ) ); // Acquire the user preprocessor option set associated with the target shader type and user option set index. const Shader::Options& rUserOptions = pShader->GetUserOptions(); DynArray< Name > toggleNames; DynArray< Shader::SelectPair > selectPairs; rUserOptions.GetOptionSetFromIndex( shaderType, userOptionIndex, toggleNames, selectPairs ); DynArray< PlatformPreprocessor::ShaderToken > shaderTokens; size_t userToggleNameCount = toggleNames.GetSize(); for( size_t toggleNameIndex = 0; toggleNameIndex < userToggleNameCount; ++toggleNameIndex ) { PlatformPreprocessor::ShaderToken* pToken = shaderTokens.New(); HELIUM_ASSERT( pToken ); StringConverter< tchar_t, char >::Convert( pToken->name, *toggleNames[ toggleNameIndex ] ); pToken->definition = "1"; } size_t userSelectPairCount = selectPairs.GetSize(); for( size_t selectPairIndex = 0; selectPairIndex < userSelectPairCount; ++selectPairIndex ) { const Shader::SelectPair& rPair = selectPairs[ selectPairIndex ]; PlatformPreprocessor::ShaderToken* pToken = shaderTokens.New(); HELIUM_ASSERT( pToken ); StringConverter< tchar_t, char >::Convert( pToken->name, *rPair.name ); pToken->definition = "1"; pToken = shaderTokens.New(); HELIUM_ASSERT( pToken ); StringConverter< tchar_t, char >::Convert( pToken->name, *rPair.choice ); pToken->definition = "1"; } size_t userShaderTokenCount = shaderTokens.GetSize(); // Load the entire shader resource into memory. FileStream* pSourceFileStream = File::Open( rSourceFilePath, FileStream::MODE_READ ); if( !pSourceFileStream ) { HELIUM_TRACE( TRACE_ERROR, ( TXT( "ShaderVariantResourceHandler: Source file for shader variant resource \"%s\" failed to open " ) TXT( "properly.\n" ) ), *pVariant->GetPath().ToString() ); return false; } int64_t size64 = pSourceFileStream->GetSize(); HELIUM_ASSERT( size64 != -1 ); HELIUM_ASSERT( static_cast< uint64_t >( size64 ) <= static_cast< size_t >( -1 ) ); if( size64 > static_cast< uint64_t >( static_cast< size_t >( -1 ) ) ) { HELIUM_TRACE( TRACE_ERROR, ( TXT( "ShaderVariantResourceHandler: Source file for shader resource \"%s\" is too large to fit " ) TXT( "into memory for preprocessing.\n" ) ), *pShader->GetPath().ToString() ); delete pSourceFileStream; return false; } size_t size = static_cast< size_t >( size64 ); DefaultAllocator allocator; void* pShaderSource = allocator.Allocate( size ); HELIUM_ASSERT( pShaderSource ); if( !pShaderSource ) { HELIUM_TRACE( TRACE_ERROR, ( TXT( "ShaderVariantResourceHandler: Failed to allocate %" ) TPRIuSZ TXT( " bytes for loading the " ) TXT( "source data of \"%s\" for preprocessing.\n" ) ), size, *pShader->GetPath().ToString() ); delete pSourceFileStream; return false; } BufferedStream( pSourceFileStream ).Read( pShaderSource, 1, size ); delete pSourceFileStream; // Compile each variant of system options for each shader profile in each supported target platform. const Shader::Options& rSystemOptions = pShader->GetSystemOptions(); size_t systemOptionSetCount = rSystemOptions.ComputeOptionSetCount( shaderType ); if( systemOptionSetCount > UINT32_MAX ) { HELIUM_TRACE( TRACE_ERROR, ( TXT( "ShaderVariantResourceHandler: System option set count (%" ) TPRIuSZ TXT( ") in shader \"%s\" " ) TXT( "exceeds the maximum supported (%" ) TPRIuSZ TXT( ").\n" ) ), systemOptionSetCount, *pShader->GetPath().ToString(), static_cast< size_t >( UINT32_MAX ) ); allocator.Free( pShaderSource ); return false; } uint32_t systemOptionSetCount32 = static_cast< uint32_t >( systemOptionSetCount ); for( size_t platformIndex = 0; platformIndex < static_cast< size_t >( Cache::PLATFORM_MAX ); ++platformIndex ) { PlatformPreprocessor* pPreprocessor = pObjectPreprocessor->GetPlatformPreprocessor( static_cast< Cache::EPlatform >( platformIndex ) ); if( !pPreprocessor ) { continue; } Resource::PreprocessedData& rPreprocessedData = pVariant->GetPreprocessedData( static_cast< Cache::EPlatform >( platformIndex ) ); ShaderVariant::PersistentResourceData persistentResourceData; persistentResourceData.m_resourceCount = systemOptionSetCount32; SaveObjectToPersistentDataBuffer(&persistentResourceData, rPreprocessedData.persistentDataBuffer); size_t shaderProfileCount = pPreprocessor->GetShaderProfileCount(); size_t shaderCount = shaderProfileCount * systemOptionSetCount; DynArray< DynArray< uint8_t > >& rSubDataBuffers = rPreprocessedData.subDataBuffers; rSubDataBuffers.Reserve( shaderCount ); rSubDataBuffers.Resize( 0 ); rSubDataBuffers.Resize( shaderCount ); rSubDataBuffers.Trim(); rPreprocessedData.bLoaded = true; } // DynArray< uint8_t > compiledCodeBuffer; // DynArray< ShaderConstantBufferInfo > constantBuffers, pcSm4ConstantBuffers; // DynArray< ShaderSamplerInfo > samplerInputs; // DynArray< ShaderTextureInfo > textureInputs; CompiledShaderData csd_pc_sm4; for( size_t systemOptionSetIndex = 0; systemOptionSetIndex < systemOptionSetCount; ++systemOptionSetIndex ) { rSystemOptions.GetOptionSetFromIndex( shaderType, systemOptionSetIndex, toggleNames, selectPairs ); size_t systemToggleNameCount = toggleNames.GetSize(); for( size_t toggleNameIndex = 0; toggleNameIndex < systemToggleNameCount; ++toggleNameIndex ) { PlatformPreprocessor::ShaderToken* pToken = shaderTokens.New(); HELIUM_ASSERT( pToken ); StringConverter< tchar_t, char >::Convert( pToken->name, *toggleNames[ toggleNameIndex ] ); pToken->definition = "1"; } size_t systemSelectPairCount = selectPairs.GetSize(); for( size_t selectPairIndex = 0; selectPairIndex < systemSelectPairCount; ++selectPairIndex ) { const Shader::SelectPair& rPair = selectPairs[ selectPairIndex ]; PlatformPreprocessor::ShaderToken* pToken = shaderTokens.New(); HELIUM_ASSERT( pToken ); StringConverter< tchar_t, char >::Convert( pToken->name, *rPair.name ); pToken->definition = "1"; pToken = shaderTokens.New(); HELIUM_ASSERT( pToken ); StringConverter< tchar_t, char >::Convert( pToken->name, *rPair.choice ); pToken->definition = "1"; } // Compile for PC shader model 4 first so that we can get the constant buffer information. PlatformPreprocessor* pPreprocessor = pObjectPreprocessor->GetPlatformPreprocessor( Cache::PLATFORM_PC ); HELIUM_ASSERT( pPreprocessor ); csd_pc_sm4.compiledCodeBuffer.Resize( 0 ); bool bCompiled = CompileShader( pVariant, pPreprocessor, Cache::PLATFORM_PC, ShaderProfile::PC_SM4, shaderType, pShaderSource, size, shaderTokens, csd_pc_sm4.compiledCodeBuffer ); if( !bCompiled ) { HELIUM_TRACE( TRACE_ERROR, ( TXT( "ShaderVariantResourceHandler: Failed to compile shader for PC shader model 4, which is " ) TXT( "needed for reflection purposes. Additional shader targets will not be built.\n" ) ) ); } else { csd_pc_sm4.constantBuffers.Resize( 0 ); csd_pc_sm4.samplerInputs.Resize( 0 ); csd_pc_sm4.textureInputs.Resize( 0 ); bool bReadConstantBuffers = pPreprocessor->FillShaderReflectionData( ShaderProfile::PC_SM4, csd_pc_sm4.compiledCodeBuffer.GetData(), csd_pc_sm4.compiledCodeBuffer.GetSize(), csd_pc_sm4.constantBuffers, csd_pc_sm4.samplerInputs, csd_pc_sm4.textureInputs ); if( !bReadConstantBuffers ) { HELIUM_TRACE( TRACE_ERROR, ( TXT( "ShaderVariantResourceHandler: Failed to read reflection information for PC shader " ) TXT( "model 4. Additional shader targets will not be built.\n" ) ) ); } else { Resource::PreprocessedData& rPcPreprocessedData = pVariant->GetPreprocessedData( Cache::PLATFORM_PC ); DynArray< DynArray< uint8_t > >& rPcSubDataBuffers = rPcPreprocessedData.subDataBuffers; DynArray< uint8_t >& rPcSm4SubDataBuffer = rPcSubDataBuffers[ ShaderProfile::PC_SM4 * systemOptionSetCount + systemOptionSetIndex ]; Cache::WriteCacheObjectToBuffer(csd_pc_sm4, rPcSm4SubDataBuffer); // FOR EACH PLATFORM for( size_t platformIndex = 0; platformIndex < static_cast< size_t >( Cache::PLATFORM_MAX ); ++platformIndex ) { PlatformPreprocessor* pPreprocessor = pObjectPreprocessor->GetPlatformPreprocessor( static_cast< Cache::EPlatform >( platformIndex ) ); if( !pPreprocessor ) { continue; } // GET PLATFORM'S SUBDATA BUFFER Resource::PreprocessedData& rPreprocessedData = pVariant->GetPreprocessedData( static_cast< Cache::EPlatform >( platformIndex ) ); DynArray< DynArray< uint8_t > >& rSubDataBuffers = rPreprocessedData.subDataBuffers; size_t shaderProfileCount = pPreprocessor->GetShaderProfileCount(); for( size_t shaderProfileIndex = 0; shaderProfileIndex < shaderProfileCount; ++shaderProfileIndex ) { CompiledShaderData csd; // Already cached PC shader model 4... if( shaderProfileIndex == ShaderProfile::PC_SM4 && platformIndex == Cache::PLATFORM_PC ) { continue; } bCompiled = CompileShader( pVariant, pPreprocessor, platformIndex, shaderProfileIndex, shaderType, pShaderSource, size, shaderTokens, csd.compiledCodeBuffer ); if( !bCompiled ) { continue; } csd.constantBuffers = csd_pc_sm4.constantBuffers; csd.samplerInputs.Resize( 0 ); csd.textureInputs.Resize( 0 ); bReadConstantBuffers = pPreprocessor->FillShaderReflectionData( shaderProfileIndex, csd.compiledCodeBuffer.GetData(), csd.compiledCodeBuffer.GetSize(), csd.constantBuffers, csd.samplerInputs, csd.textureInputs ); if( !bReadConstantBuffers ) { continue; } DynArray< uint8_t >& rTargetSubDataBuffer = rSubDataBuffers[ shaderProfileIndex * systemOptionSetCount + systemOptionSetIndex ]; Cache::WriteCacheObjectToBuffer(csd, rTargetSubDataBuffer); } } } } // Trim the system tokens off the shader token list for the next pass. shaderTokens.Resize( userShaderTokenCount ); } allocator.Free( pShaderSource ); return true; }
/// @copydoc ResourceHandler::CacheResource() bool MeshResourceHandler::CacheResource( ObjectPreprocessor* pObjectPreprocessor, Resource* pResource, const String& rSourceFilePath ) { HELIUM_ASSERT( pObjectPreprocessor ); HELIUM_ASSERT( pResource ); // Load and parse the mesh data. DynArray< StaticMeshVertex< 1 > > vertices; DynArray< uint16_t > indices; DynArray< uint16_t > sectionVertexCounts; DynArray< uint32_t > sectionTriangleCounts; DynArray< FbxSupport::BoneData > bones; DynArray< FbxSupport::BlendData > vertexBlendData; DynArray< uint8_t > skinningPaletteMap; bool bLoadSuccess = m_rFbxSupport.LoadMesh( rSourceFilePath, vertices, indices, sectionVertexCounts, sectionTriangleCounts, bones, vertexBlendData, skinningPaletteMap ); if( !bLoadSuccess ) { HELIUM_TRACE( TRACE_ERROR, TXT( "MeshResourceHandler::CacheResource(): Failed to build mesh from source file \"%s\".\n" ), *rSourceFilePath ); return false; } size_t vertexCountActual = vertices.GetSize(); HELIUM_ASSERT( vertexCountActual <= UINT32_MAX ); uint32_t vertexCount = static_cast< uint32_t >( vertexCountActual ); size_t indexCount = indices.GetSize(); size_t triangleCountActual = indexCount; HELIUM_ASSERT( triangleCountActual % 3 == 0 ); triangleCountActual /= 3; HELIUM_ASSERT( triangleCountActual <= UINT32_MAX ); uint32_t triangleCount = static_cast< uint32_t >( triangleCountActual ); size_t boneCountActual = bones.GetSize(); HELIUM_ASSERT( boneCountActual <= UINT8_MAX ); #if !HELIUM_USE_GRANNY_ANIMATION uint8_t boneCount = static_cast< uint8_t >( boneCountActual ); #endif // Compute the mesh bounding box. Simd::AaBox bounds; if( vertexCountActual != 0 ) { const float32_t* pPosition = vertices[ 0 ].position; Simd::Vector3 position( pPosition[ 0 ], pPosition[ 1 ], pPosition[ 2 ] ); bounds.Set( position, position ); for( size_t vertexIndex = 1; vertexIndex < vertexCountActual; ++vertexIndex ) { pPosition = vertices[ vertexIndex ].position; bounds.Expand( Simd::Vector3( pPosition[ 0 ], pPosition[ 1 ], pPosition[ 2 ] ) ); } } #if HELIUM_USE_GRANNY_ANIMATION Granny::MeshCachingData grannyMeshCachingData; grannyMeshCachingData.BuildResourceData( bones ); #endif // HELIUM_USE_GRANNY_ANIMATION // Cache the data for each supported platform. BinarySerializer serializer; for( size_t platformIndex = 0; platformIndex < static_cast< size_t >( Cache::PLATFORM_MAX ); ++platformIndex ) { PlatformPreprocessor* pPreprocessor = pObjectPreprocessor->GetPlatformPreprocessor( static_cast< Cache::EPlatform >( platformIndex ) ); if( !pPreprocessor ) { continue; } Resource::PreprocessedData& rPreprocessedData = pResource->GetPreprocessedData( static_cast< Cache::EPlatform >( platformIndex ) ); DynArray< DynArray< uint8_t > >& rSubDataBuffers = rPreprocessedData.subDataBuffers; rSubDataBuffers.Reserve( 2 ); rSubDataBuffers.Resize( 2 ); rSubDataBuffers.Trim(); serializer.SetByteSwapping( pPreprocessor->SwapBytes() ); // Serialize the buffer sizes and mesh bounds first. serializer.BeginSerialize(); serializer << Serializer::WrapDynArray( sectionVertexCounts ); serializer << Serializer::WrapDynArray( sectionTriangleCounts ); serializer << Serializer::WrapDynArray( skinningPaletteMap ); serializer << vertexCount; serializer << triangleCount; serializer << bounds; #if HELIUM_USE_GRANNY_ANIMATION grannyMeshCachingData.CachePlatformResourceData( pPreprocessor, serializer ); #else serializer << boneCount; for( size_t boneIndex = 0; boneIndex < boneCount; ++boneIndex ) { FbxSupport::BoneData& rBoneData = bones[ boneIndex ]; serializer << rBoneData.name; serializer << rBoneData.parentIndex; serializer << rBoneData.referenceTransform; } #endif serializer.EndSerialize(); rPreprocessedData.persistentDataBuffer = serializer.GetPropertyStreamBuffer(); // Serialize the vertex buffer. If the mesh is a skinned mesh, the vertices will need to be converted to // and serialized as an array of SkinnedMeshVertex structs. serializer.BeginSerialize(); if( boneCountActual == 0 ) { for( size_t vertexIndex = 0; vertexIndex < vertexCountActual; ++vertexIndex ) { vertices[ vertexIndex ].Serialize( serializer ); } } else { HELIUM_ASSERT( vertexBlendData.GetSize() == vertexCountActual ); SkinnedMeshVertex vertex; for( size_t vertexIndex = 0; vertexIndex < vertexCountActual; ++vertexIndex ) { const StaticMeshVertex< 1 >& rStaticVertex = vertices[ vertexIndex ]; const FbxSupport::BlendData& rBlendData = vertexBlendData[ vertexIndex ]; MemoryCopy( vertex.position, rStaticVertex.position, sizeof( vertex.position ) ); vertex.blendWeights[ 0 ] = static_cast< uint8_t >( Clamp( rBlendData.weights[ 0 ] * 255.0f + 0.5f, 0.0f, 255.0f ) ); vertex.blendWeights[ 1 ] = static_cast< uint8_t >( Clamp( rBlendData.weights[ 1 ] * 255.0f + 0.5f, 0.0f, 255.0f ) ); vertex.blendWeights[ 2 ] = static_cast< uint8_t >( Clamp( rBlendData.weights[ 2 ] * 255.0f + 0.5f, 0.0f, 255.0f ) ); vertex.blendWeights[ 3 ] = static_cast< uint8_t >( Clamp( rBlendData.weights[ 3 ] * 255.0f + 0.5f, 0.0f, 255.0f ) ); // Tweak the blend weights to ensure they still add up to 255 (1.0 when normalized by the GPU). size_t blendWeightTotal = static_cast< size_t >( vertex.blendWeights[ 0 ] ) + static_cast< size_t >( vertex.blendWeights[ 1 ] ) + static_cast< size_t >( vertex.blendWeights[ 2 ] ) + static_cast< size_t >( vertex.blendWeights[ 3 ] ); if( blendWeightTotal != 0 && blendWeightTotal != 255 ) { if( blendWeightTotal > 255 ) { // Total blend weight is too large, so decrease blend weights, starting from the lowest // non-zero weight. size_t weightAdjustIndex = 0; do { do { weightAdjustIndex = ( weightAdjustIndex + 3 ) % 4; } while( vertex.blendWeights[ weightAdjustIndex ] == 0 ); --vertex.blendWeights[ weightAdjustIndex ]; --blendWeightTotal; } while( blendWeightTotal > 255 ); } else { // Total blend weight is too small, so increase blend weights, starting from the highest // non-zero blend weight. Note that we should not have to check whether the blend weight is // already at its max, as that would mean our total blend weight would have to already be at // least 255. size_t weightAdjustIndex = 3; do { do { weightAdjustIndex = ( weightAdjustIndex + 1 ) % 4; } while( vertex.blendWeights[ weightAdjustIndex ] == 0 ); HELIUM_ASSERT( vertex.blendWeights[ weightAdjustIndex ] != 255 ); ++vertex.blendWeights[ weightAdjustIndex ]; ++blendWeightTotal; } while( blendWeightTotal < 255 ); } HELIUM_ASSERT( blendWeightTotal == 255 ); } MemoryCopy( vertex.blendIndices, rBlendData.indices, sizeof( vertex.blendIndices ) ); MemoryCopy( vertex.normal, rStaticVertex.normal, sizeof( vertex.normal ) ); MemoryCopy( vertex.tangent, rStaticVertex.tangent, sizeof( vertex.tangent ) ); MemoryCopy( vertex.texCoords, rStaticVertex.texCoords[ 0 ], sizeof( vertex.texCoords ) ); vertex.Serialize( serializer ); } } serializer.EndSerialize(); rSubDataBuffers[ 0 ] = serializer.GetPropertyStreamBuffer(); // Serialize the index buffer. serializer.BeginSerialize(); for( size_t indexIndex = 0; indexIndex < indexCount; ++indexIndex ) { serializer << indices[ indexIndex ]; } serializer.EndSerialize(); rSubDataBuffers[ 1 ] = serializer.GetPropertyStreamBuffer(); // Platform data is now loaded. rPreprocessedData.bLoaded = true; } return true; }
/// @copydoc GameObject::Serialize() void Material::Serialize( Serializer& s ) { L_SERIALIZE_BASE( s ); s << L_TAGGED( m_spShader ); #if L_EDITOR if( s.CanResolveTags() ) { m_userOptions.Resize( 0 ); Serializer::EMode serializerMode = s.GetMode(); bool bSaving = ( serializerMode == Serializer::MODE_SAVE ); bool bLoading = ( serializerMode == Serializer::MODE_LOAD ); if( bSaving ) { // Aggregate all user options from the resource information into the single "m_userOptions" array. Shader* pShader = m_spShader; if( pShader ) { const Shader::Options& rUserOptions = pShader->GetUserOptions(); DynArray< Name > enabledToggles; rUserOptions.GetOptionSetFromIndex( RShader::TYPE_FIRST, m_shaderVariantIndices[ 0 ], enabledToggles, m_userOptions ); size_t enabledToggleCount = enabledToggles.GetSize(); Shader::SelectPair optionPair; Name enabledChoice( TXT( "1" ) ); Name disabledChoice( TXT( "0" ) ); const DynArray< Shader::Toggle >& rUserToggles = rUserOptions.GetToggles(); size_t userToggleCount = rUserToggles.GetSize(); for( size_t userToggleIndex = 0; userToggleIndex < userToggleCount; ++userToggleIndex ) { optionPair.name = rUserToggles[ userToggleIndex ].name; size_t enabledToggleIndex; for( enabledToggleIndex = 0; enabledToggleIndex < enabledToggleCount; ++enabledToggleIndex ) { if( enabledToggles[ enabledToggleIndex ] == optionPair.name ) { break; } } optionPair.choice = ( enabledToggleIndex < enabledToggleCount ? enabledChoice : disabledChoice ); m_userOptions.Push( optionPair ); } } } else if( bLoading ) { DynArray< String > propertyTagNames; s.GetPropertyTagNames( propertyTagNames ); Shader::SelectPair optionPair; optionPair.choice.Clear(); size_t propertyTagCount = propertyTagNames.GetSize(); for( size_t tagIndex = 0; tagIndex < propertyTagCount; ++tagIndex ) { const String& rTagName = propertyTagNames[ tagIndex ]; if( !rTagName.Contains( TXT( '.' ) ) ) { optionPair.name.Set( rTagName ); m_userOptions.Push( optionPair ); } } } s.PushPropertyFlags( Serializer::FLAG_EDITOR_ONLY ); size_t userOptionCount = m_userOptions.GetSize(); for( size_t optionIndex = 0; optionIndex < userOptionCount; ++optionIndex ) { Shader::SelectPair& rOptionPair = m_userOptions[ optionIndex ]; s << Serializer::Tag( *rOptionPair.name ) << rOptionPair.choice; } // XXX TMC TODO: Replace with flexible name resolution support (a la m_userOptions above). s << L_TAGGED_STRUCT_DYNARRAY( m_float1Parameters ); s << L_TAGGED_STRUCT_DYNARRAY( m_float2Parameters ); s << L_TAGGED_STRUCT_DYNARRAY( m_float3Parameters ); s << L_TAGGED_STRUCT_DYNARRAY( m_float4Parameters ); s.PopPropertyFlags(); if( bSaving ) { m_userOptions.Clear(); } else if( bLoading ) { m_bLoadedOptions = true; } } #endif // XXX TMC TODO: Replace with flexible name resolution support (a la m_userOptions above). s << L_TAGGED_STRUCT_DYNARRAY( m_textureParameters ); }
/// Synchronize the shader parameter list with those provided by the selected shader variant. /// /// @see SynchronizeFloatVectorParameters(), SynchronizeTextureParameters() void Material::SynchronizeShaderParameters() { Shader* pShader = m_spShader; if( !pShader ) { m_float1Parameters.Clear(); m_float2Parameters.Clear(); m_float3Parameters.Clear(); m_float4Parameters.Clear(); m_textureParameters.Clear(); } // Synchronize floating-point constant parameters. Name parameterConstantBufferName = GetParameterConstantBufferName(); size_t existingFloat1Count = m_float1Parameters.GetSize(); size_t existingFloat2Count = m_float2Parameters.GetSize(); size_t existingFloat3Count = m_float3Parameters.GetSize(); size_t existingFloat4Count = m_float4Parameters.GetSize(); DynArray< Float1Parameter > newFloat1Parameters; DynArray< Float2Parameter > newFloat2Parameters; DynArray< Float3Parameter > newFloat3Parameters; DynArray< Float4Parameter > newFloat4Parameters; for( size_t shaderTypeIndex = 0; shaderTypeIndex < HELIUM_ARRAY_COUNT( m_shaderVariants ); ++shaderTypeIndex ) { ShaderVariant* pShaderVariant = m_shaderVariants[ shaderTypeIndex ]; if( !pShaderVariant ) { continue; } const ShaderConstantBufferInfoSet* pBufferSet = pShaderVariant->GetConstantBufferInfoSet( 0 ); if( !pBufferSet ) { continue; } bool bCheckDuplicates = ( !newFloat1Parameters.IsEmpty() || !newFloat2Parameters.IsEmpty() || !newFloat3Parameters.IsEmpty() || !newFloat4Parameters.IsEmpty() ); const DynArray< ShaderConstantBufferInfo >& rBuffers = pBufferSet->buffers; size_t bufferCount = rBuffers.GetSize(); for( size_t bufferIndex = 0; bufferIndex < bufferCount; ++bufferIndex ) { const ShaderConstantBufferInfo& rBufferInfo = rBuffers[ bufferIndex ]; if( rBufferInfo.name != parameterConstantBufferName ) { continue; } const DynArray< ShaderConstantInfo >& rConstants = rBufferInfo.constants; size_t constantCount = rConstants.GetSize(); for( size_t constantIndex = 0; constantIndex < constantCount; ++constantIndex ) { const ShaderConstantInfo& rConstantInfo = rConstants[ constantIndex ]; // Constants must be between 1 and 4 floating-point values. uint16_t constantSize = rConstantInfo.usedSize; if( constantSize < sizeof( float32_t ) || constantSize > sizeof( float32_t ) * 4 ) { continue; } Name constantName = rConstantInfo.name; size_t parameterIndex; if( bCheckDuplicates ) { size_t parameterCount = newFloat1Parameters.GetSize(); for( parameterIndex = 0; parameterIndex < parameterCount; ++parameterIndex ) { if( newFloat1Parameters[ parameterIndex ].name == constantName ) { break; } } if( parameterIndex < parameterCount ) { continue; } parameterCount = newFloat2Parameters.GetSize(); for( parameterIndex = 0; parameterIndex < parameterCount; ++parameterIndex ) { if( newFloat2Parameters[ parameterIndex ].name == constantName ) { break; } } if( parameterIndex < parameterCount ) { continue; } parameterCount = newFloat3Parameters.GetSize(); for( parameterIndex = 0; parameterIndex < parameterCount; ++parameterIndex ) { if( newFloat3Parameters[ parameterIndex ].name == constantName ) { break; } } if( parameterIndex < parameterCount ) { continue; } parameterCount = newFloat4Parameters.GetSize(); for( parameterIndex = 0; parameterIndex < parameterCount; ++parameterIndex ) { if( newFloat4Parameters[ parameterIndex ].name == constantName ) { break; } } if( parameterIndex < parameterCount ) { continue; } } Simd::Vector4 newValue( 0.0f ); for( parameterIndex = 0; parameterIndex < existingFloat1Count; ++parameterIndex ) { const Float1Parameter& rExistingParameter = m_float1Parameters[ parameterIndex ]; if( rExistingParameter.name == constantName ) { newValue.SetElement( 0, rExistingParameter.value ); break; } } if( parameterIndex >= existingFloat1Count ) { for( parameterIndex = 0; parameterIndex < existingFloat2Count; ++parameterIndex ) { const Float2Parameter& rExistingParameter = m_float2Parameters[ parameterIndex ]; if( rExistingParameter.name == constantName ) { newValue.SetElement( 0, rExistingParameter.value.GetX() ); newValue.SetElement( 1, rExistingParameter.value.GetY() ); break; } } if( parameterIndex >= existingFloat2Count ) { for( parameterIndex = 0; parameterIndex < existingFloat3Count; ++parameterIndex ) { const Float3Parameter& rExistingParameter = m_float3Parameters[ parameterIndex ]; if( rExistingParameter.name == constantName ) { newValue.SetElement( 0, rExistingParameter.value.GetElement( 0 ) ); newValue.SetElement( 1, rExistingParameter.value.GetElement( 1 ) ); newValue.SetElement( 2, rExistingParameter.value.GetElement( 2 ) ); break; } } if( parameterIndex >= existingFloat3Count ) { for( parameterIndex = 0; parameterIndex < existingFloat4Count; ++parameterIndex ) { const Float4Parameter& rExistingParameter = m_float4Parameters[ parameterIndex ]; if( rExistingParameter.name == constantName ) { newValue = rExistingParameter.value; break; } } } } } if( constantSize < sizeof( float32_t ) * 2 ) { Float1Parameter* pParameter = newFloat1Parameters.New(); HELIUM_ASSERT( pParameter ); pParameter->name = constantName; pParameter->value = newValue.GetElement( 0 ); } else if( constantSize < sizeof( float32_t ) * 3 ) { Float2Parameter* pParameter = newFloat2Parameters.New(); HELIUM_ASSERT( pParameter ); pParameter->name = constantName; pParameter->value = Simd::Vector2( newValue.GetElement( 0 ), newValue.GetElement( 1 ) ); } else if( constantSize < sizeof( float32_t ) * 4 ) { Float3Parameter* pParameter = newFloat3Parameters.New(); HELIUM_ASSERT( pParameter ); pParameter->name = constantName; pParameter->value = Simd::Vector3( newValue.GetElement( 0 ), newValue.GetElement( 1 ), newValue.GetElement( 2 ) ); } else { Float4Parameter* pParameter = newFloat4Parameters.New(); HELIUM_ASSERT( pParameter ); pParameter->name = constantName; pParameter->value = newValue; } } } } newFloat1Parameters.Trim(); newFloat2Parameters.Trim(); newFloat3Parameters.Trim(); newFloat4Parameters.Trim(); m_float1Parameters.Swap( newFloat1Parameters ); m_float2Parameters.Swap( newFloat2Parameters ); m_float3Parameters.Swap( newFloat3Parameters ); m_float4Parameters.Swap( newFloat4Parameters ); newFloat1Parameters.Clear(); newFloat2Parameters.Clear(); newFloat3Parameters.Clear(); newFloat4Parameters.Clear(); // Synchronize texture parameters. size_t existingTextureCount = m_textureParameters.GetSize(); DynArray< TextureParameter > newTextureParameters; for( size_t shaderTypeIndex = 0; shaderTypeIndex < HELIUM_ARRAY_COUNT( m_shaderVariants ); ++shaderTypeIndex ) { ShaderVariant* pShaderVariant = m_shaderVariants[ shaderTypeIndex ]; if( !pShaderVariant ) { continue; } const ShaderTextureInfoSet* pTextureSet = pShaderVariant->GetTextureInfoSet( 0 ); if( !pTextureSet ) { continue; } bool bCheckDuplicates = !newTextureParameters.IsEmpty(); const DynArray< ShaderTextureInfo >& rTextureInputs = pTextureSet->inputs; size_t textureInputCount = rTextureInputs.GetSize(); for( size_t textureIndex = 0; textureIndex < textureInputCount; ++textureIndex ) { const ShaderTextureInfo& rTextureInfo = rTextureInputs[ textureIndex ]; // Ignore textures prefixed with an underscore, as they are reserved for system use. Name textureInputName = rTextureInfo.name; if( !textureInputName.IsEmpty() && ( *textureInputName )[ 0 ] == TXT( '_' ) ) { continue; } size_t parameterIndex; if( bCheckDuplicates ) { size_t textureParameterCount = newTextureParameters.GetSize(); for( parameterIndex = 0; parameterIndex < textureParameterCount; ++parameterIndex ) { if( newTextureParameters[ parameterIndex ].name == textureInputName ) { break; } } if( parameterIndex < textureParameterCount ) { continue; } } TextureParameter* pParameter = newTextureParameters.New(); HELIUM_ASSERT( pParameter ); pParameter->name = textureInputName; for( parameterIndex = 0; parameterIndex < existingTextureCount; ++parameterIndex ) { const TextureParameter& rTextureParameter = m_textureParameters[ parameterIndex ]; if( rTextureParameter.name == textureInputName ) { pParameter->value = rTextureParameter.value; break; } } } } newTextureParameters.Trim(); m_textureParameters.Swap( newTextureParameters ); newTextureParameters.Clear(); }