Просмотр исходного кода

Add Vertex/PixelShaderResource

Daniele Bartolini 12 лет назад
Родитель
Сommit
54d507e467

+ 4 - 0
src/CMakeLists.txt

@@ -10,6 +10,8 @@ set (SRC
 	TextResource.cpp
 	ScriptResource.cpp
 	FontResource.cpp
+	VertexShaderResource.cpp
+	PixelShaderResource.cpp
 	ArchiveResourceArchive.cpp
 	FileResourceArchive.cpp
 
@@ -34,6 +36,8 @@ set (HEADERS
 	TextResource.h
 	ScriptResource.h
 	FontResource.h
+	VertexShaderResource.h
+	PixelShaderResource.h
 	ArchiveResourceArchive.h
 	FileResourceArchive.h
 

+ 2 - 0
src/Crown.h

@@ -109,6 +109,8 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "ScriptResource.h"
 #include "MaterialResource.h"
 #include "FontResource.h"
+#include "VertexShaderResource.h"
+#include "PixelShaderResource.h"
 
 // Engine/Filesystem
 #include "Filesystem.h"

+ 86 - 0
src/PixelShaderResource.cpp

@@ -0,0 +1,86 @@
+/*
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include "PixelShaderResource.h"
+#include "FileStream.h"
+#include "ResourceArchive.h"
+#include "Allocator.h"
+#include "Device.h"
+#include "Log.h"
+
+namespace crown
+{
+
+//-----------------------------------------------------------------------------
+void* PixelShaderResource::load(Allocator& allocator, ResourceArchive& archive, ResourceId id)
+{
+	FileStream* stream = archive.open(id);
+
+	if (stream != NULL)
+	{
+		PixelShaderResource* resource = (PixelShaderResource*)allocator.allocate(sizeof(PixelShaderResource));
+
+		stream->read(&resource->m_program_text_length, sizeof(uint32_t));
+
+		resource->m_program_text = (char*)allocator.allocate(sizeof(char) * (resource->m_program_text_length + 1));
+
+		stream->read(resource->m_program_text, (size_t)resource->m_program_text_length);
+		
+		resource->m_program_text[resource->m_program_text_length] = '\0';
+
+		archive.close(stream);
+
+		return resource;
+	}
+
+	return NULL;
+}
+
+//-----------------------------------------------------------------------------
+void PixelShaderResource::unload(Allocator& allocator, void* resource)
+{
+	assert(resource != NULL);
+
+	((PixelShaderResource*)resource)->m_program_text_length = 0;
+
+	allocator.deallocate(((PixelShaderResource*)resource)->m_program_text);
+	allocator.deallocate(resource);
+}
+
+//-----------------------------------------------------------------------------
+void PixelShaderResource::online(void* resource)
+{
+	Renderer* renderer = device()->renderer();
+	PixelShaderResource* pixel_shader_resource = (PixelShaderResource*) resource;
+
+	pixel_shader_resource->m_pixel_shader_id = renderer->create_pixel_shader(pixel_shader_resource->m_program_text);
+}
+
+//-----------------------------------------------------------------------------
+void PixelShaderResource::offline()
+{
+}
+
+} // namespace crown

+ 57 - 0
src/PixelShaderResource.h

@@ -0,0 +1,57 @@
+/*
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#pragma once
+
+#include "Types.h"
+#include "Resource.h"
+#include "Renderer.h"
+
+namespace crown
+{
+
+class ResourceArchive;
+class Allocator;
+
+class PixelShaderResource
+{
+public:
+
+	static void*		load(Allocator& allocator, ResourceArchive& archive, ResourceId id);
+	static void			online(void* resource);
+	static void			unload(Allocator& allocator, void* resource);
+	static void			offline();
+
+	PixelShaderId		pixel_shader() const { return m_pixel_shader_id; }
+
+private:
+
+	uint32_t			m_program_text_length;
+	char*				m_program_text;
+
+	PixelShaderId		m_pixel_shader_id;
+};
+
+} // namespace crown

+ 37 - 11
src/ResourceManager.cpp

@@ -39,6 +39,8 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "TextResource.h"
 #include "TextureResource.h"
 #include "ScriptResource.h"
+#include "VertexShaderResource.h"
+#include "PixelShaderResource.h"
 
 namespace crown
 {
@@ -263,16 +265,16 @@ void ResourceManager::bring_loaded_online()
 //-----------------------------------------------------------------------------
 ResourceId ResourceManager::load(uint32_t name, uint32_t type)
 {
+	ResourceId id;
+	id.name = name;
+	id.type = type;
+
 	// Search for an already existent resource
-	ResourceEntry* entry = std::find(m_resources.begin(), m_resources.end(), name);
+	ResourceEntry* entry = std::find(m_resources.begin(), m_resources.end(), id);
 
 	// If resource not found, create a new one
 	if (entry == m_resources.end())
 	{
-		ResourceId id;
-
-		id.name = name;
-		id.type = type;
 		id.index = m_resources.size();
 
 		ResourceEntry entry;
@@ -347,6 +349,14 @@ void* ResourceManager::load_by_type(ResourceId name) const
 	{
 		return ScriptResource::load(m_resource_allocator, m_resource_archive, name);
 	}
+	else if (name.type == VERTEX_SHADER_TYPE)
+	{
+		return VertexShaderResource::load(m_resource_allocator, m_resource_archive, name);
+	}
+	else if (name.type == PIXEL_SHADER_TYPE)
+	{
+		return PixelShaderResource::load(m_resource_allocator, m_resource_archive, name);
+	}
 
 	return NULL;
 }
@@ -356,15 +366,23 @@ void ResourceManager::unload_by_type(ResourceId name, void* resource) const
 {
 	if (name.type == TEXTURE_TYPE)
 	{
-		TextureResource::unload(m_resource_allocator, (TextureResource*)resource);
+		TextureResource::unload(m_resource_allocator, resource);
 	}
 	else if (name.type == TEXT_TYPE)
 	{
-		TextResource::unload(m_resource_allocator, (TextResource*)resource);
+		TextResource::unload(m_resource_allocator, resource);
 	}
 	else if (name.type == SCRIPT_TYPE)
 	{
-		ScriptResource::unload(m_resource_allocator, (ScriptResource*)resource);
+		ScriptResource::unload(m_resource_allocator, resource);
+	}
+	else if (name.type == VERTEX_SHADER_TYPE)
+	{
+		VertexShaderResource::unload(m_resource_allocator, resource);
+	}
+	else if (name.type == PIXEL_SHADER_TYPE)
+	{
+		PixelShaderResource::unload(m_resource_allocator, resource);
 	}
 
 	return;
@@ -375,15 +393,23 @@ void ResourceManager::online(ResourceId name, void* resource)
 {
 	if (name.type == TEXTURE_TYPE)
 	{
-		TextureResource::online((TextureResource*)resource);
+		TextureResource::online(resource);
 	}
 	else if (name.type == TEXT_TYPE)
 	{
-		TextResource::unload(m_resource_allocator, (TextResource*)resource);
+		TextResource::online(resource);
 	}
 	else if (name.type == SCRIPT_TYPE)
 	{
-		ScriptResource::online((ScriptResource*)resource);
+		ScriptResource::online(resource);
+	}
+	else if (name.type == VERTEX_SHADER_TYPE)
+	{
+		VertexShaderResource::online(resource);
+	}
+	else if (name.type == PIXEL_SHADER_TYPE)
+	{
+		PixelShaderResource::online(resource);
 	}
 
 	m_resources_mutex.lock();

+ 3 - 3
src/ResourceManager.h

@@ -46,14 +46,14 @@ struct ResourceEntry
 
 	void*			resource;
 
-	bool			operator==(const uint32_t& name)
+	bool			operator==(const ResourceId& resource)
 					{
-						return id.name == name;
+						return id == resource;
 					}
 
 	bool			operator==(const ResourceEntry& b)
 					{
-						return id.name == b.id.name;
+						return id == b.id;
 					}
 };
 

+ 86 - 0
src/VertexShaderResource.cpp

@@ -0,0 +1,86 @@
+/*
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include "VertexShaderResource.h"
+#include "FileStream.h"
+#include "ResourceArchive.h"
+#include "Allocator.h"
+#include "Device.h"
+#include "Log.h"
+
+namespace crown
+{
+
+//-----------------------------------------------------------------------------
+void* VertexShaderResource::load(Allocator& allocator, ResourceArchive& archive, ResourceId id)
+{
+	FileStream* stream = archive.open(id);
+
+	if (stream != NULL)
+	{
+		VertexShaderResource* resource = (VertexShaderResource*)allocator.allocate(sizeof(VertexShaderResource));
+
+		stream->read(&resource->m_program_text_length, sizeof(uint32_t));
+
+		resource->m_program_text = (char*)allocator.allocate(sizeof(char) * (resource->m_program_text_length + 1));
+
+		stream->read(resource->m_program_text, (size_t)resource->m_program_text_length);
+		
+		resource->m_program_text[resource->m_program_text_length] = '\0';
+
+		archive.close(stream);
+
+		return resource;
+	}
+
+	return NULL;
+}
+
+//-----------------------------------------------------------------------------
+void VertexShaderResource::unload(Allocator& allocator, void* resource)
+{
+	assert(resource != NULL);
+
+	((VertexShaderResource*)resource)->m_program_text_length = 0;
+
+	allocator.deallocate(((VertexShaderResource*)resource)->m_program_text);
+	allocator.deallocate(resource);
+}
+
+//-----------------------------------------------------------------------------
+void VertexShaderResource::online(void* resource)
+{
+	Renderer* renderer = device()->renderer();
+	VertexShaderResource* vertex_shader_resource = (VertexShaderResource*) resource;
+
+	vertex_shader_resource->m_vertex_shader_id = renderer->create_vertex_shader(vertex_shader_resource->m_program_text);
+}
+
+//-----------------------------------------------------------------------------
+void VertexShaderResource::offline()
+{
+}
+
+} // namespace crown

+ 57 - 0
src/VertexShaderResource.h

@@ -0,0 +1,57 @@
+/*
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#pragma once
+
+#include "Types.h"
+#include "Resource.h"
+#include "Renderer.h"
+
+namespace crown
+{
+
+class ResourceArchive;
+class Allocator;
+
+class VertexShaderResource
+{
+public:
+
+	static void*		load(Allocator& allocator, ResourceArchive& archive, ResourceId id);
+	static void			online(void* resource);
+	static void			unload(Allocator& allocator, void* resource);
+	static void			offline();
+
+	VertexShaderId		vertex_shader() const { return m_vertex_shader_id; }
+
+private:
+
+	uint32_t			m_program_text_length;
+	char*				m_program_text;
+
+	VertexShaderId		m_vertex_shader_id;
+};
+
+} // namespace crown