This commit is contained in:
		
							parent
							
								
									197e10c0cf
								
							
						
					
					
						commit
						b6acc19ac8
					
				
					 2 changed files with 81 additions and 77 deletions
				
			
		|  | @ -10,10 +10,12 @@ ShaderAsset::ShaderAsset(const std::filesystem::path &path): m_stream(path) | ||||||
| 	    + sizeof(BlobMetadata); | 	    + sizeof(BlobMetadata); | ||||||
| 
 | 
 | ||||||
| 	ensure(m_stream.is_open(), "Failed to open shader asset at: {}", path.string()); | 	ensure(m_stream.is_open(), "Failed to open shader asset at: {}", path.string()); | ||||||
|  | 	const auto read = [this](auto &field) { | ||||||
|  | 		m_stream.read(std::bit_cast<char *>(&field), sizeof(field)); | ||||||
|  | 	}; | ||||||
| 
 | 
 | ||||||
| 	m_stream.seekg(0, std::ifstream::end); | 	m_stream.seekg(0, std::ifstream::end); | ||||||
| 	const auto file_size = static_cast<size_t>(m_stream.tellg()); | 	const auto file_size = static_cast<size_t>(m_stream.tellg()); | ||||||
| 
 |  | ||||||
| 	ensure( | 	ensure( | ||||||
| 	    file_size > total_metadata_size, | 	    file_size > total_metadata_size, | ||||||
| 	    "Failed to open shader asset at: {}, file smaller than metadata: {} < {}", | 	    "Failed to open shader asset at: {}, file smaller than metadata: {} < {}", | ||||||
|  | @ -22,12 +24,15 @@ ShaderAsset::ShaderAsset(const std::filesystem::path &path): m_stream(path) | ||||||
| 	    file_size | 	    file_size | ||||||
| 	); | 	); | ||||||
| 
 | 
 | ||||||
| 	// NOLINTBEGIN(cppcoreguidelines-pro-type-cstyle-cast)
 |  | ||||||
| 	m_stream.seekg(0, std::ifstream::beg); | 	m_stream.seekg(0, std::ifstream::beg); | ||||||
| 	m_stream.read((char *)&m_asset_metadata, sizeof(m_asset_metadata)); | 	read(m_asset_metadata.type); | ||||||
| 	m_stream.read((char *)&m_metadata, sizeof(m_metadata)); | 	read(m_asset_metadata.version); | ||||||
| 	m_stream.read((char *)&m_code_blob_metadata, sizeof(m_code_blob_metadata)); | 	read(m_metadata.type); | ||||||
| 	// NOLINTEND(cppcoreguidelines-pro-type-cstyle-cast)
 | 	read(m_code_blob_metadata.tag); | ||||||
|  | 	read(m_code_blob_metadata.offset); | ||||||
|  | 	read(m_code_blob_metadata.compression_type); | ||||||
|  | 	read(m_code_blob_metadata.compressed_size); | ||||||
|  | 	read(m_code_blob_metadata.uncompressed_size); | ||||||
| 
 | 
 | ||||||
