summaryrefslogtreecommitdiff
path: root/Runtime/Graphics/GfxDevice.cpp
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/Graphics/GfxDevice.cpp
parent91c32cb173201ac8803a1e4452e8342969b8e484 (diff)
*passing texture to glsl
Diffstat (limited to 'Runtime/Graphics/GfxDevice.cpp')
-rw-r--r--Runtime/Graphics/GfxDevice.cpp24
1 files changed, 24 insertions, 0 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;