summaryrefslogtreecommitdiff
path: root/Runtime
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2021-10-29 15:02:46 +0800
committerchai <chaifix@163.com>2021-10-29 15:02:46 +0800
commit796b4b05ec62eb5d58a634854998f485072e8a2b (patch)
tree30f188382f14b3b9c69fe5846262a39a8b4664cc /Runtime
parent91c32cb173201ac8803a1e4452e8342969b8e484 (diff)
*passing texture to glsl
Diffstat (limited to 'Runtime')
-rw-r--r--Runtime/Graphics/GfxDevice.cpp24
-rw-r--r--Runtime/Graphics/GfxDevice.h3
-rw-r--r--Runtime/Graphics/Shader.h1
-rw-r--r--Runtime/Scripting/GL/GL.bind.cpp16
-rw-r--r--Runtime/Scripting/Rendering/Rendering.bind.cpp8
-rw-r--r--Runtime/Scripting/Rendering/Shader.bind.cpp19
6 files changed, 61 insertions, 10 deletions
diff --git a/Runtime/Graphics/GfxDevice.cpp b/Runtime/Graphics/GfxDevice.cpp
index e4747d8..bd8dd9d 100644
--- a/Runtime/Graphics/GfxDevice.cpp
+++ b/Runtime/Graphics/GfxDevice.cpp
@@ -1,7 +1,11 @@
+#include <vector>
#include "GfxDevice.h"
static bool deviceInited = false;
+static const std::vector<byte> s_AvailableTextureUnitPreset = {0,1,2,3,4,5,6,7}; // 最多支持8个贴图
+static std::vector<byte> s_AvailableTextureUnit = s_AvailableTextureUnitPreset;
+
GfxDevice g_GfxDevice;
GfxDevice::GfxDevice()
@@ -121,6 +125,26 @@ void GfxDevice::SetUniformMat4(const char* name, Internal::Matrix44 mat4)
glUniformMatrix4fv(loc, 1, GL_TRUE, &mat4.m[0][0]);
}
+void GfxDevice::SetUniformTexture(const char* name, Texture* texture)
+{
+ if (s_AvailableTextureUnit.size() == 0)
+ {
+ log_error("No available texture unit. Too many textures or forget invoke ResetUniformsState()");
+ return;
+ }
+ int texUnit = s_AvailableTextureUnit.back();
+ s_AvailableTextureUnit.pop_back();
+ glActiveTexture(GL_TEXTURE0 + texUnit);
+ glBindTexture(GL_TEXTURE_2D, texture->GetGpuID());
+ GLint loc = glGetUniformLocation(m_Shader.GetID(), name);
+ glUniform1i(loc, texUnit);
+}
+
+void GfxDevice::ResetUniformsState()
+{
+ s_AvailableTextureUnit = s_AvailableTextureUnitPreset;
+}
+
void GfxDevice::BeginFrame()
{
m_IsInsideFrame = true;
diff --git a/Runtime/Graphics/GfxDevice.h b/Runtime/Graphics/GfxDevice.h
index ef7788c..68cd6c0 100644
--- a/Runtime/Graphics/GfxDevice.h
+++ b/Runtime/Graphics/GfxDevice.h
@@ -67,6 +67,9 @@ public:
void SetUniformVec3(const char* name, Internal::Vector3 vec3);
void SetUniformVec4(const char* name, Internal::Vector4 vec4);
void SetUniformMat4(const char* name, Internal::Matrix44 mat4);
+ void SetUniformTexture(const char* name, Texture* texture);
+
+ void ResetUniformsState();
void BeginFrame();
void EndFrame();
diff --git a/Runtime/Graphics/Shader.h b/Runtime/Graphics/Shader.h
index 4e0dc37..8a8c1f5 100644
--- a/Runtime/Graphics/Shader.h
+++ b/Runtime/Graphics/Shader.h
@@ -46,6 +46,7 @@ private:
LUA_BIND_DECL_METHOD(_SetVector4);
LUA_BIND_DECL_METHOD(_SetMatrix3);
LUA_BIND_DECL_METHOD(_SetMatrix4);
+ LUA_BIND_DECL_METHOD(_SetTexture);
//LUA_BIND_DECL_METHOD(_SetColor);
};
diff --git a/Runtime/Scripting/GL/GL.bind.cpp b/Runtime/Scripting/GL/GL.bind.cpp
index 903e706..40093e9 100644
--- a/Runtime/Scripting/GL/GL.bind.cpp
+++ b/Runtime/Scripting/GL/GL.bind.cpp
@@ -54,10 +54,10 @@ int ClearColor(lua_State* L)
LUA_BIND_STATE(L);
if (LuaHelper::IsType(state, "GameLab.Engine.Rendering.Color", -1))
{
- float r = state.GetField(-1, "r", 0);
- float g = state.GetField(-1, "g", 0);
- float b = state.GetField(-1, "b", 0);
- float a = state.GetField(-1, "a", 0);
+ float r = state.GetField<float>(-1, "r", 0);
+ float g = state.GetField<float>(-1, "g", 0);
+ float b = state.GetField<float>(-1, "b", 0);
+ float a = state.GetField<float>(-1, "a", 0);
glClearColor(r, g, b, a);
}
else if (LuaHelper::IsType(state, "GameLab.Engine.Rendering.Color32", -1))
@@ -70,10 +70,10 @@ int ClearColor(lua_State* L)
}
else if (LuaHelper::IsType(state, "table", -1))
{
- float r = state.GetField(-1, 1, 0);
- float g = state.GetField(-1, 2, 0);
- float b = state.GetField(-1, 3, 0);
- float a = state.GetField(-1, 4, 0);
+ float r = state.GetField<float>(-1, 1, 0);
+ float g = state.GetField<float>(-1, 2, 0);
+ float b = state.GetField<float>(-1, 3, 0);
+ float a = state.GetField<float>(-1, 4, 0);
glClearColor(r, g, b, a);
}
else
diff --git a/Runtime/Scripting/Rendering/Rendering.bind.cpp b/Runtime/Scripting/Rendering/Rendering.bind.cpp
index 197ff0a..cb294a3 100644
--- a/Runtime/Scripting/Rendering/Rendering.bind.cpp
+++ b/Runtime/Scripting/Rendering/Rendering.bind.cpp
@@ -2,6 +2,7 @@
#include "Runtime/Graphics/Texture.h"
#include "Runtime/Graphics/ImageData.h"
#include "Runtime/Graphics/UIQuad.h"
+#include "Runtime/Graphics/GfxDevice.h"
// Rendering.DrawUIQuad({})
static int DrawUIQuad(lua_State* L)
@@ -13,8 +14,15 @@ static int DrawUIQuad(lua_State* L)
return 0;
}
+static int ResetUniformState(lua_State* L)
+{
+ g_GfxDevice.ResetUniformsState();
+ return 0;
+}
+
static luaL_Reg funcs[] = {
{"DrawUIQuad", DrawUIQuad},
+ {"ResetUniformState", ResetUniformState},
{0, 0}
};
diff --git a/Runtime/Scripting/Rendering/Shader.bind.cpp b/Runtime/Scripting/Rendering/Shader.bind.cpp
index 89f1ebb..83f8477 100644
--- a/Runtime/Scripting/Rendering/Shader.bind.cpp
+++ b/Runtime/Scripting/Rendering/Shader.bind.cpp
@@ -15,12 +15,13 @@ LUA_BIND_REGISTRY(Shader)
{ "Use", _Use },
{ "Unuse", _UnUse },
//{ "SetColor", _SetColor },
+ { "SetTexture", _SetTexture },
{ "SetVector2", _SetVector2 },
{ "SetVector3", _SetVector3 },
{ "SetVector4", _SetVector4 },
//{ "SetMatrix3", _SetMatrix3 },
{ "SetMatrix44", _SetMatrix4 }
- );
+ );
}
LUA_BIND_POSTPROCESS(Shader)
@@ -160,4 +161,18 @@ LUA_BIND_IMPL_METHOD(Shader, _SetMatrix4)
g_GfxDevice.SetUniformMat4(name, m4);
return 1;
-} \ No newline at end of file
+}
+
+// shader:SetTexture(name,texture)
+LUA_BIND_IMPL_METHOD(Shader, _SetTexture)
+{
+ LUA_BIND_PREPARE(L, Shader);
+ LUA_BIND_CHECK(L, "USU");
+
+ cc8* name = state.GetValue(2, "");
+ Texture* tex = state.GetUserdata<Texture>();
+
+ g_GfxDevice.SetUniformTexture(name, tex);
+
+ return 1;
+}