| 	ensure( | 	ensure( | ||||||
| 	    m_asset_metadata.type == asset_type_identifier, | 	    m_asset_metadata.type == asset_type_identifier, | ||||||
|  | @ -69,4 +74,69 @@ ShaderAsset::ShaderAsset(const std::filesystem::path &path): m_stream(path) | ||||||
| 	); | 	); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | /* static */ void ShaderAsset::pack( | ||||||
|  |     const std::filesystem::path &destination, | ||||||
|  |     AssetMetadata asset_metadata, | ||||||
|  |     Metadata metadata, | ||||||
|  |     Blob code_blob | ||||||
|  | ) | ||||||
|  | { | ||||||
|  | 	auto stream = std::ofstream { | ||||||
|  | 		destination, | ||||||
|  | 		std::ios::binary | std::ios::trunc, | ||||||
|  | 	}; | ||||||
|  | 	const auto code_blob_metadata = BlobMetadata { | ||||||
|  | 		.tag = std::to_underlying(BlobTag::code), | ||||||
|  | 		.offset = static_cast<size_t>(stream.tellp()) + sizeof(BlobMetadata), | ||||||
|  | 		.compression_type = CompressionType::none, | ||||||
|  | 		.compressed_size = code_blob.size(), | ||||||
|  | 		.uncompressed_size = code_blob.size(), | ||||||
|  | 	}; | ||||||
|  | 
 | ||||||
|  | 	ensure(stream.is_open(), "Failed to pack shader asset to {}", destination.string()); | ||||||
|  | 	const auto write = [&stream](auto &field) { | ||||||
|  | 		stream.write(std::bit_cast<char *>(&field), sizeof(field)); | ||||||
|  | 	}; | ||||||
|  | 	write(asset_metadata.type); | ||||||
|  | 	write(asset_metadata.version); | ||||||
|  | 	write(metadata.type); | ||||||
|  | 	write(code_blob_metadata.tag); | ||||||
|  | 	write(code_blob_metadata.offset); | ||||||
|  | 	write(code_blob_metadata.compression_type); | ||||||
|  | 	write(code_blob_metadata.compressed_size); | ||||||
|  | 	write(code_blob_metadata.uncompressed_size); | ||||||
|  | 	stream.write(std::bit_cast<char *>(code_blob.data()), static_cast<long long>(code_blob.size())); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void ShaderAsset::unpack_to(BlobTag tag, std::span<std::byte> destination) const | ||||||
|  | { | ||||||
|  | 	ensure(tag == BlobTag::code, "Invalid blob tag for shader asset: {}", std::to_underlying(tag)); | ||||||
|  | 
 | ||||||
|  | 	ensure( | ||||||
|  | 	    destination.size() >= m_code_blob_metadata.uncompressed_size, | ||||||
|  | 	    "Failed to unpack shader blob {} to destination ({}) of size {} since it's smaller " | ||||||
|  | 	    "than the blobl's uncompressed size: {}", | ||||||
|  | 	    std::to_underlying(tag), | ||||||
|  | 	    std::bit_cast<size_t>(destination.data()), | ||||||
|  | 	    destination.size(), | ||||||
|  | 	    m_code_blob_metadata.uncompressed_size | ||||||
|  | 	); | ||||||
|  | 
 | ||||||
|  | 	m_stream.seekg(static_cast<long long>(m_code_blob_metadata.offset)); | ||||||
|  | 	m_stream.read( | ||||||
|  | 	    std::bit_cast<char *>(destination.data()), | ||||||
|  | 	    static_cast<long long>(m_code_blob_metadata.uncompressed_size) | ||||||
|  | 	); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | [[nodiscard]] auto ShaderAsset::unpack(BlobTag tag) const -> Blob | ||||||
|  | { | ||||||
|  | 	ensure(tag == BlobTag::code, "Invalid blob tag for shader asset: {}", std::to_underlying(tag)); | ||||||
|  | 
 | ||||||
|  | 	auto blob = Blob(m_code_blob_metadata.uncompressed_size); | ||||||
|  | 	unpack_to(tag, blob); | ||||||
|  | 
 | ||||||
|  | 	return blob; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| } // namespace lt::assets
 | } // namespace lt::assets
 | ||||||
|  |  | ||||||
|  | @ -32,41 +32,14 @@ public: | ||||||
| 	    AssetMetadata asset_metadata, | 	    AssetMetadata asset_metadata, | ||||||
| 	    Metadata metadata, | 	    Metadata metadata, | ||||||
| 	    Blob code_blob | 	    Blob code_blob | ||||||
| 	) |  | ||||||
| 	{ |  | ||||||
| 		auto stream = std::ofstream { |  | ||||||
| 			destination, |  | ||||||
| 			std::ios::binary | std::ios::trunc, |  | ||||||
| 		}; |  | ||||||
| 		const auto code_blob_metadata = BlobMetadata { |  | ||||||
| 			.tag = std::to_underlying(BlobTag::code), |  | ||||||
| 			.offset = static_cast<size_t>(stream.tellp()) + sizeof(BlobMetadata), |  | ||||||
| 			.compression_type = CompressionType::none, |  | ||||||
| 			.compressed_size = code_blob.size(), |  | ||||||
| 			.uncompressed_size = code_blob.size(), |  | ||||||
| 		}; |  | ||||||
| 
 |  | ||||||
| 		ensure(stream.is_open(), "Failed to pack shader asset to {}", destination.string()); |  | ||||||
| 		const auto write = [&stream](auto &field) { |  | ||||||
| 			stream.write(std::bit_cast<char *>(&field), sizeof(field)); |  | ||||||
| 		}; |  | ||||||
| 
 |  | ||||||
| 		write(asset_metadata.type); |  | ||||||
| 		write(asset_metadata.version); |  | ||||||
| 		write(metadata.type); |  | ||||||
| 		write(code_blob_metadata.tag); |  | ||||||
| 		write(code_blob_metadata.offset); |  | ||||||
| 		write(code_blob_metadata.compression_type); |  | ||||||
| 		write(code_blob_metadata.compressed_size); |  | ||||||
| 		write(code_blob_metadata.uncompressed_size); |  | ||||||
| 		stream.write( |  | ||||||
| 		    std::bit_cast<char *>(code_blob.data()), |  | ||||||
| 		    static_cast<long long>(code_blob.size()) |  | ||||||
| 	); | 	); | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	ShaderAsset(const std::filesystem::path &path); | 	ShaderAsset(const std::filesystem::path &path); | ||||||
| 
 | 
 | ||||||
