2019-12-16 04:54:44 +00:00
|
|
|
|
|
2019-12-15 21:53:33 +00:00
|
|
|
|
#include "Precomp.h"
|
|
|
|
|
#include "RenderDevice.h"
|
|
|
|
|
#include "VertexBuffer.h"
|
|
|
|
|
#include "IndexBuffer.h"
|
|
|
|
|
#include "Texture.h"
|
|
|
|
|
#include "ShaderManager.h"
|
|
|
|
|
#include <stdexcept>
|
|
|
|
|
#include <cstdarg>
|
2019-12-18 01:27:49 +00:00
|
|
|
|
#include <algorithm>
|
2019-12-19 02:12:44 +00:00
|
|
|
|
#include <cmath>
|
|
|
|
|
#include "fasttrig.h"
|
|
|
|
|
|
|
|
|
|
#ifndef NO_SSE
|
|
|
|
|
#include <xmmintrin.h>
|
|
|
|
|
#endif
|
2019-12-15 21:53:33 +00:00
|
|
|
|
|
2019-12-16 04:54:44 +00:00
|
|
|
|
static bool GLLogStarted = false;
|
|
|
|
|
static void APIENTRY GLLogCallback(GLenum source, GLenum type, GLuint id,
|
|
|
|
|
GLenum severity, GLsizei length, const GLchar* message, const void* userParam)
|
|
|
|
|
{
|
|
|
|
|
FILE* f = fopen("OpenGLDebug.log", GLLogStarted ? "a" : "w");
|
|
|
|
|
if (!f) return;
|
|
|
|
|
GLLogStarted = true;
|
|
|
|
|
fprintf(f, "%s\r\n", message);
|
|
|
|
|
fclose(f);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-15 21:53:33 +00:00
|
|
|
|
RenderDevice::RenderDevice(void* disp, void* window)
|
|
|
|
|
{
|
2019-12-18 03:22:47 +00:00
|
|
|
|
DeclareUniform(UniformName::projection, "projection", UniformType::Matrix);
|
|
|
|
|
DeclareUniform(UniformName::view, "view", UniformType::Matrix);
|
|
|
|
|
DeclareUniform(UniformName::world, "world", UniformType::Matrix);
|
|
|
|
|
DeclareUniform(UniformName::modelnormal, "modelnormal", UniformType::Matrix);
|
|
|
|
|
DeclareUniform(UniformName::rendersettings, "rendersettings", UniformType::Vec4f);
|
|
|
|
|
DeclareUniform(UniformName::highlightcolor, "highlightcolor", UniformType::Vec4f);
|
|
|
|
|
DeclareUniform(UniformName::FillColor, "fillColor", UniformType::Vec4f);
|
|
|
|
|
DeclareUniform(UniformName::vertexColor, "vertexColor", UniformType::Vec4f);
|
|
|
|
|
DeclareUniform(UniformName::stencilColor, "stencilColor", UniformType::Vec4f);
|
|
|
|
|
DeclareUniform(UniformName::lightPosAndRadius, "lightPosAndRadius", UniformType::Vec4f);
|
|
|
|
|
DeclareUniform(UniformName::lightColor, "lightColor", UniformType::Vec4f);
|
|
|
|
|
DeclareUniform(UniformName::campos, "campos", UniformType::Vec4f);
|
|
|
|
|
DeclareUniform(UniformName::texturefactor, "texturefactor", UniformType::Vec4f);
|
|
|
|
|
DeclareUniform(UniformName::fogsettings, "fogsettings", UniformType::Vec4f);
|
|
|
|
|
DeclareUniform(UniformName::fogcolor, "fogcolor", UniformType::Vec4f);
|
|
|
|
|
DeclareUniform(UniformName::lightOrientation, "lightOrientation", UniformType::Vec3f);
|
|
|
|
|
DeclareUniform(UniformName::light2Radius, "light2Radius", UniformType::Vec2f);
|
|
|
|
|
DeclareUniform(UniformName::desaturation, "desaturation", UniformType::Float);
|
|
|
|
|
DeclareUniform(UniformName::ignoreNormals, "ignoreNormals", UniformType::Float);
|
|
|
|
|
DeclareUniform(UniformName::spotLight, "spotLight", UniformType::Float);
|
|
|
|
|
|
2019-12-15 21:53:33 +00:00
|
|
|
|
Context = IOpenGLContext::Create(disp, window);
|
|
|
|
|
if (Context)
|
|
|
|
|
{
|
|
|
|
|
Context->MakeCurrent();
|
|
|
|
|
|
2019-12-16 04:54:44 +00:00
|
|
|
|
#ifdef _DEBUG
|
|
|
|
|
glEnable(GL_DEBUG_OUTPUT);
|
|
|
|
|
glDebugMessageCallback(&GLLogCallback, nullptr);
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-12-15 21:53:33 +00:00
|
|
|
|
glGenVertexArrays(1, &mStreamVAO);
|
|
|
|
|
glGenBuffers(1, &mStreamVertexBuffer);
|
|
|
|
|
glBindVertexArray(mStreamVAO);
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, mStreamVertexBuffer);
|
2019-12-18 01:27:49 +00:00
|
|
|
|
SharedVertexBuffer::SetupFlatVAO();
|
|
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
for (auto& sharedbuf : mSharedVertexBuffers)
|
|
|
|
|
{
|
|
|
|
|
sharedbuf.reset(new SharedVertexBuffer((VertexFormat)i, (int64_t)16 * 1024 * 1024));
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, sharedbuf->GetBuffer());
|
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sharedbuf->Size, nullptr, GL_STATIC_DRAW);
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-15 21:53:33 +00:00
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
|
|
|
|
|
|
mShaderManager = std::make_unique<ShaderManager>();
|
|
|
|
|
|
|
|
|
|
CheckGLError();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RenderDevice::~RenderDevice()
|
|
|
|
|
{
|
|
|
|
|
if (Context)
|
|
|
|
|
{
|
|
|
|
|
Context->MakeCurrent();
|
|
|
|
|
glDeleteBuffers(1, &mStreamVertexBuffer);
|
|
|
|
|
glDeleteVertexArrays(1, &mStreamVAO);
|
2019-12-18 01:27:49 +00:00
|
|
|
|
for (auto& sharedbuf : mSharedVertexBuffers)
|
|
|
|
|
{
|
|
|
|
|
GLuint handle = sharedbuf->GetBuffer();
|
|
|
|
|
glDeleteBuffers(1, &handle);
|
|
|
|
|
handle = sharedbuf->GetVAO();
|
|
|
|
|
glDeleteVertexArrays(1, &handle);
|
|
|
|
|
}
|
2019-12-18 02:24:09 +00:00
|
|
|
|
for (auto& it : mSamplers)
|
|
|
|
|
{
|
|
|
|
|
for (GLuint handle : it.second.WrapModes)
|
|
|
|
|
{
|
|
|
|
|
if (handle != 0)
|
|
|
|
|
glDeleteSamplers(1, &handle);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-15 21:53:33 +00:00
|
|
|
|
mShaderManager->ReleaseResources();
|
|
|
|
|
Context->ClearCurrent();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RenderDevice::SetVertexBuffer(VertexBuffer* buffer)
|
|
|
|
|
{
|
2019-12-18 01:27:49 +00:00
|
|
|
|
if (buffer != nullptr)
|
2019-12-15 21:53:33 +00:00
|
|
|
|
{
|
2019-12-18 01:27:49 +00:00
|
|
|
|
mVertexBufferStartIndex = buffer->BufferStartIndex;
|
|
|
|
|
if (mVertexBuffer != (int)buffer->Format)
|
|
|
|
|
{
|
|
|
|
|
mVertexBuffer = (int)buffer->Format;
|
|
|
|
|
mNeedApply = true;
|
|
|
|
|
mVertexBufferChanged = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
mVertexBufferStartIndex = 0;
|
|
|
|
|
if (mVertexBuffer != -1)
|
|
|
|
|
{
|
|
|
|
|
mVertexBuffer = -1;
|
|
|
|
|
mNeedApply = true;
|
|
|
|
|
mVertexBufferChanged = true;
|
|
|
|
|
}
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RenderDevice::SetIndexBuffer(IndexBuffer* buffer)
|
|
|
|
|
{
|
|
|
|
|
if (mIndexBuffer != buffer)
|
|
|
|
|
{
|
|
|
|
|
mIndexBuffer = buffer;
|
|
|
|
|
mNeedApply = true;
|
|
|
|
|
mIndexBufferChanged = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RenderDevice::SetAlphaBlendEnable(bool value)
|
|
|
|
|
{
|
|
|
|
|
if (mAlphaBlend != value)
|
|
|
|
|
{
|
|
|
|
|
mAlphaBlend = value;
|
|
|
|
|
mNeedApply = true;
|
|
|
|
|
mBlendStateChanged = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RenderDevice::SetAlphaTestEnable(bool value)
|
|
|
|
|
{
|
|
|
|
|
if (mAlphaTest != value)
|
|
|
|
|
{
|
|
|
|
|
mAlphaTest = value;
|
|
|
|
|
mNeedApply = true;
|
|
|
|
|
mShaderChanged = true;
|
|
|
|
|
mUniformsChanged = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RenderDevice::SetCullMode(Cull mode)
|
|
|
|
|
{
|
|
|
|
|
if (mCullMode != mode)
|
|
|
|
|
{
|
|
|
|
|
mCullMode = mode;
|
|
|
|
|
mNeedApply = true;
|
|
|
|
|
mRasterizerStateChanged = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RenderDevice::SetBlendOperation(BlendOperation op)
|
|
|
|
|
{
|
|
|
|
|
if (mBlendOperation != op)
|
|
|
|
|
{
|
|
|
|
|
mBlendOperation = op;
|
|
|
|
|
mNeedApply = true;
|
|
|
|
|
mBlendStateChanged = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RenderDevice::SetSourceBlend(Blend blend)
|
|
|
|
|
{
|
|
|
|
|
if (mSourceBlend != blend)
|
|
|
|
|
{
|
|
|
|
|
mSourceBlend = blend;
|
|
|
|
|
mNeedApply = true;
|
|
|
|
|
mBlendStateChanged = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RenderDevice::SetDestinationBlend(Blend blend)
|
|
|
|
|
{
|
|
|
|
|
if (mDestinationBlend != blend)
|
|
|
|
|
{
|
|
|
|
|
mDestinationBlend = blend;
|
|
|
|
|
mNeedApply = true;
|
|
|
|
|
mBlendStateChanged = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RenderDevice::SetFillMode(FillMode mode)
|
|
|
|
|
{
|
|
|
|
|
if (mFillMode != mode)
|
|
|
|
|
{
|
|
|
|
|
mFillMode = mode;
|
|
|
|
|
mNeedApply = true;
|
|
|
|
|
mRasterizerStateChanged = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RenderDevice::SetMultisampleAntialias(bool value)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RenderDevice::SetZEnable(bool value)
|
|
|
|
|
{
|
|
|
|
|
if (mDepthTest != value)
|
|
|
|
|
{
|
|
|
|
|
mDepthTest = value;
|
|
|
|
|
mNeedApply = true;
|
|
|
|
|
mDepthStateChanged = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RenderDevice::SetZWriteEnable(bool value)
|
|
|
|
|
{
|
|
|
|
|
if (mDepthWrite != value)
|
|
|
|
|
{
|
|
|
|
|
mDepthWrite = value;
|
|
|
|
|
mNeedApply = true;
|
|
|
|
|
mDepthStateChanged = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RenderDevice::SetTexture(Texture* texture)
|
|
|
|
|
{
|
|
|
|
|
if (mTextureUnit.Tex != texture)
|
|
|
|
|
{
|
|
|
|
|
mTextureUnit.Tex = texture;
|
|
|
|
|
mNeedApply = true;
|
|
|
|
|
mTexturesChanged = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RenderDevice::SetSamplerFilter(TextureFilter minfilter, TextureFilter magfilter, TextureFilter mipfilter, float maxanisotropy)
|
|
|
|
|
{
|
2019-12-18 02:24:09 +00:00
|
|
|
|
SamplerFilterKey key;
|
|
|
|
|
key.MinFilter = GetGLMinFilter(minfilter, mipfilter);
|
|
|
|
|
key.MagFilter = (magfilter == TextureFilter::Point || magfilter == TextureFilter::None) ? GL_NEAREST : GL_LINEAR;
|
|
|
|
|
key.MaxAnisotropy = maxanisotropy;
|
|
|
|
|
if (mSamplerFilterKey != key)
|
2019-12-15 21:53:33 +00:00
|
|
|
|
{
|
2019-12-18 02:24:09 +00:00
|
|
|
|
mSamplerFilterKey = key;
|
|
|
|
|
mSamplerFilter = &mSamplers[mSamplerFilterKey];
|
|
|
|
|
|
2019-12-15 21:53:33 +00:00
|
|
|
|
mNeedApply = true;
|
|
|
|
|
mTexturesChanged = 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-12-18 02:24:09 +00:00
|
|
|
|
void RenderDevice::SetSamplerState(TextureAddress address)
|
2019-12-15 21:53:33 +00:00
|
|
|
|
{
|
2019-12-18 02:24:09 +00:00
|
|
|
|
if (mTextureUnit.WrapMode != address)
|
2019-12-15 21:53:33 +00:00
|
|
|
|
{
|
2019-12-18 02:24:09 +00:00
|
|
|
|
mTextureUnit.WrapMode = address;
|
2019-12-15 21:53:33 +00:00
|
|
|
|
mNeedApply = true;
|
|
|
|
|
mTexturesChanged = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool RenderDevice::Draw(PrimitiveType type, int startIndex, int primitiveCount)
|
2019-12-15 21:53:33 +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 };
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
if (mNeedApply && !ApplyChanges()) return false;
|
2019-12-18 01:27:49 +00:00
|
|
|
|
glDrawArrays(modes[(int)type], mVertexBufferStartIndex + startIndex, toVertexStart[(int)type] + primitiveCount * toVertexCount[(int)type]);
|
2019-12-20 02:39:06 +00:00
|
|
|
|
return CheckGLError();
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool RenderDevice::DrawIndexed(PrimitiveType type, int startIndex, int primitiveCount)
|
2019-12-15 21:53:33 +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 };
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
if (mNeedApply && !ApplyChanges()) return false;
|
2019-12-18 01:27:49 +00:00
|
|
|
|
glDrawElementsBaseVertex(modes[(int)type], toVertexStart[(int)type] + primitiveCount * toVertexCount[(int)type], GL_UNSIGNED_INT, (const void*)(startIndex * sizeof(uint32_t)), mVertexBufferStartIndex);
|
2019-12-20 02:39:06 +00:00
|
|
|
|
return CheckGLError();
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool RenderDevice::DrawData(PrimitiveType type, int startIndex, int primitiveCount, const void* data)
|
2019-12-15 21:53:33 +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];
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
if (mNeedApply && !ApplyChanges()) return false;
|
2019-12-15 21:53:33 +00:00
|
|
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, mStreamVertexBuffer);
|
2019-12-18 01:27:49 +00:00
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, vertcount * (size_t)SharedVertexBuffer::FlatStride, static_cast<const uint8_t*>(data) + startIndex * (size_t)SharedVertexBuffer::FlatStride, GL_STREAM_DRAW);
|
2019-12-15 21:53:33 +00:00
|
|
|
|
glBindVertexArray(mStreamVAO);
|
|
|
|
|
glDrawArrays(modes[(int)type], 0, vertcount);
|
2019-12-20 02:39:06 +00:00
|
|
|
|
if (!CheckGLError()) return false;
|
|
|
|
|
|
|
|
|
|
return ApplyVertexBuffer();
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool RenderDevice::StartRendering(bool clear, int backcolor, Texture* target, bool usedepthbuffer)
|
2019-12-15 21:53:33 +00:00
|
|
|
|
{
|
|
|
|
|
Context->MakeCurrent();
|
|
|
|
|
mContextIsCurrent = true;
|
|
|
|
|
|
|
|
|
|
if (target)
|
|
|
|
|
{
|
|
|
|
|
GLuint framebuffer = 0;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
framebuffer = target->GetFramebuffer(usedepthbuffer);
|
|
|
|
|
}
|
|
|
|
|
catch (std::runtime_error& e)
|
|
|
|
|
{
|
|
|
|
|
SetError("Error setting render target: %s", e.what());
|
2019-12-20 02:39:06 +00:00
|
|
|
|
return false;
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
|
|
|
|
|
mViewportWidth = target->GetWidth();
|
|
|
|
|
mViewportHeight = target->GetHeight();
|
2019-12-20 02:39:06 +00:00
|
|
|
|
if (!ApplyViewport()) return false;
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
|
|
mViewportWidth = Context->GetWidth();
|
|
|
|
|
mViewportHeight = Context->GetHeight();
|
2019-12-20 02:39:06 +00:00
|
|
|
|
if (!ApplyViewport()) return false;
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (clear && usedepthbuffer)
|
|
|
|
|
{
|
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
|
glDepthMask(GL_TRUE);
|
|
|
|
|
glClearColor(RPART(backcolor) / 255.0f, GPART(backcolor) / 255.0f, BPART(backcolor) / 255.0f, APART(backcolor) / 255.0f);
|
|
|
|
|
glClearDepthf(1.0f);
|
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
}
|
|
|
|
|
else if (clear)
|
|
|
|
|
{
|
|
|
|
|
glClearColor(RPART(backcolor) / 255.0f, GPART(backcolor) / 255.0f, BPART(backcolor) / 255.0f, APART(backcolor) / 255.0f);
|
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mNeedApply = true;
|
|
|
|
|
mShaderChanged = true;
|
|
|
|
|
mUniformsChanged = true;
|
|
|
|
|
mTexturesChanged = true;
|
|
|
|
|
mIndexBufferChanged = true;
|
|
|
|
|
mVertexBufferChanged = true;
|
|
|
|
|
mDepthStateChanged = true;
|
|
|
|
|
mBlendStateChanged = true;
|
|
|
|
|
mRasterizerStateChanged = true;
|
2019-12-16 04:54:44 +00:00
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
return CheckGLError();
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool RenderDevice::FinishRendering()
|
2019-12-15 21:53:33 +00:00
|
|
|
|
{
|
|
|
|
|
mContextIsCurrent = false;
|
2019-12-20 02:39:06 +00:00
|
|
|
|
return true;
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool RenderDevice::Present()
|
2019-12-15 21:53:33 +00:00
|
|
|
|
{
|
|
|
|
|
Context->SwapBuffers();
|
2019-12-20 02:39:06 +00:00
|
|
|
|
return CheckGLError();
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool RenderDevice::ClearTexture(int backcolor, Texture* texture)
|
2019-12-15 21:53:33 +00:00
|
|
|
|
{
|
2019-12-20 02:39:06 +00:00
|
|
|
|
if (!StartRendering(true, backcolor, texture, false)) return false;
|
|
|
|
|
return FinishRendering();
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool RenderDevice::CopyTexture(Texture* dst, CubeMapFace face)
|
2019-12-15 21:53:33 +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
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (!mContextIsCurrent) Context->MakeCurrent();
|
|
|
|
|
GLint oldTexture = 0;
|
|
|
|
|
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-12-20 02:39:06 +00:00
|
|
|
|
bool result = CheckGLError();
|
|
|
|
|
return result;
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool RenderDevice::SetVertexBufferData(VertexBuffer* buffer, void* data, int64_t size, VertexFormat format)
|
2019-12-15 21:53:33 +00:00
|
|
|
|
{
|
|
|
|
|
if (!mContextIsCurrent) Context->MakeCurrent();
|
2019-12-18 01:27:49 +00:00
|
|
|
|
|
2019-12-15 21:53:33 +00:00
|
|
|
|
GLint oldbinding = 0;
|
|
|
|
|
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &oldbinding);
|
2019-12-18 01:27:49 +00:00
|
|
|
|
|
|
|
|
|
auto& sharedbuf = mSharedVertexBuffers[(int)format];
|
|
|
|
|
if (sharedbuf->NextPos + size > sharedbuf->Size)
|
|
|
|
|
{
|
|
|
|
|
std::unique_ptr<SharedVertexBuffer> old = std::move(sharedbuf);
|
|
|
|
|
sharedbuf.reset(new SharedVertexBuffer(format, old->Size * 2));
|
|
|
|
|
sharedbuf->NextPos = old->NextPos;
|
|
|
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, sharedbuf->GetBuffer());
|
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sharedbuf->Size, nullptr, GL_STATIC_DRAW);
|
|
|
|
|
|
|
|
|
|
glBindBuffer(GL_COPY_READ_BUFFER, old->GetBuffer());
|
|
|
|
|
glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_ARRAY_BUFFER, 0, 0, old->Size);
|
|
|
|
|
glBindBuffer(GL_COPY_READ_BUFFER, 0);
|
|
|
|
|
|
|
|
|
|
GLuint handle = old->GetBuffer();
|
|
|
|
|
glDeleteBuffers(1, &handle);
|
|
|
|
|
handle = old->GetVAO();
|
|
|
|
|
glDeleteVertexArrays(1, &handle);
|
|
|
|
|
|
|
|
|
|
mVertexBufferChanged = true;
|
|
|
|
|
mNeedApply = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, sharedbuf->GetBuffer());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buffer->Format = format;
|
|
|
|
|
buffer->BufferOffset = sharedbuf->NextPos;
|
|
|
|
|
buffer->BufferStartIndex = buffer->BufferOffset / (format == VertexFormat::Flat ? SharedVertexBuffer::FlatStride : SharedVertexBuffer::WorldStride);
|
|
|
|
|
sharedbuf->NextPos += size;
|
|
|
|
|
|
|
|
|
|
glBufferSubData(GL_ARRAY_BUFFER, buffer->BufferOffset, size, data);
|
2019-12-15 21:53:33 +00:00
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, oldbinding);
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool result = CheckGLError();
|
|
|
|
|
return result;
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool RenderDevice::SetVertexBufferSubdata(VertexBuffer* buffer, int64_t destOffset, void* data, int64_t size)
|
2019-12-15 21:53:33 +00:00
|
|
|
|
{
|
|
|
|
|
if (!mContextIsCurrent) Context->MakeCurrent();
|
|
|
|
|
GLint oldbinding = 0;
|
|
|
|
|
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &oldbinding);
|
2019-12-18 01:27:49 +00:00
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, mSharedVertexBuffers[(int)buffer->Format]->GetBuffer());
|
|
|
|
|
glBufferSubData(GL_ARRAY_BUFFER, buffer->BufferOffset + destOffset, size, data);
|
2019-12-15 21:53:33 +00:00
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, oldbinding);
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool result = CheckGLError();
|
|
|
|
|
return result;
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool RenderDevice::SetIndexBufferData(IndexBuffer* buffer, void* data, int64_t size)
|
2019-12-15 21:53:33 +00:00
|
|
|
|
{
|
|
|
|
|
if (!mContextIsCurrent) Context->MakeCurrent();
|
|
|
|
|
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-12-20 02:39:06 +00:00
|
|
|
|
bool result = CheckGLError();
|
|
|
|
|
return result;
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool RenderDevice::SetPixels(Texture* texture, const void* data)
|
2019-12-15 21:53:33 +00:00
|
|
|
|
{
|
|
|
|
|
texture->SetPixels(data);
|
2019-12-20 02:39:06 +00:00
|
|
|
|
return InvalidateTexture(texture);
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool RenderDevice::SetCubePixels(Texture* texture, CubeMapFace face, const void* data)
|
2019-12-15 21:53:33 +00:00
|
|
|
|
{
|
|
|
|
|
texture->SetCubePixels(face, data);
|
2019-12-20 02:39:06 +00:00
|
|
|
|
return InvalidateTexture(texture);
|
2019-12-16 04:54:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void* RenderDevice::MapPBO(Texture* texture)
|
|
|
|
|
{
|
|
|
|
|
if (!mContextIsCurrent) Context->MakeCurrent();
|
|
|
|
|
GLint pbo = texture->GetPBO();
|
|
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo);
|
|
|
|
|
void* buf = glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool result = CheckGLError();
|
|
|
|
|
if (!result && buf)
|
|
|
|
|
{
|
|
|
|
|
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
|
|
|
|
|
buf = nullptr;
|
|
|
|
|
}
|
2019-12-16 04:54:44 +00:00
|
|
|
|
return buf;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool RenderDevice::UnmapPBO(Texture* texture)
|
2019-12-16 04:54:44 +00:00
|
|
|
|
{
|
|
|
|
|
if (!mContextIsCurrent) Context->MakeCurrent();
|
|
|
|
|
GLint pbo = texture->GetPBO();
|
|
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo);
|
|
|
|
|
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture->GetTexture());
|
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, texture->GetWidth(), texture->GetHeight(), 0, GL_BGRA, GL_UNSIGNED_BYTE, nullptr);
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool result = CheckGLError();
|
2019-12-16 04:54:44 +00:00
|
|
|
|
mNeedApply = true;
|
|
|
|
|
mTexturesChanged = true;
|
2019-12-20 02:39:06 +00:00
|
|
|
|
return result;
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool RenderDevice::InvalidateTexture(Texture* texture)
|
2019-12-15 21:53:33 +00:00
|
|
|
|
{
|
|
|
|
|
if (texture->IsTextureCreated())
|
|
|
|
|
{
|
|
|
|
|
if (!mContextIsCurrent) Context->MakeCurrent();
|
|
|
|
|
texture->Invalidate();
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool result = CheckGLError();
|
2019-12-15 21:53:33 +00:00
|
|
|
|
mNeedApply = true;
|
|
|
|
|
mTexturesChanged = true;
|
2019-12-20 02:39:06 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return true;
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-16 04:54:44 +00:00
|
|
|
|
bool RenderDevice::CheckGLError()
|
2019-12-15 21:53:33 +00:00
|
|
|
|
{
|
2019-12-20 02:39:06 +00:00
|
|
|
|
if (!Context->IsCurrent())
|
2019-12-16 04:54:44 +00:00
|
|
|
|
{
|
2019-12-20 02:39:06 +00:00
|
|
|
|
SetError("Unexpected current OpenGL context");
|
2019-12-16 04:54:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
GLenum error = glGetError();
|
|
|
|
|
if (error == GL_NO_ERROR)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
SetError("OpenGL error: %d", error);
|
2019-12-16 04:54:44 +00:00
|
|
|
|
return false;
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RenderDevice::SetError(const char* fmt, ...)
|
|
|
|
|
{
|
|
|
|
|
va_list va;
|
|
|
|
|
va_start(va, fmt);
|
2019-12-20 02:39:06 +00:00
|
|
|
|
mSetErrorBuffer[0] = 0;
|
|
|
|
|
mSetErrorBuffer[sizeof(mSetErrorBuffer) - 1] = 0;
|
|
|
|
|
_vsnprintf(mSetErrorBuffer, sizeof(mSetErrorBuffer)-1, fmt, va);
|
2019-12-15 21:53:33 +00:00
|
|
|
|
va_end(va);
|
2019-12-20 02:39:06 +00:00
|
|
|
|
mLastError = mSetErrorBuffer;
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char* RenderDevice::GetError()
|
|
|
|
|
{
|
2019-12-20 02:39:06 +00:00
|
|
|
|
mReturnError.swap(mLastError);
|
|
|
|
|
mLastError.clear();
|
|
|
|
|
return mReturnError.c_str();
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Shader* RenderDevice::GetActiveShader()
|
|
|
|
|
{
|
|
|
|
|
if (mAlphaTest)
|
|
|
|
|
return &mShaderManager->AlphaTestShaders[(int)mShaderName];
|
|
|
|
|
else
|
|
|
|
|
return &mShaderManager->Shaders[(int)mShaderName];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RenderDevice::SetShader(ShaderName name)
|
|
|
|
|
{
|
|
|
|
|
if (name != mShaderName)
|
|
|
|
|
{
|
|
|
|
|
mShaderName = name;
|
|
|
|
|
mNeedApply = true;
|
|
|
|
|
mShaderChanged = true;
|
|
|
|
|
mUniformsChanged = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RenderDevice::SetUniform(UniformName name, const void* values, int count)
|
|
|
|
|
{
|
2019-12-18 03:22:47 +00:00
|
|
|
|
float* dest = mUniformData.data() + mUniformInfo[(int)name].Offset;
|
2019-12-15 21:53:33 +00:00
|
|
|
|
if (memcmp(dest, values, sizeof(float) * count) != 0)
|
|
|
|
|
{
|
|
|
|
|
memcpy(dest, values, sizeof(float) * count);
|
2019-12-18 03:22:47 +00:00
|
|
|
|
mUniformInfo[(int)name].LastUpdate++;
|
2019-12-15 21:53:33 +00:00
|
|
|
|
mNeedApply = true;
|
|
|
|
|
mUniformsChanged = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool RenderDevice::ApplyChanges()
|
|
|
|
|
{
|
|
|
|
|
if (mShaderChanged && !ApplyShader()) return false;
|
|
|
|
|
if (mVertexBufferChanged && !ApplyVertexBuffer()) return false;
|
|
|
|
|
if (mIndexBufferChanged && !ApplyIndexBuffer()) return false;
|
|
|
|
|
if (mUniformsChanged && !ApplyUniforms()) return false;
|
|
|
|
|
if (mTexturesChanged && !ApplyTextures()) return false;
|
|
|
|
|
if (mRasterizerStateChanged && !ApplyRasterizerState()) return false;
|
|
|
|
|
if (mBlendStateChanged && !ApplyBlendState()) return false;
|
|
|
|
|
if (mDepthStateChanged && !ApplyDepthState()) return false;
|
2019-12-15 21:53:33 +00:00
|
|
|
|
|
|
|
|
|
mNeedApply = false;
|
2019-12-20 02:39:06 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool RenderDevice::ApplyViewport()
|
|
|
|
|
{
|
|
|
|
|
glViewport(0, 0, mViewportWidth, mViewportHeight);
|
|
|
|
|
return CheckGLError();
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool RenderDevice::ApplyShader()
|
2019-12-15 21:53:33 +00:00
|
|
|
|
{
|
|
|
|
|
Shader* curShader = GetActiveShader();
|
2019-12-18 03:22:47 +00:00
|
|
|
|
if (!curShader->CheckCompile(this))
|
2019-12-15 21:53:33 +00:00
|
|
|
|
{
|
|
|
|
|
SetError("Failed to bind shader:\r\n%s", curShader->GetCompileError().c_str());
|
2019-12-20 02:39:06 +00:00
|
|
|
|
return false;
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
curShader->Bind();
|
|
|
|
|
mShaderChanged = false;
|
2019-12-16 04:54:44 +00:00
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
return CheckGLError();
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool RenderDevice::ApplyRasterizerState()
|
2019-12-15 21:53:33 +00:00
|
|
|
|
{
|
|
|
|
|
if (mCullMode == Cull::None)
|
|
|
|
|
{
|
|
|
|
|
glDisable(GL_CULL_FACE);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
glEnable(GL_CULL_FACE);
|
|
|
|
|
glFrontFace(GL_CW);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GLenum fillMode2GL[] = { GL_FILL, GL_LINE };
|
|
|
|
|
glPolygonMode(GL_FRONT_AND_BACK, fillMode2GL[(int)mFillMode]);
|
|
|
|
|
|
|
|
|
|
mRasterizerStateChanged = false;
|
2019-12-16 04:54:44 +00:00
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
return CheckGLError();
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool RenderDevice::ApplyBlendState()
|
2019-12-15 21:53:33 +00:00
|
|
|
|
{
|
|
|
|
|
if (mAlphaBlend)
|
|
|
|
|
{
|
|
|
|
|
static const GLenum blendOp2GL[] = { GL_FUNC_ADD, GL_FUNC_REVERSE_SUBTRACT };
|
|
|
|
|
static const GLenum blendFunc2GL[] = { GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA, GL_ONE };
|
|
|
|
|
|
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
|
glBlendEquation(blendOp2GL[(int)mBlendOperation]);
|
|
|
|
|
glBlendFunc(blendFunc2GL[(int)mSourceBlend], blendFunc2GL[(int)mDestinationBlend]);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
glDisable(GL_BLEND);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mBlendStateChanged = false;
|
2019-12-16 04:54:44 +00:00
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
return CheckGLError();
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool RenderDevice::ApplyDepthState()
|
2019-12-15 21:53:33 +00:00
|
|
|
|
{
|
|
|
|
|
if (mDepthTest)
|
|
|
|
|
{
|
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
|
glDepthFunc(GL_LEQUAL);
|
|
|
|
|
glDepthMask(mDepthWrite ? GL_TRUE : GL_FALSE);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
glDisable(GL_DEPTH_TEST);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mDepthStateChanged = false;
|
2019-12-16 04:54:44 +00:00
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
return CheckGLError();
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool RenderDevice::ApplyIndexBuffer()
|
2019-12-15 21:53:33 +00:00
|
|
|
|
{
|
|
|
|
|
if (mIndexBuffer)
|
|
|
|
|
{
|
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer->GetBuffer());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mIndexBufferChanged = false;
|
2019-12-16 04:54:44 +00:00
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
return CheckGLError();
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool RenderDevice::ApplyVertexBuffer()
|
2019-12-15 21:53:33 +00:00
|
|
|
|
{
|
2019-12-18 01:27:49 +00:00
|
|
|
|
if (mVertexBuffer != -1)
|
|
|
|
|
glBindVertexArray(mSharedVertexBuffers[mVertexBuffer]->GetVAO());
|
2019-12-15 21:53:33 +00:00
|
|
|
|
|
|
|
|
|
mVertexBufferChanged = false;
|
2019-12-16 04:54:44 +00:00
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
return CheckGLError();
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-18 03:22:47 +00:00
|
|
|
|
void RenderDevice::DeclareUniform(UniformName name, const char* glslname, UniformType type)
|
|
|
|
|
{
|
|
|
|
|
UniformInfo& info = mUniformInfo[(int)name];
|
|
|
|
|
info.Name = glslname;
|
|
|
|
|
info.Type = type;
|
|
|
|
|
info.Offset = (int)mUniformData.size();
|
|
|
|
|
|
|
|
|
|
mUniformData.resize(mUniformData.size() + (type == UniformType::Matrix ? 16 : 4));
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool RenderDevice::ApplyUniforms()
|
2019-12-15 21:53:33 +00:00
|
|
|
|
{
|
|
|
|
|
Shader* shader = GetActiveShader();
|
|
|
|
|
auto& locations = shader->UniformLocations;
|
2019-12-18 03:22:47 +00:00
|
|
|
|
auto& lastupdates = shader->UniformLastUpdates;
|
2019-12-15 21:53:33 +00:00
|
|
|
|
|
2019-12-18 03:22:47 +00:00
|
|
|
|
for (int i = 0; i < (int)UniformName::NumUniforms; i++)
|
|
|
|
|
{
|
|
|
|
|
if (lastupdates[i] != mUniformInfo[i].LastUpdate)
|
|
|
|
|
{
|
|
|
|
|
float* data = mUniformData.data() + mUniformInfo[i].Offset;
|
|
|
|
|
GLuint location = locations[i];
|
|
|
|
|
switch (mUniformInfo[i].Type)
|
|
|
|
|
{
|
|
|
|
|
default:
|
|
|
|
|
case UniformType::Vec4f: glUniform4fv(location, 1, data); break;
|
|
|
|
|
case UniformType::Vec3f: glUniform3fv(location, 1, data); break;
|
|
|
|
|
case UniformType::Vec2f: glUniform2fv(location, 1, data); break;
|
|
|
|
|
case UniformType::Float: glUniform1fv(location, 1, data); break;
|
|
|
|
|
case UniformType::Matrix: glUniformMatrix4fv(location, 1, GL_FALSE, data); break;
|
|
|
|
|
}
|
|
|
|
|
lastupdates[i] = mUniformInfo[i].LastUpdate;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-15 21:53:33 +00:00
|
|
|
|
|
|
|
|
|
mUniformsChanged = false;
|
2019-12-16 04:54:44 +00:00
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
return CheckGLError();
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool RenderDevice::ApplyTextures()
|
2019-12-15 21:53:33 +00:00
|
|
|
|
{
|
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
|
if (mTextureUnit.Tex)
|
|
|
|
|
{
|
|
|
|
|
GLenum target = mTextureUnit.Tex->IsCubeTexture() ? GL_TEXTURE_CUBE_MAP : GL_TEXTURE_2D;
|
|
|
|
|
|
|
|
|
|
glBindTexture(target, mTextureUnit.Tex->GetTexture());
|
2019-12-18 02:24:09 +00:00
|
|
|
|
|
|
|
|
|
GLuint& samplerHandle = mSamplerFilter->WrapModes[(int)mTextureUnit.WrapMode];
|
|
|
|
|
if (samplerHandle == 0)
|
|
|
|
|
{
|
|
|
|
|
static const int wrapMode[] = { GL_REPEAT, GL_CLAMP_TO_EDGE };
|
|
|
|
|
|
|
|
|
|
glGenSamplers(1, &samplerHandle);
|
|
|
|
|
glSamplerParameteri(samplerHandle, GL_TEXTURE_MIN_FILTER, mSamplerFilterKey.MinFilter);
|
|
|
|
|
glSamplerParameteri(samplerHandle, GL_TEXTURE_MAG_FILTER, mSamplerFilterKey.MagFilter);
|
|
|
|
|
glSamplerParameteri(samplerHandle, GL_TEXTURE_WRAP_S, wrapMode[(int)mTextureUnit.WrapMode]);
|
|
|
|
|
glSamplerParameteri(samplerHandle, GL_TEXTURE_WRAP_T, wrapMode[(int)mTextureUnit.WrapMode]);
|
|
|
|
|
glSamplerParameteri(samplerHandle, GL_TEXTURE_WRAP_R, wrapMode[(int)mTextureUnit.WrapMode]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mTextureUnit.SamplerHandle != samplerHandle)
|
|
|
|
|
{
|
|
|
|
|
mTextureUnit.SamplerHandle = samplerHandle;
|
|
|
|
|
glBindSampler(0, samplerHandle);
|
|
|
|
|
}
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mTexturesChanged = false;
|
2019-12-16 04:54:44 +00:00
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
return CheckGLError();
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
RenderDevice* RenderDevice_New(void* disp, void* window)
|
|
|
|
|
{
|
|
|
|
|
RenderDevice *device = new RenderDevice(disp, window);
|
|
|
|
|
if (!device->Context)
|
|
|
|
|
{
|
|
|
|
|
delete device;
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return device;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RenderDevice_Delete(RenderDevice* device)
|
|
|
|
|
{
|
|
|
|
|
delete device;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char* RenderDevice_GetError(RenderDevice* device)
|
|
|
|
|
{
|
|
|
|
|
return device->GetError();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RenderDevice_SetVertexBuffer(RenderDevice* device, VertexBuffer* buffer)
|
|
|
|
|
{
|
|
|
|
|
device->SetVertexBuffer(buffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RenderDevice_SetTexture(RenderDevice* device, Texture* texture)
|
|
|
|
|
{
|
|
|
|
|
device->SetTexture(texture);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RenderDevice_SetSamplerFilter(RenderDevice* device, TextureFilter minfilter, TextureFilter magfilter, TextureFilter mipfilter, float maxanisotropy)
|
|
|
|
|
{
|
|
|
|
|
device->SetSamplerFilter(minfilter, magfilter, mipfilter, maxanisotropy);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-18 02:24:09 +00:00
|
|
|
|
void RenderDevice_SetSamplerState(RenderDevice* device, TextureAddress address)
|
2019-12-15 21:53:33 +00:00
|
|
|
|
{
|
2019-12-18 02:24:09 +00:00
|
|
|
|
device->SetSamplerState(address);
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool RenderDevice_Draw(RenderDevice* device, PrimitiveType type, int startIndex, int primitiveCount)
|
2019-12-15 21:53:33 +00:00
|
|
|
|
{
|
2019-12-20 02:39:06 +00:00
|
|
|
|
return device->Draw(type, startIndex, primitiveCount);
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool RenderDevice_DrawIndexed(RenderDevice* device, PrimitiveType type, int startIndex, int primitiveCount)
|
2019-12-15 21:53:33 +00:00
|
|
|
|
{
|
2019-12-20 02:39:06 +00:00
|
|
|
|
return device->DrawIndexed(type, startIndex, primitiveCount);
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool RenderDevice_DrawData(RenderDevice* device, PrimitiveType type, int startIndex, int primitiveCount, const void* data)
|
2019-12-15 21:53:33 +00:00
|
|
|
|
{
|
2019-12-20 02:39:06 +00:00
|
|
|
|
return device->DrawData(type, startIndex, primitiveCount, data);
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool RenderDevice_StartRendering(RenderDevice* device, bool clear, int backcolor, Texture* target, bool usedepthbuffer)
|
2019-12-15 21:53:33 +00:00
|
|
|
|
{
|
2019-12-20 02:39:06 +00:00
|
|
|
|
return device->StartRendering(clear, backcolor, target, usedepthbuffer);
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool RenderDevice_FinishRendering(RenderDevice* device)
|
2019-12-15 21:53:33 +00:00
|
|
|
|
{
|
2019-12-20 02:39:06 +00:00
|
|
|
|
return device->FinishRendering();
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool RenderDevice_Present(RenderDevice* device)
|
2019-12-15 21:53:33 +00:00
|
|
|
|
{
|
2019-12-20 02:39:06 +00:00
|
|
|
|
return device->Present();
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool RenderDevice_ClearTexture(RenderDevice* device, int backcolor, Texture* texture)
|
2019-12-15 21:53:33 +00:00
|
|
|
|
{
|
2019-12-20 02:39:06 +00:00
|
|
|
|
return device->ClearTexture(backcolor, texture);
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool RenderDevice_CopyTexture(RenderDevice* device, Texture* dst, CubeMapFace face)
|
2019-12-15 21:53:33 +00:00
|
|
|
|
{
|
2019-12-20 02:39:06 +00:00
|
|
|
|
return device->CopyTexture(dst, face);
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool RenderDevice_SetVertexBufferData(RenderDevice* device, VertexBuffer* buffer, void* data, int64_t size, VertexFormat format)
|
2019-12-15 21:53:33 +00:00
|
|
|
|
{
|
2019-12-20 02:39:06 +00:00
|
|
|
|
return device->SetVertexBufferData(buffer, data, size, format);
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool RenderDevice_SetVertexBufferSubdata(RenderDevice* device, VertexBuffer* buffer, int64_t destOffset, void* data, int64_t size)
|
2019-12-15 21:53:33 +00:00
|
|
|
|
{
|
2019-12-20 02:39:06 +00:00
|
|
|
|
return device->SetVertexBufferSubdata(buffer, destOffset, data, size);
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool RenderDevice_SetIndexBufferData(RenderDevice* device, IndexBuffer* buffer, void* data, int64_t size)
|
2019-12-15 21:53:33 +00:00
|
|
|
|
{
|
2019-12-20 02:39:06 +00:00
|
|
|
|
return device->SetIndexBufferData(buffer, data, size);
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool RenderDevice_SetPixels(RenderDevice* device, Texture* texture, const void* data)
|
2019-12-15 21:53:33 +00:00
|
|
|
|
{
|
2019-12-20 02:39:06 +00:00
|
|
|
|
return device->SetPixels(texture, data);
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool RenderDevice_SetCubePixels(RenderDevice* device, Texture* texture, CubeMapFace face, const void* data)
|
2019-12-15 21:53:33 +00:00
|
|
|
|
{
|
2019-12-20 02:39:06 +00:00
|
|
|
|
return device->SetCubePixels(texture, face, data);
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-16 04:54:44 +00:00
|
|
|
|
void* RenderDevice_MapPBO(RenderDevice* device, Texture* texture)
|
|
|
|
|
{
|
|
|
|
|
return device->MapPBO(texture);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
|
bool RenderDevice_UnmapPBO(RenderDevice* device, Texture* texture)
|
2019-12-16 04:54:44 +00:00
|
|
|
|
{
|
2019-12-20 02:39:06 +00:00
|
|
|
|
return device->UnmapPBO(texture);
|
2019-12-16 04:54:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-19 02:12:44 +00:00
|
|
|
|
#ifdef NO_SSE
|
|
|
|
|
|
|
|
|
|
void Matrix_Null(float result[4][4])
|
|
|
|
|
{
|
|
|
|
|
memset(result, 0, sizeof(float) * 16);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
void Matrix_Null(float result[4][4])
|
|
|
|
|
{
|
|
|
|
|
__m128 zero = _mm_setzero_ps();
|
|
|
|
|
_mm_storeu_ps(result[0], zero);
|
|
|
|
|
_mm_storeu_ps(result[1], zero);
|
|
|
|
|
_mm_storeu_ps(result[2], zero);
|
|
|
|
|
_mm_storeu_ps(result[3], zero);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
void Matrix_Identity(float result[4][4])
|
|
|
|
|
{
|
|
|
|
|
Matrix_Null(result);
|
|
|
|
|
result[0][0] = 1.0f;
|
|
|
|
|
result[1][1] = 1.0f;
|
|
|
|
|
result[2][2] = 1.0f;
|
|
|
|
|
result[3][3] = 1.0f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Matrix_Translation(float x, float y, float z, float result[4][4])
|
|
|
|
|
{
|
|
|
|
|
Matrix_Null(result);
|
|
|
|
|
result[0][0] = 1.0f;
|
|
|
|
|
result[1][1] = 1.0f;
|
|
|
|
|
result[2][2] = 1.0f;
|
|
|
|
|
result[3][0] = x;
|
|
|
|
|
result[3][1] = y;
|
|
|
|
|
result[3][2] = z;
|
|
|
|
|
result[3][3] = 1.0f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Matrix_RotationX(float angle, float result[4][4])
|
|
|
|
|
{
|
|
|
|
|
float cos = fastcos(angle);
|
|
|
|
|
float sin = fastsin(angle);
|
|
|
|
|
|
|
|
|
|
Matrix_Null(result);
|
|
|
|
|
result[0][0] = 1.0f;
|
|
|
|
|
result[1][1] = cos;
|
|
|
|
|
result[1][2] = sin;
|
|
|
|
|
result[2][1] = -sin;
|
|
|
|
|
result[2][2] = cos;
|
|
|
|
|
result[3][3] = 1.0f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Matrix_RotationY(float angle, float result[4][4])
|
|
|
|
|
{
|
|
|
|
|
float cos = fastcos(angle);
|
|
|
|
|
float sin = fastsin(angle);
|
|
|
|
|
|
|
|
|
|
Matrix_Null(result);
|
|
|
|
|
result[0][0] = cos;
|
|
|
|
|
result[0][2] = -sin;
|
|
|
|
|
result[1][1] = 1.0f;
|
|
|
|
|
result[2][0] = sin;
|
|
|
|
|
result[2][2] = cos;
|
|
|
|
|
result[3][3] = 1.0f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Matrix_RotationZ(float angle, float result[4][4])
|
|
|
|
|
{
|
|
|
|
|
float cos = fastcos(angle);
|
|
|
|
|
float sin = fastsin(angle);
|
|
|
|
|
|
|
|
|
|
Matrix_Null(result);
|
|
|
|
|
result[0][0] = cos;
|
|
|
|
|
result[0][1] = sin;
|
|
|
|
|
result[1][0] = -sin;
|
|
|
|
|
result[1][1] = cos;
|
|
|
|
|
result[2][2] = 1.0f;
|
|
|
|
|
result[3][3] = 1.0f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Matrix_Scaling(float x, float y, float z, float result[4][4])
|
|
|
|
|
{
|
|
|
|
|
Matrix_Null(result);
|
|
|
|
|
result[0][0] = x;
|
|
|
|
|
result[1][1] = y;
|
|
|
|
|
result[2][2] = z;
|
|
|
|
|
result[3][3] = 1.0f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef NO_SSE
|
|
|
|
|
|
|
|
|
|
void Matrix_Multiply(const float* left, const float* right, float* result)
|
|
|
|
|
{
|
|
|
|
|
result[0 * 4 + 0] = left[0 * 4 + 0] * right[0 * 4 + 0] + left[0 * 4 + 1] * right[1 * 4 + 0] + left[0 * 4 + 2] * right[2 * 4 + 0] + left[0 * 4 + 3] * right[3 * 4 + 0];
|
|
|
|
|
result[0 * 4 + 1] = left[0 * 4 + 0] * right[0 * 4 + 1] + left[0 * 4 + 1] * right[1 * 4 + 1] + left[0 * 4 + 2] * right[2 * 4 + 1] + left[0 * 4 + 3] * right[3 * 4 + 1];
|
|
|
|
|
result[0 * 4 + 2] = left[0 * 4 + 0] * right[0 * 4 + 2] + left[0 * 4 + 1] * right[1 * 4 + 2] + left[0 * 4 + 2] * right[2 * 4 + 2] + left[0 * 4 + 3] * right[3 * 4 + 2];
|
|
|
|
|
result[0 * 4 + 3] = left[0 * 4 + 0] * right[0 * 4 + 3] + left[0 * 4 + 1] * right[1 * 4 + 3] + left[0 * 4 + 2] * right[2 * 4 + 3] + left[0 * 4 + 3] * right[3 * 4 + 3];
|
|
|
|
|
result[1 * 4 + 0] = left[1 * 4 + 0] * right[0 * 4 + 0] + left[1 * 4 + 1] * right[1 * 4 + 0] + left[1 * 4 + 2] * right[2 * 4 + 0] + left[1 * 4 + 3] * right[3 * 4 + 0];
|
|
|
|
|
result[1 * 4 + 1] = left[1 * 4 + 0] * right[0 * 4 + 1] + left[1 * 4 + 1] * right[1 * 4 + 1] + left[1 * 4 + 2] * right[2 * 4 + 1] + left[1 * 4 + 3] * right[3 * 4 + 1];
|
|
|
|
|
result[1 * 4 + 2] = left[1 * 4 + 0] * right[0 * 4 + 2] + left[1 * 4 + 1] * right[1 * 4 + 2] + left[1 * 4 + 2] * right[2 * 4 + 2] + left[1 * 4 + 3] * right[3 * 4 + 2];
|
|
|
|
|
result[1 * 4 + 3] = left[1 * 4 + 0] * right[0 * 4 + 3] + left[1 * 4 + 1] * right[1 * 4 + 3] + left[1 * 4 + 2] * right[2 * 4 + 3] + left[1 * 4 + 3] * right[3 * 4 + 3];
|
|
|
|
|
result[2 * 4 + 0] = left[2 * 4 + 0] * right[0 * 4 + 0] + left[2 * 4 + 1] * right[1 * 4 + 0] + left[2 * 4 + 2] * right[2 * 4 + 0] + left[2 * 4 + 3] * right[3 * 4 + 0];
|
|
|
|
|
result[2 * 4 + 1] = left[2 * 4 + 0] * right[0 * 4 + 1] + left[2 * 4 + 1] * right[1 * 4 + 1] + left[2 * 4 + 2] * right[2 * 4 + 1] + left[2 * 4 + 3] * right[3 * 4 + 1];
|
|
|
|
|
result[2 * 4 + 2] = left[2 * 4 + 0] * right[0 * 4 + 2] + left[2 * 4 + 1] * right[1 * 4 + 2] + left[2 * 4 + 2] * right[2 * 4 + 2] + left[2 * 4 + 3] * right[3 * 4 + 2];
|
|
|
|
|
result[2 * 4 + 3] = left[2 * 4 + 0] * right[0 * 4 + 3] + left[2 * 4 + 1] * right[1 * 4 + 3] + left[2 * 4 + 2] * right[2 * 4 + 3] + left[2 * 4 + 3] * right[3 * 4 + 3];
|
|
|
|
|
result[3 * 4 + 0] = left[3 * 4 + 0] * right[0 * 4 + 0] + left[3 * 4 + 1] * right[1 * 4 + 0] + left[3 * 4 + 2] * right[2 * 4 + 0] + left[3 * 4 + 3] * right[3 * 4 + 0];
|
|
|
|
|
result[3 * 4 + 1] = left[3 * 4 + 0] * right[0 * 4 + 1] + left[3 * 4 + 1] * right[1 * 4 + 1] + left[3 * 4 + 2] * right[2 * 4 + 1] + left[3 * 4 + 3] * right[3 * 4 + 1];
|
|
|
|
|
result[3 * 4 + 2] = left[3 * 4 + 0] * right[0 * 4 + 2] + left[3 * 4 + 1] * right[1 * 4 + 2] + left[3 * 4 + 2] * right[2 * 4 + 2] + left[3 * 4 + 3] * right[3 * 4 + 2];
|
|
|
|
|
result[3 * 4 + 3] = left[3 * 4 + 0] * right[0 * 4 + 3] + left[3 * 4 + 1] * right[1 * 4 + 3] + left[3 * 4 + 2] * right[2 * 4 + 3] + left[3 * 4 + 3] * right[3 * 4 + 3];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
void Matrix_Multiply(const float a[4][4], const float b[4][4], float result[4][4])
|
|
|
|
|
{
|
|
|
|
|
__m128 otherRow0 = _mm_loadu_ps(b[0]);
|
|
|
|
|
__m128 otherRow1 = _mm_loadu_ps(b[1]);
|
|
|
|
|
__m128 otherRow2 = _mm_loadu_ps(b[2]);
|
|
|
|
|
__m128 otherRow3 = _mm_loadu_ps(b[3]);
|
|
|
|
|
|
|
|
|
|
__m128 newRow0 = _mm_mul_ps(otherRow0, _mm_set1_ps(a[0][0]));
|
|
|
|
|
newRow0 = _mm_add_ps(newRow0, _mm_mul_ps(otherRow1, _mm_set1_ps(a[0][1])));
|
|
|
|
|
newRow0 = _mm_add_ps(newRow0, _mm_mul_ps(otherRow2, _mm_set1_ps(a[0][2])));
|
|
|
|
|
newRow0 = _mm_add_ps(newRow0, _mm_mul_ps(otherRow3, _mm_set1_ps(a[0][3])));
|
|
|
|
|
|
|
|
|
|
__m128 newRow1 = _mm_mul_ps(otherRow0, _mm_set1_ps(a[1][0]));
|
|
|
|
|
newRow1 = _mm_add_ps(newRow1, _mm_mul_ps(otherRow1, _mm_set1_ps(a[1][1])));
|
|
|
|
|
newRow1 = _mm_add_ps(newRow1, _mm_mul_ps(otherRow2, _mm_set1_ps(a[1][2])));
|
|
|
|
|
newRow1 = _mm_add_ps(newRow1, _mm_mul_ps(otherRow3, _mm_set1_ps(a[1][3])));
|
|
|
|
|
|
|
|
|
|
__m128 newRow2 = _mm_mul_ps(otherRow0, _mm_set1_ps(a[2][0]));
|
|
|
|
|
newRow2 = _mm_add_ps(newRow2, _mm_mul_ps(otherRow1, _mm_set1_ps(a[2][1])));
|
|
|
|
|
newRow2 = _mm_add_ps(newRow2, _mm_mul_ps(otherRow2, _mm_set1_ps(a[2][2])));
|
|
|
|
|
newRow2 = _mm_add_ps(newRow2, _mm_mul_ps(otherRow3, _mm_set1_ps(a[2][3])));
|
|
|
|
|
|
|
|
|
|
__m128 newRow3 = _mm_mul_ps(otherRow0, _mm_set1_ps(a[3][0]));
|
|
|
|
|
newRow3 = _mm_add_ps(newRow3, _mm_mul_ps(otherRow1, _mm_set1_ps(a[3][1])));
|
|
|
|
|
newRow3 = _mm_add_ps(newRow3, _mm_mul_ps(otherRow2, _mm_set1_ps(a[3][2])));
|
|
|
|
|
newRow3 = _mm_add_ps(newRow3, _mm_mul_ps(otherRow3, _mm_set1_ps(a[3][3])));
|
|
|
|
|
|
|
|
|
|
_mm_storeu_ps(result[0], newRow0);
|
|
|
|
|
_mm_storeu_ps(result[1], newRow1);
|
|
|
|
|
_mm_storeu_ps(result[2], newRow2);
|
|
|
|
|
_mm_storeu_ps(result[3], newRow3);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-12-15 21:53:33 +00:00
|
|
|
|
}
|