2019-08-09 04:18:08 +00:00
|
|
|
|
2019-08-10 00:32:08 +00:00
|
|
|
#include "Precomp.h"
|
2019-08-09 04:18:08 +00:00
|
|
|
#include "RenderDevice.h"
|
2019-08-09 22:46:51 +00:00
|
|
|
#include "VertexBuffer.h"
|
|
|
|
#include "IndexBuffer.h"
|
|
|
|
#include "Texture.h"
|
2019-08-14 05:55:21 +00:00
|
|
|
#include "ShaderManager.h"
|
2019-08-12 06:33:40 +00:00
|
|
|
#include <stdexcept>
|
|
|
|
|
2019-08-18 01:29:33 +00:00
|
|
|
RenderDevice::RenderDevice(void* hwnd) : Context(hwnd)
|
2019-08-12 06:33:40 +00:00
|
|
|
{
|
2019-08-14 10:36:33 +00:00
|
|
|
memset(mUniforms, 0, sizeof(mUniforms));
|
|
|
|
|
2019-08-12 06:33:40 +00:00
|
|
|
if (Context)
|
|
|
|
{
|
|
|
|
Context.Begin();
|
2019-08-16 11:07:57 +00:00
|
|
|
|
|
|
|
glGenVertexArrays(1, &mStreamVAO);
|
2019-08-16 05:45:34 +00:00
|
|
|
glGenBuffers(1, &mStreamVertexBuffer);
|
2019-08-16 11:07:57 +00:00
|
|
|
glBindVertexArray(mStreamVAO);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, mStreamVertexBuffer);
|
|
|
|
VertexBuffer::SetupFlatVAO();
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
|
2019-08-14 05:55:21 +00:00
|
|
|
mShaderManager = std::make_unique<ShaderManager>();
|
2019-08-16 11:07:57 +00:00
|
|
|
|
|
|
|
CheckError();
|
2019-08-12 06:33:40 +00:00
|
|
|
Context.End();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RenderDevice::~RenderDevice()
|
2019-08-09 04:18:08 +00:00
|
|
|
{
|
2019-08-14 05:55:21 +00:00
|
|
|
if (Context)
|
|
|
|
{
|
|
|
|
Context.Begin();
|
2019-08-16 05:45:34 +00:00
|
|
|
glDeleteBuffers(1, &mStreamVertexBuffer);
|
2019-08-16 11:07:57 +00:00
|
|
|
glDeleteVertexArrays(1, &mStreamVAO);
|
2019-08-14 05:55:21 +00:00
|
|
|
mShaderManager->ReleaseResources();
|
|
|
|
Context.End();
|
|
|
|
}
|
2019-08-09 04:18:08 +00:00
|
|
|
}
|
|
|
|
|
2019-08-16 11:07:57 +00:00
|
|
|
void RenderDevice::SetVertexBuffer(VertexBuffer* buffer)
|
2019-08-09 22:46:51 +00:00
|
|
|
{
|
2019-08-16 11:07:57 +00:00
|
|
|
mVertexBuffer = buffer;
|
2019-08-10 05:46:29 +00:00
|
|
|
mNeedApply = true;
|
2019-08-09 22:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice::SetIndexBuffer(IndexBuffer* buffer)
|
|
|
|
{
|
2019-08-10 05:46:29 +00:00
|
|
|
mIndexBuffer = buffer;
|
|
|
|
mNeedApply = true;
|
2019-08-09 22:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice::SetAlphaBlendEnable(bool value)
|
|
|
|
{
|
2019-08-12 06:33:40 +00:00
|
|
|
mAlphaBlend = value;
|
|
|
|
mNeedApply = true;
|
2019-08-09 22:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice::SetAlphaTestEnable(bool value)
|
|
|
|
{
|
2019-08-12 06:33:40 +00:00
|
|
|
mAlphaTest = value;
|
|
|
|
mNeedApply = true;
|
2019-08-09 22:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice::SetCullMode(Cull mode)
|
|
|
|
{
|
2019-08-12 06:33:40 +00:00
|
|
|
mCullMode = mode;
|
|
|
|
mNeedApply = true;
|
2019-08-09 22:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice::SetBlendOperation(BlendOperation op)
|
|
|
|
{
|
2019-08-12 06:33:40 +00:00
|
|
|
mBlendOperation = op;
|
|
|
|
mNeedApply = true;
|
2019-08-09 22:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice::SetSourceBlend(Blend blend)
|
|
|
|
{
|
2019-08-12 06:33:40 +00:00
|
|
|
mSourceBlend = blend;
|
|
|
|
mNeedApply = true;
|
2019-08-09 22:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice::SetDestinationBlend(Blend blend)
|
|
|
|
{
|
2019-08-12 06:33:40 +00:00
|
|
|
mDestinationBlend = blend;
|
|
|
|
mNeedApply = true;
|
2019-08-09 22:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice::SetFillMode(FillMode mode)
|
|
|
|
{
|
2019-08-12 06:33:40 +00:00
|
|
|
mFillMode = mode;
|
|
|
|
mNeedApply = true;
|
2019-08-09 22:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice::SetMultisampleAntialias(bool value)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice::SetZEnable(bool value)
|
|
|
|
{
|
2019-08-12 06:33:40 +00:00
|
|
|
mDepthTest = value;
|
|
|
|
mNeedApply = true;
|
2019-08-09 22:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice::SetZWriteEnable(bool value)
|
|
|
|
{
|
2019-08-12 06:33:40 +00:00
|
|
|
mDepthWrite = value;
|
|
|
|
mNeedApply = true;
|
2019-08-09 22:46:51 +00:00
|
|
|
}
|
|
|
|
|
2019-08-17 01:21:11 +00:00
|
|
|
void RenderDevice::SetTexture(Texture* texture)
|
2019-08-14 11:51:05 +00:00
|
|
|
{
|
2019-08-17 01:21:11 +00:00
|
|
|
mTextureUnit.Tex = texture;
|
2019-08-14 11:51:05 +00:00
|
|
|
mNeedApply = true;
|
|
|
|
}
|
|
|
|
|
2019-08-17 01:21:11 +00:00
|
|
|
void RenderDevice::SetSamplerFilter(TextureFilter minfilter, TextureFilter magfilter, TextureFilter mipfilter, float maxanisotropy)
|
2019-08-14 11:51:05 +00:00
|
|
|
{
|
2019-08-17 01:21:11 +00:00
|
|
|
mTextureUnit.MinFilter = GetGLMinFilter(minfilter, mipfilter);
|
|
|
|
mTextureUnit.MagFilter = (magfilter == TextureFilter::Point || magfilter == TextureFilter::None) ? GL_NEAREST : GL_LINEAR;
|
|
|
|
mTextureUnit.MaxAnisotropy = maxanisotropy;
|
2019-08-14 11:51:05 +00:00
|
|
|
mNeedApply = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLint RenderDevice::GetGLMinFilter(TextureFilter filter, TextureFilter mipfilter)
|
|
|
|
{
|
|
|
|
if (mipfilter == TextureFilter::Linear)
|
|
|
|
{
|
|
|
|
if (filter == TextureFilter::Point || filter == TextureFilter::None)
|
|
|
|
return GL_LINEAR_MIPMAP_NEAREST;
|
|
|
|
else
|
|
|
|
return GL_LINEAR_MIPMAP_LINEAR;
|
|
|
|
}
|
|
|
|
else if (mipfilter == TextureFilter::Point)
|
|
|
|
{
|
|
|
|
if (filter == TextureFilter::Point || filter == TextureFilter::None)
|
|
|
|
return GL_NEAREST_MIPMAP_NEAREST;
|
|
|
|
else
|
|
|
|
return GL_NEAREST_MIPMAP_LINEAR;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (filter == TextureFilter::Point || filter == TextureFilter::None)
|
|
|
|
return GL_NEAREST;
|
|
|
|
else
|
|
|
|
return GL_LINEAR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-17 01:21:11 +00:00
|
|
|
void RenderDevice::SetSamplerState(TextureAddress addressU, TextureAddress addressV, TextureAddress addressW)
|
2019-08-09 22:46:51 +00:00
|
|
|
{
|
2019-08-17 01:21:11 +00:00
|
|
|
mTextureUnit.AddressU = addressU;
|
|
|
|
mTextureUnit.AddressV = addressV;
|
|
|
|
mTextureUnit.AddressW = addressW;
|
2019-08-10 05:46:29 +00:00
|
|
|
mNeedApply = true;
|
2019-08-09 22:46:51 +00:00
|
|
|
}
|
|
|
|
|
2019-08-15 00:52:21 +00:00
|
|
|
void RenderDevice::Draw(PrimitiveType type, int startIndex, int primitiveCount)
|
2019-08-09 22:46:51 +00:00
|
|
|
{
|
2019-08-10 05:46:29 +00:00
|
|
|
static const int modes[] = { GL_LINES, GL_TRIANGLES, GL_TRIANGLE_STRIP };
|
|
|
|
static const int toVertexCount[] = { 2, 3, 1 };
|
|
|
|
static const int toVertexStart[] = { 0, 0, 2 };
|
|
|
|
|
|
|
|
Context.Begin();
|
|
|
|
if (mNeedApply) ApplyChanges();
|
|
|
|
glDrawArrays(modes[(int)type], startIndex, toVertexStart[(int)type] + primitiveCount * toVertexCount[(int)type]);
|
2019-08-17 01:08:34 +00:00
|
|
|
CheckError();
|
2019-08-10 05:46:29 +00:00
|
|
|
Context.End();
|
2019-08-09 22:46:51 +00:00
|
|
|
}
|
|
|
|
|
2019-08-15 00:52:21 +00:00
|
|
|
void RenderDevice::DrawIndexed(PrimitiveType type, int startIndex, int primitiveCount)
|
|
|
|
{
|
|
|
|
static const int modes[] = { GL_LINES, GL_TRIANGLES, GL_TRIANGLE_STRIP };
|
|
|
|
static const int toVertexCount[] = { 2, 3, 1 };
|
|
|
|
static const int toVertexStart[] = { 0, 0, 2 };
|
|
|
|
|
|
|
|
Context.Begin();
|
|
|
|
if (mNeedApply) ApplyChanges();
|
|
|
|
glDrawElements(modes[(int)type], toVertexStart[(int)type] + primitiveCount * toVertexCount[(int)type], GL_UNSIGNED_INT, (const void*)(startIndex * sizeof(uint32_t)));
|
2019-08-17 01:08:34 +00:00
|
|
|
CheckError();
|
2019-08-15 00:52:21 +00:00
|
|
|
Context.End();
|
|
|
|
}
|
|
|
|
|
2019-08-16 11:07:57 +00:00
|
|
|
void RenderDevice::DrawData(PrimitiveType type, int startIndex, int primitiveCount, const void* data)
|
2019-08-09 22:46:51 +00:00
|
|
|
{
|
2019-08-16 05:45:34 +00:00
|
|
|
static const int modes[] = { GL_LINES, GL_TRIANGLES, GL_TRIANGLE_STRIP };
|
|
|
|
static const int toVertexCount[] = { 2, 3, 1 };
|
|
|
|
static const int toVertexStart[] = { 0, 0, 2 };
|
|
|
|
|
|
|
|
int vertcount = toVertexStart[(int)type] + primitiveCount * toVertexCount[(int)type];
|
|
|
|
|
|
|
|
Context.Begin();
|
2019-08-16 11:07:57 +00:00
|
|
|
if (mNeedApply) ApplyChanges();
|
|
|
|
|
2019-08-16 05:45:34 +00:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, mStreamVertexBuffer);
|
2019-08-16 11:07:57 +00:00
|
|
|
glBufferData(GL_ARRAY_BUFFER, vertcount * (size_t)VertexBuffer::FlatStride, static_cast<const uint8_t*>(data) + startIndex * (size_t)VertexBuffer::FlatStride, GL_STREAM_DRAW);
|
|
|
|
glBindVertexArray(mStreamVAO);
|
2019-08-16 05:45:34 +00:00
|
|
|
glDrawArrays(modes[(int)type], 0, vertcount);
|
2019-08-17 01:08:34 +00:00
|
|
|
CheckError();
|
2019-08-16 11:07:57 +00:00
|
|
|
ApplyVertexBuffer();
|
2019-08-17 01:08:34 +00:00
|
|
|
CheckError();
|
2019-08-16 05:45:34 +00:00
|
|
|
Context.End();
|
2019-08-09 22:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice::StartRendering(bool clear, int backcolor, Texture* target, bool usedepthbuffer)
|
|
|
|
{
|
2019-08-10 00:32:08 +00:00
|
|
|
Context.Begin();
|
2019-08-15 12:49:49 +00:00
|
|
|
|
|
|
|
if (target)
|
|
|
|
{
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, target->GetFramebuffer(usedepthbuffer));
|
|
|
|
glViewport(0, 0, target->GetWidth(), target->GetHeight());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
|
glViewport(0, 0, Context.GetWidth(), Context.GetHeight());
|
|
|
|
}
|
|
|
|
|
2019-08-10 00:32:08 +00:00
|
|
|
if (clear && usedepthbuffer)
|
|
|
|
{
|
2019-08-15 12:49:49 +00:00
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
glDepthMask(GL_TRUE);
|
2019-08-10 00:32:08 +00:00
|
|
|
glClearColor(RPART(backcolor) / 255.0f, GPART(backcolor) / 255.0f, BPART(backcolor) / 255.0f, APART(backcolor) / 255.0f);
|
|
|
|
glClearDepthf(1.0f);
|
2019-08-12 06:33:40 +00:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
2019-08-10 00:32:08 +00:00
|
|
|
}
|
|
|
|
else if (clear)
|
|
|
|
{
|
|
|
|
glClearColor(RPART(backcolor) / 255.0f, GPART(backcolor) / 255.0f, BPART(backcolor) / 255.0f, APART(backcolor) / 255.0f);
|
2019-08-12 06:33:40 +00:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
2019-08-10 00:32:08 +00:00
|
|
|
}
|
2019-08-15 12:49:49 +00:00
|
|
|
|
|
|
|
mNeedApply = true;
|
|
|
|
|
2019-08-10 00:32:08 +00:00
|
|
|
Context.End();
|
2019-08-09 22:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice::FinishRendering()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice::Present()
|
|
|
|
{
|
2019-08-10 00:32:08 +00:00
|
|
|
Context.SwapBuffers();
|
2019-08-09 22:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice::ClearTexture(int backcolor, Texture* texture)
|
|
|
|
{
|
2019-08-17 01:08:34 +00:00
|
|
|
StartRendering(true, backcolor, texture, false);
|
|
|
|
FinishRendering();
|
2019-08-09 22:46:51 +00:00
|
|
|
}
|
|
|
|
|
2019-08-17 01:08:34 +00:00
|
|
|
void RenderDevice::CopyTexture(Texture* dst, CubeMapFace face)
|
2019-08-09 22:46:51 +00:00
|
|
|
{
|
2019-08-16 09:24:22 +00:00
|
|
|
static const GLenum facegl[] = {
|
|
|
|
GL_TEXTURE_CUBE_MAP_POSITIVE_X,
|
|
|
|
GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
|
|
|
|
GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
|
|
|
|
GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
|
|
|
|
GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
|
|
|
|
GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
|
|
|
|
};
|
|
|
|
|
2019-08-17 01:08:34 +00:00
|
|
|
Context.Begin();
|
2019-08-15 00:52:21 +00:00
|
|
|
GLint oldTexture = 0;
|
2019-08-16 09:24:22 +00:00
|
|
|
glGetIntegerv(GL_TEXTURE_BINDING_CUBE_MAP, &oldTexture);
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_CUBE_MAP, dst->GetTexture());
|
|
|
|
glCopyTexSubImage2D(facegl[(int)face], 0, 0, 0, 0, 0, dst->GetWidth(), dst->GetHeight());
|
|
|
|
if (face == CubeMapFace::NegativeZ)
|
|
|
|
glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_CUBE_MAP, oldTexture);
|
2019-08-17 01:08:34 +00:00
|
|
|
Context.End();
|
2019-08-15 00:52:21 +00:00
|
|
|
}
|
|
|
|
|
2019-08-16 11:07:57 +00:00
|
|
|
void RenderDevice::SetVertexBufferData(VertexBuffer* buffer, void* data, int64_t size, VertexFormat format)
|
2019-08-15 00:52:21 +00:00
|
|
|
{
|
|
|
|
Context.Begin();
|
2019-08-17 01:08:34 +00:00
|
|
|
CheckError();
|
2019-08-16 11:07:57 +00:00
|
|
|
buffer->Format = format;
|
2019-08-15 00:52:21 +00:00
|
|
|
GLint oldbinding = 0;
|
|
|
|
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &oldbinding);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, buffer->GetBuffer());
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, oldbinding);
|
2019-08-17 01:08:34 +00:00
|
|
|
CheckError();
|
2019-08-15 00:52:21 +00:00
|
|
|
Context.End();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice::SetVertexBufferSubdata(VertexBuffer* buffer, int64_t destOffset, void* data, int64_t size)
|
|
|
|
{
|
|
|
|
Context.Begin();
|
2019-08-17 01:08:34 +00:00
|
|
|
CheckError();
|
2019-08-15 00:52:21 +00:00
|
|
|
GLint oldbinding = 0;
|
|
|
|
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &oldbinding);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, buffer->GetBuffer());
|
|
|
|
glBufferSubData(GL_ARRAY_BUFFER, destOffset, size, data);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, oldbinding);
|
2019-08-17 01:08:34 +00:00
|
|
|
CheckError();
|
2019-08-15 00:52:21 +00:00
|
|
|
Context.End();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice::SetIndexBufferData(IndexBuffer* buffer, void* data, int64_t size)
|
|
|
|
{
|
|
|
|
Context.Begin();
|
2019-08-17 01:08:34 +00:00
|
|
|
CheckError();
|
2019-08-15 00:52:21 +00:00
|
|
|
GLint oldbinding = 0;
|
|
|
|
glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &oldbinding);
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer->GetBuffer());
|
|
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, oldbinding);
|
2019-08-17 01:08:34 +00:00
|
|
|
CheckError();
|
2019-08-15 00:52:21 +00:00
|
|
|
Context.End();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice::SetPixels(Texture* texture, const void* data)
|
|
|
|
{
|
|
|
|
texture->SetPixels(data);
|
|
|
|
InvalidateTexture(texture);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice::SetCubePixels(Texture* texture, CubeMapFace face, const void* data)
|
|
|
|
{
|
|
|
|
texture->SetCubePixels(face, data);
|
|
|
|
InvalidateTexture(texture);
|
|
|
|
}
|
|
|
|
|
|
|
|
void* RenderDevice::LockTexture(Texture* texture)
|
|
|
|
{
|
|
|
|
return texture->Lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice::UnlockTexture(Texture* texture)
|
|
|
|
{
|
|
|
|
texture->Unlock();
|
|
|
|
InvalidateTexture(texture);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice::InvalidateTexture(Texture* texture)
|
|
|
|
{
|
|
|
|
if (texture->IsTextureCreated())
|
|
|
|
{
|
|
|
|
Context.Begin();
|
|
|
|
texture->Invalidate();
|
|
|
|
Context.End();
|
|
|
|
mNeedApply = true;
|
|
|
|
}
|
2019-08-09 22:46:51 +00:00
|
|
|
}
|
|
|
|
|
2019-08-12 06:33:40 +00:00
|
|
|
void RenderDevice::CheckError()
|
|
|
|
{
|
|
|
|
GLenum error = glGetError();
|
|
|
|
if (error != GL_NO_ERROR)
|
|
|
|
throw std::runtime_error("OpenGL error!");
|
|
|
|
}
|
|
|
|
|
2019-08-16 02:10:03 +00:00
|
|
|
Shader* RenderDevice::GetActiveShader()
|
|
|
|
{
|
|
|
|
if (mAlphaTest)
|
|
|
|
return &mShaderManager->AlphaTestShaders[(int)mShaderName];
|
|
|
|
else
|
|
|
|
return &mShaderManager->Shaders[(int)mShaderName];
|
|
|
|
}
|
|
|
|
|
2019-08-10 05:46:29 +00:00
|
|
|
void RenderDevice::ApplyChanges()
|
|
|
|
{
|
2019-08-17 01:08:34 +00:00
|
|
|
CheckError();
|
2019-08-12 06:33:40 +00:00
|
|
|
ApplyShader();
|
2019-08-17 01:08:34 +00:00
|
|
|
CheckError();
|
2019-08-16 11:07:57 +00:00
|
|
|
ApplyVertexBuffer();
|
2019-08-17 01:08:34 +00:00
|
|
|
CheckError();
|
2019-08-10 05:46:29 +00:00
|
|
|
ApplyIndexBuffer();
|
2019-08-17 01:08:34 +00:00
|
|
|
CheckError();
|
2019-08-14 10:36:33 +00:00
|
|
|
ApplyUniforms();
|
2019-08-17 01:08:34 +00:00
|
|
|
CheckError();
|
2019-08-10 05:46:29 +00:00
|
|
|
ApplyTextures();
|
2019-08-17 01:08:34 +00:00
|
|
|
CheckError();
|
2019-08-12 06:33:40 +00:00
|
|
|
ApplyRasterizerState();
|
2019-08-17 01:08:34 +00:00
|
|
|
CheckError();
|
2019-08-12 06:33:40 +00:00
|
|
|
ApplyBlendState();
|
2019-08-17 01:08:34 +00:00
|
|
|
CheckError();
|
2019-08-12 06:33:40 +00:00
|
|
|
ApplyDepthState();
|
|
|
|
|
|
|
|
CheckError();
|
2019-08-10 05:46:29 +00:00
|
|
|
|
|
|
|
mNeedApply = false;
|
|
|
|
}
|
|
|
|
|
2019-08-12 06:33:40 +00:00
|
|
|
void RenderDevice::ApplyShader()
|
|
|
|
{
|
2019-08-16 02:10:03 +00:00
|
|
|
glUseProgram(GetActiveShader()->GetProgram());
|
2019-08-12 06:33:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice::ApplyRasterizerState()
|
|
|
|
{
|
|
|
|
if (mCullMode == Cull::None)
|
|
|
|
{
|
|
|
|
glDisable(GL_CULL_FACE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glEnable(GL_CULL_FACE);
|
2019-08-15 12:49:49 +00:00
|
|
|
glFrontFace(GL_CW);
|
2019-08-12 06:33:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GLenum fillMode2GL[] = { GL_FILL, GL_LINE };
|
|
|
|
glPolygonMode(GL_FRONT_AND_BACK, fillMode2GL[(int)mFillMode]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice::ApplyBlendState()
|
|
|
|
{
|
|
|
|
if (mAlphaBlend)
|
|
|
|
{
|
|
|
|
static const GLenum blendOp2GL[] = { GL_FUNC_ADD, GL_FUNC_REVERSE_SUBTRACT };
|
2019-08-17 03:21:28 +00:00
|
|
|
static const GLenum blendFunc2GL[] = { GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA, GL_ONE };
|
2019-08-12 06:33:40 +00:00
|
|
|
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glBlendEquation(blendOp2GL[(int)mBlendOperation]);
|
|
|
|
glBlendFunc(blendFunc2GL[(int)mSourceBlend], blendFunc2GL[(int)mDestinationBlend]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glDisable(GL_BLEND);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice::ApplyDepthState()
|
|
|
|
{
|
|
|
|
if (mDepthTest)
|
|
|
|
{
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
2019-08-17 03:21:28 +00:00
|
|
|
glDepthFunc(GL_LEQUAL);
|
2019-08-12 06:33:40 +00:00
|
|
|
glDepthMask(mDepthWrite ? GL_TRUE : GL_FALSE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glDisable(GL_DEPTH_TEST);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-10 05:46:29 +00:00
|
|
|
void RenderDevice::ApplyIndexBuffer()
|
|
|
|
{
|
|
|
|
if (mIndexBuffer)
|
|
|
|
{
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer->GetBuffer());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-16 11:07:57 +00:00
|
|
|
void RenderDevice::ApplyVertexBuffer()
|
2019-08-10 05:46:29 +00:00
|
|
|
{
|
2019-08-16 11:07:57 +00:00
|
|
|
if (mVertexBuffer)
|
|
|
|
glBindVertexArray(mVertexBuffer->GetVAO());
|
2019-08-10 05:46:29 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 10:36:33 +00:00
|
|
|
void RenderDevice::SetShader(ShaderName name)
|
|
|
|
{
|
2019-08-16 02:10:03 +00:00
|
|
|
mShaderName = name;
|
2019-08-14 10:36:33 +00:00
|
|
|
mNeedApply = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const int uniformLocations[(int)UniformName::NumUniforms] = {
|
|
|
|
64, // rendersettings
|
|
|
|
0, // transformsettings
|
|
|
|
108, // desaturation
|
|
|
|
80, // highlightcolor
|
|
|
|
16, // worldviewproj
|
|
|
|
32, // world
|
|
|
|
48, // modelnormal
|
|
|
|
68, // FillColor
|
|
|
|
72, // vertexColor
|
|
|
|
84, // stencilColor
|
|
|
|
92, // lightPosAndRadius
|
|
|
|
96, // lightOrientation
|
|
|
|
100, // light2Radius
|
|
|
|
104, // lightColor
|
|
|
|
109, // ignoreNormals
|
|
|
|
110, // spotLight
|
2019-08-16 12:01:27 +00:00
|
|
|
76, // campos,
|
|
|
|
112, // texturefactor
|
2019-08-14 10:36:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void RenderDevice::SetUniform(UniformName name, const void* values, int count)
|
|
|
|
{
|
|
|
|
memcpy(&mUniforms[uniformLocations[(int)name]], values, sizeof(float) * count);
|
|
|
|
mNeedApply = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice::ApplyUniforms()
|
|
|
|
{
|
2019-08-16 02:10:03 +00:00
|
|
|
Shader* shader = GetActiveShader();
|
|
|
|
auto& locations = shader->UniformLocations;
|
2019-08-14 10:36:33 +00:00
|
|
|
|
|
|
|
glUniformMatrix4fv(locations[(int)UniformName::transformsettings], 1, GL_FALSE, &mUniforms[0].valuef);
|
|
|
|
glUniformMatrix4fv(locations[(int)UniformName::worldviewproj], 1, GL_FALSE, &mUniforms[16].valuef);
|
|
|
|
glUniformMatrix4fv(locations[(int)UniformName::world], 1, GL_FALSE, &mUniforms[32].valuef);
|
|
|
|
glUniformMatrix4fv(locations[(int)UniformName::modelnormal], 1, GL_FALSE, &mUniforms[48].valuef);
|
|
|
|
|
|
|
|
glUniform4fv(locations[(int)UniformName::rendersettings], 1, &mUniforms[64].valuef);
|
|
|
|
glUniform4fv(locations[(int)UniformName::FillColor], 1, &mUniforms[68].valuef);
|
|
|
|
glUniform4fv(locations[(int)UniformName::vertexColor], 1, &mUniforms[72].valuef);
|
|
|
|
glUniform4fv(locations[(int)UniformName::campos], 1, &mUniforms[76].valuef);
|
|
|
|
glUniform4fv(locations[(int)UniformName::highlightcolor], 1, &mUniforms[80].valuef);
|
|
|
|
glUniform4fv(locations[(int)UniformName::stencilColor], 1, &mUniforms[84].valuef);
|
|
|
|
glUniform4fv(locations[(int)UniformName::lightColor], 1, &mUniforms[88].valuef);
|
|
|
|
glUniform4fv(locations[(int)UniformName::lightPosAndRadius], 1, &mUniforms[92].valuef);
|
|
|
|
glUniform3fv(locations[(int)UniformName::lightOrientation], 1, &mUniforms[96].valuef);
|
|
|
|
glUniform2fv(locations[(int)UniformName::light2Radius], 1, &mUniforms[100].valuef);
|
|
|
|
glUniform4fv(locations[(int)UniformName::lightColor], 1, &mUniforms[104].valuef);
|
|
|
|
|
|
|
|
glUniform1fv(locations[(int)UniformName::desaturation], 1, &mUniforms[108].valuef);
|
|
|
|
glUniform1fv(locations[(int)UniformName::ignoreNormals], 1, &mUniforms[109].valuef);
|
|
|
|
glUniform1fv(locations[(int)UniformName::spotLight], 1, &mUniforms[110].valuef);
|
2019-08-16 02:10:03 +00:00
|
|
|
|
2019-08-16 12:01:27 +00:00
|
|
|
glUniform4fv(locations[(int)UniformName::texturefactor], 1, &mUniforms[112].valuef);
|
|
|
|
|
2019-08-16 02:10:03 +00:00
|
|
|
for (int i = 0; i < Shader::MaxSamplers; i++)
|
|
|
|
glUniform1i(shader->SamplerLocations[i], i);
|
2019-08-14 10:36:33 +00:00
|
|
|
}
|
|
|
|
|
2019-08-10 05:46:29 +00:00
|
|
|
void RenderDevice::ApplyTextures()
|
|
|
|
{
|
|
|
|
static const int wrapMode[] = { GL_REPEAT, GL_CLAMP_TO_EDGE };
|
|
|
|
|
2019-08-17 01:21:11 +00:00
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
if (mTextureUnit.Tex)
|
2019-08-10 05:46:29 +00:00
|
|
|
{
|
2019-08-17 01:21:11 +00:00
|
|
|
GLenum target = mTextureUnit.Tex->IsCubeTexture() ? GL_TEXTURE_CUBE_MAP : GL_TEXTURE_2D;
|
|
|
|
|
|
|
|
glBindTexture(target, mTextureUnit.Tex->GetTexture());
|
|
|
|
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, mTextureUnit.MinFilter);
|
|
|
|
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, mTextureUnit.MagFilter);
|
|
|
|
glTexParameteri(target, GL_TEXTURE_WRAP_S, wrapMode[(int)mTextureUnit.AddressU]);
|
|
|
|
glTexParameteri(target, GL_TEXTURE_WRAP_T, wrapMode[(int)mTextureUnit.AddressV]);
|
|
|
|
glTexParameteri(target, GL_TEXTURE_WRAP_R, wrapMode[(int)mTextureUnit.AddressW]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
2019-08-10 05:46:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-09 22:46:51 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-08-18 01:29:33 +00:00
|
|
|
RenderDevice* RenderDevice_New(void* hwnd)
|
2019-08-10 00:32:08 +00:00
|
|
|
{
|
|
|
|
RenderDevice *device = new RenderDevice(hwnd);
|
|
|
|
if (!device->Context)
|
|
|
|
{
|
|
|
|
delete device;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return device;
|
|
|
|
}
|
2019-08-09 04:18:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice_Delete(RenderDevice* device)
|
|
|
|
{
|
|
|
|
delete device;
|
|
|
|
}
|
2019-08-09 22:46:51 +00:00
|
|
|
|
2019-08-14 10:36:33 +00:00
|
|
|
void RenderDevice_SetShader(RenderDevice* device, ShaderName name)
|
|
|
|
{
|
|
|
|
device->SetShader(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice_SetUniform(RenderDevice* device, UniformName name, const void* values, int count)
|
|
|
|
{
|
|
|
|
device->SetUniform(name, values, count);
|
|
|
|
}
|
|
|
|
|
2019-08-16 11:07:57 +00:00
|
|
|
void RenderDevice_SetVertexBuffer(RenderDevice* device, VertexBuffer* buffer)
|
2019-08-09 22:46:51 +00:00
|
|
|
{
|
2019-08-16 11:07:57 +00:00
|
|
|
device->SetVertexBuffer(buffer);
|
2019-08-09 22:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice_SetIndexBuffer(RenderDevice* device, IndexBuffer* buffer)
|
|
|
|
{
|
|
|
|
device->SetIndexBuffer(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice_SetAlphaBlendEnable(RenderDevice* device, bool value)
|
|
|
|
{
|
|
|
|
device->SetAlphaBlendEnable(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice_SetAlphaTestEnable(RenderDevice* device, bool value)
|
|
|
|
{
|
|
|
|
device->SetAlphaTestEnable(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice_SetCullMode(RenderDevice* device, Cull mode)
|
|
|
|
{
|
|
|
|
device->SetCullMode(mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice_SetBlendOperation(RenderDevice* device, BlendOperation op)
|
|
|
|
{
|
|
|
|
device->SetBlendOperation(op);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice_SetSourceBlend(RenderDevice* device, Blend blend)
|
|
|
|
{
|
|
|
|
device->SetSourceBlend(blend);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice_SetDestinationBlend(RenderDevice* device, Blend blend)
|
|
|
|
{
|
|
|
|
device->SetDestinationBlend(blend);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice_SetFillMode(RenderDevice* device, FillMode mode)
|
|
|
|
{
|
|
|
|
device->SetFillMode(mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice_SetMultisampleAntialias(RenderDevice* device, bool value)
|
|
|
|
{
|
|
|
|
device->SetMultisampleAntialias(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice_SetZEnable(RenderDevice* device, bool value)
|
|
|
|
{
|
|
|
|
device->SetZEnable(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice_SetZWriteEnable(RenderDevice* device, bool value)
|
|
|
|
{
|
|
|
|
device->SetZWriteEnable(value);
|
|
|
|
}
|
|
|
|
|
2019-08-17 01:21:11 +00:00
|
|
|
void RenderDevice_SetTexture(RenderDevice* device, Texture* texture)
|
2019-08-14 11:51:05 +00:00
|
|
|
{
|
2019-08-17 01:21:11 +00:00
|
|
|
device->SetTexture(texture);
|
2019-08-14 11:51:05 +00:00
|
|
|
}
|
|
|
|
|
2019-08-17 01:21:11 +00:00
|
|
|
void RenderDevice_SetSamplerFilter(RenderDevice* device, TextureFilter minfilter, TextureFilter magfilter, TextureFilter mipfilter, float maxanisotropy)
|
2019-08-14 11:51:05 +00:00
|
|
|
{
|
2019-08-17 01:21:11 +00:00
|
|
|
device->SetSamplerFilter(minfilter, magfilter, mipfilter, maxanisotropy);
|
2019-08-14 11:51:05 +00:00
|
|
|
}
|
|
|
|
|
2019-08-17 01:21:11 +00:00
|
|
|
void RenderDevice_SetSamplerState(RenderDevice* device, TextureAddress addressU, TextureAddress addressV, TextureAddress addressW)
|
2019-08-09 22:46:51 +00:00
|
|
|
{
|
2019-08-17 01:21:11 +00:00
|
|
|
device->SetSamplerState(addressU, addressV, addressW);
|
2019-08-09 22:46:51 +00:00
|
|
|
}
|
|
|
|
|
2019-08-15 00:52:21 +00:00
|
|
|
void RenderDevice_Draw(RenderDevice* device, PrimitiveType type, int startIndex, int primitiveCount)
|
2019-08-09 22:46:51 +00:00
|
|
|
{
|
2019-08-15 00:52:21 +00:00
|
|
|
device->Draw(type, startIndex, primitiveCount);
|
2019-08-09 22:46:51 +00:00
|
|
|
}
|
|
|
|
|
2019-08-15 00:52:21 +00:00
|
|
|
void RenderDevice_DrawIndexed(RenderDevice* device, PrimitiveType type, int startIndex, int primitiveCount)
|
2019-08-09 22:46:51 +00:00
|
|
|
{
|
2019-08-15 00:52:21 +00:00
|
|
|
device->DrawIndexed(type, startIndex, primitiveCount);
|
|
|
|
}
|
|
|
|
|
2019-08-16 11:07:57 +00:00
|
|
|
void RenderDevice_DrawData(RenderDevice* device, PrimitiveType type, int startIndex, int primitiveCount, const void* data)
|
2019-08-09 22:46:51 +00:00
|
|
|
{
|
2019-08-16 11:07:57 +00:00
|
|
|
device->DrawData(type, startIndex, primitiveCount, data);
|
2019-08-09 22:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice_StartRendering(RenderDevice* device, bool clear, int backcolor, Texture* target, bool usedepthbuffer)
|
|
|
|
{
|
|
|
|
device->StartRendering(clear, backcolor, target, usedepthbuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice_FinishRendering(RenderDevice* device)
|
|
|
|
{
|
|
|
|
device->FinishRendering();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice_Present(RenderDevice* device)
|
|
|
|
{
|
|
|
|
device->Present();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice_ClearTexture(RenderDevice* device, int backcolor, Texture* texture)
|
|
|
|
{
|
|
|
|
device->ClearTexture(backcolor, texture);
|
|
|
|
}
|
|
|
|
|
2019-08-17 01:08:34 +00:00
|
|
|
void RenderDevice_CopyTexture(RenderDevice* device, Texture* dst, CubeMapFace face)
|
2019-08-09 22:46:51 +00:00
|
|
|
{
|
2019-08-17 01:08:34 +00:00
|
|
|
device->CopyTexture(dst, face);
|
2019-08-09 22:46:51 +00:00
|
|
|
}
|
2019-08-15 00:52:21 +00:00
|
|
|
|
2019-08-16 11:07:57 +00:00
|
|
|
void RenderDevice_SetVertexBufferData(RenderDevice* device, VertexBuffer* buffer, void* data, int64_t size, VertexFormat format)
|
2019-08-15 00:52:21 +00:00
|
|
|
{
|
2019-08-16 11:07:57 +00:00
|
|
|
device->SetVertexBufferData(buffer, data, size, format);
|
2019-08-15 00:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice_SetVertexBufferSubdata(RenderDevice* device, VertexBuffer* buffer, int64_t destOffset, void* data, int64_t size)
|
|
|
|
{
|
|
|
|
device->SetVertexBufferSubdata(buffer, destOffset, data, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice_SetIndexBufferData(RenderDevice* device, IndexBuffer* buffer, void* data, int64_t size)
|
|
|
|
{
|
|
|
|
device->SetIndexBufferData(buffer, data, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice_SetPixels(RenderDevice* device, Texture* texture, const void* data)
|
|
|
|
{
|
|
|
|
device->SetPixels(texture, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice_SetCubePixels(RenderDevice* device, Texture* texture, CubeMapFace face, const void* data)
|
|
|
|
{
|
|
|
|
device->SetCubePixels(texture, face, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void* RenderDevice_LockTexture(RenderDevice* device, Texture* texture)
|
|
|
|
{
|
|
|
|
return device->LockTexture(texture);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderDevice_UnlockTexture(RenderDevice* device, Texture* texture)
|
|
|
|
{
|
|
|
|
device->UnlockTexture(texture);
|
|
|
|
}
|