diff options
Diffstat (limited to 'Runtime/Graphics')
-rw-r--r-- | Runtime/Graphics/Device.cpp | 7 | ||||
-rw-r--r-- | Runtime/Graphics/Device.h | 5 | ||||
-rw-r--r-- | Runtime/Graphics/FrameBuffer.cpp | 4 | ||||
-rw-r--r-- | Runtime/Graphics/GpuDataBuffer.cpp | 2 | ||||
-rw-r--r-- | Runtime/Graphics/ImageData.cpp | 2 | ||||
-rw-r--r-- | Runtime/Graphics/ImageData.h | 1 | ||||
-rw-r--r-- | Runtime/Graphics/Scripting/wrap_Device.cpp | 0 | ||||
-rw-r--r-- | Runtime/Graphics/Scripting/wrap_GL.cpp | 0 | ||||
-rw-r--r-- | Runtime/Graphics/Scripting/wrap_RenderTexture.cpp | 0 | ||||
-rw-r--r-- | Runtime/Graphics/Shader.cpp | 146 | ||||
-rw-r--r-- | Runtime/Graphics/Shader.h | 56 | ||||
-rw-r--r-- | Runtime/Graphics/Texture.h | 4 | ||||
-rw-r--r-- | Runtime/Graphics/VertexBuffer.cpp | 2 | ||||
-rw-r--r-- | Runtime/Graphics/VertexBuffer.h | 3 |
14 files changed, 223 insertions, 9 deletions
diff --git a/Runtime/Graphics/Device.cpp b/Runtime/Graphics/Device.cpp index d729a37..8136a54 100644 --- a/Runtime/Graphics/Device.cpp +++ b/Runtime/Graphics/Device.cpp @@ -5,10 +5,15 @@ Device g_Device; Device::Device() { - Assert(deviceInited); + //Assert(deviceInited); deviceInited = true; } +Device::~Device() +{ + +} + void Device::Initialize(DeviceSetting& setting) { GPU::BufferPool::Instance()->Initialize(); diff --git a/Runtime/Graphics/Device.h b/Runtime/Graphics/Device.h index 4e02b72..e445efa 100644 --- a/Runtime/Graphics/Device.h +++ b/Runtime/Graphics/Device.h @@ -18,7 +18,7 @@ struct DeviceSetting class Device : public NonCopyable { -public: +public: Device(); ~Device(); @@ -41,7 +41,8 @@ public: void EndFrame(); void PresentFrame(); - GET(SharedVertexBuffer*, SharedVBO, m_SharedVBO); + //GET(SharedVertexBuffer*, SharedVBO, m_SharedVBO); + SharedVertexBuffer* GetSharedVBO() { return &m_SharedVBO; } GET_SET(Color, ClearColor, m_ClearColor); diff --git a/Runtime/Graphics/FrameBuffer.cpp b/Runtime/Graphics/FrameBuffer.cpp index e675aab..af4d831 100644 --- a/Runtime/Graphics/FrameBuffer.cpp +++ b/Runtime/Graphics/FrameBuffer.cpp @@ -3,10 +3,10 @@ // 有些版本的OpenGL不支持绑定多个RT bool FrameBuffer::BindRenderTexture(RenderTexture* rt, int location /* = 0 */) { - + return false; } bool FrameBuffer::Blit(FrameBuffer* target) { - + return false; } diff --git a/Runtime/Graphics/GpuDataBuffer.cpp b/Runtime/Graphics/GpuDataBuffer.cpp index 0b3e4ee..d20be65 100644 --- a/Runtime/Graphics/GpuDataBuffer.cpp +++ b/Runtime/Graphics/GpuDataBuffer.cpp @@ -193,7 +193,7 @@ namespace GPU return 0; } - DataBuffer* ClaimBuffer(int size = 0, GLenum usage = GL_ARRAY_BUFFER) + DataBuffer* ClaimBuffer(int size, GLenum usage ) { return BufferPool::Instance()->ClaimBuffer(size, usage); } diff --git a/Runtime/Graphics/ImageData.cpp b/Runtime/Graphics/ImageData.cpp index e69de29..8e2e4bf 100644 --- a/Runtime/Graphics/ImageData.cpp +++ b/Runtime/Graphics/ImageData.cpp @@ -0,0 +1,2 @@ +#include "ImageData.h" + diff --git a/Runtime/Graphics/ImageData.h b/Runtime/Graphics/ImageData.h index cd56e83..a3162bc 100644 --- a/Runtime/Graphics/ImageData.h +++ b/Runtime/Graphics/ImageData.h @@ -3,6 +3,7 @@ #include <vector> +// 图片像素数据 class ImageData { public: diff --git a/Runtime/Graphics/Scripting/wrap_Device.cpp b/Runtime/Graphics/Scripting/wrap_Device.cpp deleted file mode 100644 index e69de29..0000000 --- a/Runtime/Graphics/Scripting/wrap_Device.cpp +++ /dev/null diff --git a/Runtime/Graphics/Scripting/wrap_GL.cpp b/Runtime/Graphics/Scripting/wrap_GL.cpp deleted file mode 100644 index e69de29..0000000 --- a/Runtime/Graphics/Scripting/wrap_GL.cpp +++ /dev/null diff --git a/Runtime/Graphics/Scripting/wrap_RenderTexture.cpp b/Runtime/Graphics/Scripting/wrap_RenderTexture.cpp deleted file mode 100644 index e69de29..0000000 --- a/Runtime/Graphics/Scripting/wrap_RenderTexture.cpp +++ /dev/null diff --git a/Runtime/Graphics/Shader.cpp b/Runtime/Graphics/Shader.cpp index e69de29..b8b9e33 100644 --- a/Runtime/Graphics/Shader.cpp +++ b/Runtime/Graphics/Shader.cpp @@ -0,0 +1,146 @@ +#include <exception> +#include "Runtime/Debug/log.h" +#include "Shader.h" +#include "OpenGL.h" + +void checkCompileshaderErrorors(GLuint shader, std::string type) +{ + GLint success; + GLchar infoLog[1024]; + std::string shaderError = ""; + if (type != "PROGRAM") + { + glGetShaderiv(shader, GL_COMPILE_STATUS, &success); + if (!success) + { + glGetShaderInfoLog(shader, 1024, NULL, infoLog); + shaderError = "ERROR::SHADER_COMPILATION_ERROR of type: " + type + "\n" + infoLog; + } + } + else + { + glGetProgramiv(shader, GL_LINK_STATUS, &success); + if (!success) + { + glGetProgramInfoLog(shader, 1024, NULL, infoLog); + shaderError = "ERROR::SHADER_COMPILATION_ERROR of type: " + type + "\n" + infoLog; + } + } + if (!success) + { + throw ShaderCompileExecption(shaderError); + } +} + +Shader::Shader(LuaBind::VM*vm, bool keepSrc) + : NativeClass<Shader>(vm) + , m_KeepSrc(keepSrc) +{ +} + +Shader::Shader(LuaBind::VM* vm, std::string& vert, std::string& frag, bool keepSrc) + : NativeClass<Shader>(vm) + , m_KeepSrc(keepSrc) +{ + const char* vertCode = vert.c_str(); + const char* fragCode = frag.c_str(); + // vertex shader + m_VertID = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(m_VertID, 1, &vertCode, NULL); + glCompileShader(m_VertID); + checkCompileshaderErrorors(m_VertID, "VERTEX"); + // fragment Shader + m_FragID = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(m_FragID, 1, &fragCode, NULL); + glCompileShader(m_FragID); + checkCompileshaderErrorors(m_FragID, "FRAGMENT"); + // create program + m_ProgramID = glCreateProgram(); + glAttachShader(m_ProgramID, m_VertID); + glAttachShader(m_ProgramID, m_FragID); + glLinkProgram(m_ProgramID); + checkCompileshaderErrorors(m_FragID, "PROGRAM"); + // keep src + if (keepSrc) + { + m_VertSrc = vert; + m_FragSrc = frag; + } +} + +Shader::~Shader() +{ + glDeleteProgram(m_ProgramID); + glDeleteShader(m_VertID); + glDeleteShader(m_FragID); +} + +void Shader::ReCompile(std::string& vert, std::string frag) +{ + const char* vertCode = vert.c_str(); + const char* fragCode = frag.c_str(); + // vertex shader + glShaderSource(m_VertID, 1, &vertCode, NULL); + glCompileShader(m_VertID); + checkCompileshaderErrorors(m_VertID, "VERTEX"); + // fragment Shader + glShaderSource(m_FragID, 1, &fragCode, NULL); + glCompileShader(m_FragID); + checkCompileshaderErrorors(m_FragID, "FRAGMENT"); + // create program + glAttachShader(m_ProgramID, m_VertID); + glAttachShader(m_ProgramID, m_FragID); + glLinkProgram(m_ProgramID); + checkCompileshaderErrorors(m_FragID, "PROGRAM"); + // + if (m_KeepSrc) + { + m_VertSrc = vert; + m_FragSrc = frag; + } +} + +void Shader::ReCompileVert(std::string& vert) +{ + glDeleteShader(m_VertID); + const char* vertCode = vert.c_str(); + // vertex shader + glShaderSource(m_VertID, 1, &vertCode, NULL); + glCompileShader(m_VertID); + checkCompileshaderErrorors(m_VertID, "VERTEX"); + // create program + glAttachShader(m_ProgramID, m_VertID); + glAttachShader(m_ProgramID, m_FragID); + glLinkProgram(m_ProgramID); + checkCompileshaderErrorors(m_FragID, "PROGRAM"); + // + if (m_KeepSrc) + { + m_VertSrc = vert; + } +} + +void Shader::ReCompileFrag(std::string frag) +{ + glDeleteShader(m_FragID); + const char* fragCode = frag.c_str(); + // fragment Shader + glShaderSource(m_FragID, 1, &fragCode, NULL); + glCompileShader(m_FragID); + checkCompileshaderErrorors(m_FragID, "FRAGMENT"); + // create program + glAttachShader(m_ProgramID, m_VertID); + glAttachShader(m_ProgramID, m_FragID); + glLinkProgram(m_ProgramID); + checkCompileshaderErrorors(m_FragID, "PROGRAM"); + // + if (m_KeepSrc) + { + m_FragSrc = frag; + } +} + +bool Shader::IsValid() +{ + return m_ProgramID != 0; +}
\ No newline at end of file diff --git a/Runtime/Graphics/Shader.h b/Runtime/Graphics/Shader.h index b22a05f..7cca768 100644 --- a/Runtime/Graphics/Shader.h +++ b/Runtime/Graphics/Shader.h @@ -1,6 +1,60 @@ #pragma once
+#include <string>
+#include <exception>
+#include "Runtime/Graphics/OpenGL.h"
+#include "Runtime/Lua/LuaHelper.h"
+#include "Runtime/Utilities/UtilMacros.h"
+#include "runtime/Debug/Log.h"
-class Shader
+// 着色器程序
+class Shader : public LuaBind::NativeClass<Shader>
{
+public:
+ Shader(LuaBind::VM*vm, bool keepSrc = false);
+ Shader(LuaBind::VM*vm, std::string& vert, std::string& frag, bool keepSrc = false)/*throw(ShaderCompileExecption)*/;
+ ~Shader();
+
+ void ReCompile(std::string& vert, std::string frag)/*throw(ShaderCompileExecption)*/;
+ void ReCompileVert(std::string& vert)/*throw(ShaderCompileExecption)*/;
+ void ReCompileFrag(std::string frag)/*throw(ShaderCompileExecption)*/;
+
+ bool IsValid();
+
+ GET(GLint, ID, m_ProgramID);
+
+private:
+ bool m_KeepSrc;
+
+ std::string m_VertSrc;
+ std::string m_FragSrc;
+
+ GLint m_ProgramID;
+ GLint m_FragID;
+ GLint m_VertID;
+
+ LUA_BIND_DECL_CLASS(Shader);
+
+ LUA_BIND_DECL_METHOD(_New);
+ LUA_BIND_DECL_METHOD(_ReCompile);
+ LUA_BIND_DECL_METHOD(_ReCompileVert);
+ LUA_BIND_DECL_METHOD(_ReCompileFrag);
+ LUA_BIND_DECL_METHOD(_IsValid);
+ LUA_BIND_DECL_METHOD(_GetVertCode);
+ LUA_BIND_DECL_METHOD(_GetFragCode);
};
+
+class ShaderCompileExecption : public std::exception
+{
+public:
+ ShaderCompileExecption(std::string& err)
+ {
+ m_Err = err;
+ }
+ char const* what() const override
+ {
+ return m_Err.c_str();
+ }
+private:
+ std::string m_Err;
+};
\ No newline at end of file diff --git a/Runtime/Graphics/Texture.h b/Runtime/Graphics/Texture.h index ce05ff7..d09429c 100644 --- a/Runtime/Graphics/Texture.h +++ b/Runtime/Graphics/Texture.h @@ -1,6 +1,8 @@ #ifndef TEXTURE_H #define TEXTURE_H +#include "Runtime/Lua/LuaHelper.h" +#include "Runtime/Lua/LuaBind/LuaBind.h" #include "../Utilities/UtilMacros.h" #include "OpenGL.h" #include "ImageData.h" @@ -32,9 +34,9 @@ enum TextureFilter class Texture { public: + Texture(); Texture(ImageData* imgData, TextureFormat format); Texture(ImageData* imgData, TextureFormat format, TextureWrap wrap, TextureFilter filter); - Texture(); ~Texture(); GET(int, Width, m_Width); diff --git a/Runtime/Graphics/VertexBuffer.cpp b/Runtime/Graphics/VertexBuffer.cpp index c9f5164..7861d6f 100644 --- a/Runtime/Graphics/VertexBuffer.cpp +++ b/Runtime/Graphics/VertexBuffer.cpp @@ -1,5 +1,5 @@ #include "VertexBuffer.h" -#include "../Profiler/FrameStats.h" +#include "../Profiling/FrameStats.h" void SetupDefaultVertexArray(const VertexArrayInfo& info); void InvalidateVertexInputCache(); diff --git a/Runtime/Graphics/VertexBuffer.h b/Runtime/Graphics/VertexBuffer.h index 33c0783..9179e0d 100644 --- a/Runtime/Graphics/VertexBuffer.h +++ b/Runtime/Graphics/VertexBuffer.h @@ -69,6 +69,9 @@ private: class SharedVertexBuffer { public: + SharedVertexBuffer(); + ~SharedVertexBuffer(); + void GetChunk(uint attrs, int maxVerts, int maxIndices, RenderMode mode, void **out_vb, void **out_ib); void ReleaseChunk(int actualVerts, int actualIndices); |