summaryrefslogtreecommitdiff
path: root/Runtime/Graphics/GfxDevice.cpp
blob: f83a9b472db02aee3ace3e40273c093b0d1945ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <vector>
#include "GfxDevice.h"
#include "Runtime/Math/Math.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_TextureUnitBucket = s_AvailableTextureUnitPreset;

static const int kMaxAvailableTextureUnitCount = 8; // 最多支持8个贴图
static int s_CurAvailableTextureUnit = 0;

static int ClaimTextureUnit()
{
    int unit = s_CurAvailableTextureUnit;
    s_CurAvailableTextureUnit = (s_CurAvailableTextureUnit + 1) % kMaxAvailableTextureUnitCount;
    return unit;
}

GfxDevice g_GfxDevice;

GfxDevice::GfxDevice()
{
	Assert(!deviceInited);
	deviceInited = true;
}

GfxDevice::~GfxDevice()
{
}

void GfxDevice::Initialize(GfxDeviceSetting setting)
{
}

void GfxDevice::Enable(EDeviceEnable enabled)
{

}

void GfxDevice::Disable(EDeviceEnable enabled)
{

}

void GfxDevice::IsEnable(uint flag)
{

}

void GfxDevice::SetDepthTest(EDepthTest testing)
{

}

void GfxDevice::SetCullFace(ECullFace face)
{

}

void GfxDevice::SetStencilMask(byte stencilMask)
{

}

void GfxDevice::SetStencilOp(EStencilOp op)
{

}

void GfxDevice::SetAntiAliasing(int level /*= 0*/)
{

}

void GfxDevice::Clear(int clearFlag)
{

}

void GfxDevice::UseShader(LuaBind::State& state, Shader* shader, int idx)
{
    if (shader == NULL)
        return;

    GLuint id = shader->GetID();
    if (id == 0)
        return;

    glUseProgram(id);

	shader->ExecuteCommand();

    m_Shader.shader = shader;
    m_Shader.ref.SetRef(state, idx);
}

void GfxDevice::UnuseShader()
{
    if (m_Shader)
    {
        m_Shader.shader = NULL;
        m_Shader.ref.UnRef();
    }
    glUseProgram(0);
}

void GfxDevice::SetUniformVec2(const char* name, Vector2 vec2)
{
    if (!m_Shader)
        return;
    GLint loc = glGetUniformLocation(m_Shader.GetID(), name);
    glUniform2f(loc, vec2.x, vec2.y);
}

void GfxDevice::SetUniformVec3(const char* name, Vector3 vec3)
{
    if (!m_Shader)
        return;
    GLint loc = glGetUniformLocation(m_Shader.GetID(), name);
    glUniform3f(loc, vec3.x, vec3.y, vec3.z);
}

void GfxDevice::SetUniformVec4(const char* name, Vector4 vec4)
{
    if (!m_Shader)
        return;
    GLint loc = glGetUniformLocation(m_Shader.GetID(), name);
    glUniform4f(loc, vec4.x, vec4.y, vec4.z, vec4.w);
}

void GfxDevice::SetUniformMat4(const char* name, Matrix44 mat4)
{
    if (!m_Shader)
        return;
    GLint loc = glGetUniformLocation(m_Shader.GetID(), name);
    glUniformMatrix4fv(loc, 1, GL_TRUE, &mat4.m[0][0]);
}

void GfxDevice::SetUniformTexture(const char* name, Texture* texture)
{
    int texUnit = ClaimTextureUnit();
    glActiveTexture(GL_TEXTURE0 + texUnit);
	glBindTexture(GL_TEXTURE_2D, texture->GetGpuID());

    GLint loc = glGetUniformLocation(m_Shader.GetID(), name);
    glUniform1i(loc, texUnit);
}

void GfxDevice::BeginFrame()
{
	m_IsInsideFrame = true;
	
}

void GfxDevice::EndFrame()
{	
	//GPU::BufferPool::Instance()->OnEndFrame();

	m_IsInsideFrame = false;
}

void GfxDevice::PresentFrame()
{
// swap buffers
}

bool GfxDevice::IsInsideFrame()
{
	return m_IsInsideFrame;
}