|  | 	void unpack_to(BlobTag tag, std::span<std::byte> destination) const; | ||||||
|  | 
 | ||||||
|  | 	[[nodiscard]] auto unpack(BlobTag tag) const -> Blob; | ||||||
|  | 
 | ||||||
| 	[[nodiscard]] auto get_asset_metadata() const -> const AssetMetadata & | 	[[nodiscard]] auto get_asset_metadata() const -> const AssetMetadata & | ||||||
| 	{ | 	{ | ||||||
| 		return m_asset_metadata; | 		return m_asset_metadata; | ||||||
|  | @ -88,45 +61,6 @@ public: | ||||||
| 		return m_code_blob_metadata; | 		return m_code_blob_metadata; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	void unpack_to(BlobTag tag, std::span<std::byte> destination) const |  | ||||||
| 	{ |  | ||||||
| 		ensure( |  | ||||||
| 		    tag == BlobTag::code, |  | ||||||
| 		    "Invalid blob tag for shader asset: {}", |  | ||||||
| 		    std::to_underlying(tag) |  | ||||||
| 		); |  | ||||||
| 
 |  | ||||||
| 		ensure( |  | ||||||
| 		    destination.size() >= m_code_blob_metadata.uncompressed_size, |  | ||||||
| 		    "Failed to unpack shader blob {} to destination ({}) of size {} since it's smaller " |  | ||||||
| 		    "than the blobl's uncompressed size: {}", |  | ||||||
| 		    std::to_underlying(tag), |  | ||||||
| 		    (size_t)(destination.data()), // NOLINT(cppcoreguidelines-pro-type-cstyle-cast)
 |  | ||||||
| 		    destination.size(), |  | ||||||
| 		    m_code_blob_metadata.uncompressed_size |  | ||||||
| 		); |  | ||||||
| 
 |  | ||||||
| 		m_stream.seekg(static_cast<long long>(m_code_blob_metadata.offset)); |  | ||||||
| 		m_stream.read( |  | ||||||
| 		    (char *)destination.data(), // NOLINT(cppcoreguidelines-pro-type-cstyle-cast)
 |  | ||||||
| 		    static_cast<long long>(m_code_blob_metadata.uncompressed_size) |  | ||||||
| 		); |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	[[nodiscard]] auto unpack(BlobTag tag) const -> Blob |  | ||||||
| 	{ |  | ||||||
| 		ensure( |  | ||||||
| 		    tag == BlobTag::code, |  | ||||||
| 		    "Invalid blob tag for shader asset: {}", |  | ||||||
| 		    std::to_underlying(tag) |  | ||||||
| 		); |  | ||||||
| 
 |  | ||||||
| 		auto blob = Blob(m_code_blob_metadata.uncompressed_size); |  | ||||||
| 		unpack_to(tag, blob); |  | ||||||
| 
 |  | ||||||
| 		return blob; |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| private: | private: | ||||||
| 	AssetMetadata m_asset_metadata {}; | 	AssetMetadata m_asset_metadata {}; | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue