2019-02-21 21:49:00 +00:00
|
|
|
|
|
|
|
#include "vk_renderstate.h"
|
|
|
|
#include "vulkan/system/vk_framebuffer.h"
|
2019-02-26 10:27:29 +00:00
|
|
|
#include "vulkan/system/vk_builders.h"
|
|
|
|
#include "vulkan/renderer/vk_renderpass.h"
|
2019-02-26 14:29:08 +00:00
|
|
|
#include "vulkan/textures/vk_hwtexture.h"
|
2019-02-21 21:49:00 +00:00
|
|
|
#include "templates.h"
|
|
|
|
#include "doomstat.h"
|
|
|
|
#include "r_data/colormaps.h"
|
|
|
|
#include "hwrenderer/scene/hw_skydome.h"
|
2019-02-26 10:27:29 +00:00
|
|
|
#include "hwrenderer/scene/hw_viewpointuniforms.h"
|
2019-02-21 21:49:00 +00:00
|
|
|
#include "hwrenderer/dynlights/hw_lightbuffer.h"
|
|
|
|
#include "hwrenderer/utility/hw_cvars.h"
|
|
|
|
#include "hwrenderer/utility/hw_clock.h"
|
|
|
|
#include "hwrenderer/data/flatvertices.h"
|
|
|
|
#include "hwrenderer/data/hw_viewpointbuffer.h"
|
2019-02-26 10:27:29 +00:00
|
|
|
#include "hwrenderer/data/shaderuniforms.h"
|
|
|
|
|
|
|
|
VkRenderState::VkRenderState()
|
|
|
|
{
|
2019-03-02 22:47:56 +00:00
|
|
|
mIdentityMatrix.loadIdentity();
|
2019-02-26 19:19:54 +00:00
|
|
|
Reset();
|
2019-02-26 10:27:29 +00:00
|
|
|
}
|
2019-02-21 21:49:00 +00:00
|
|
|
|
|
|
|
void VkRenderState::ClearScreen()
|
|
|
|
{
|
|
|
|
screen->mViewpoints->Set2D(*this, SCREENWIDTH, SCREENHEIGHT);
|
|
|
|
SetColor(0, 0, 0);
|
|
|
|
Apply(DT_TriangleStrip);
|
|
|
|
mCommandBuffer->draw(4, 1, FFlatVertexBuffer::FULLSCREEN_INDEX, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VkRenderState::Draw(int dt, int index, int count, bool apply)
|
|
|
|
{
|
2019-03-03 21:25:38 +00:00
|
|
|
if (apply || mNeedApply)
|
2019-02-21 21:49:00 +00:00
|
|
|
Apply(dt);
|
2019-02-26 10:27:29 +00:00
|
|
|
|
2019-02-21 21:49:00 +00:00
|
|
|
drawcalls.Clock();
|
2019-02-26 15:50:54 +00:00
|
|
|
mCommandBuffer->draw(count, 1, index, 0);
|
2019-02-21 21:49:00 +00:00
|
|
|
drawcalls.Unclock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VkRenderState::DrawIndexed(int dt, int index, int count, bool apply)
|
|
|
|
{
|
2019-03-03 21:25:38 +00:00
|
|
|
if (apply || mNeedApply)
|
2019-02-21 21:49:00 +00:00
|
|
|
Apply(dt);
|
2019-02-26 10:27:29 +00:00
|
|
|
|
2019-02-21 21:49:00 +00:00
|
|
|
drawcalls.Clock();
|
2019-02-26 19:19:54 +00:00
|
|
|
mCommandBuffer->drawIndexed(count, 1, index, 0, 0);
|
2019-02-21 21:49:00 +00:00
|
|
|
drawcalls.Unclock();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VkRenderState::SetDepthClamp(bool on)
|
|
|
|
{
|
2019-03-03 22:54:13 +00:00
|
|
|
bool lastValue = mDepthClamp;
|
|
|
|
mDepthClamp = on;
|
2019-03-03 21:25:38 +00:00
|
|
|
mNeedApply = true;
|
2019-02-21 21:49:00 +00:00
|
|
|
return lastValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VkRenderState::SetDepthMask(bool on)
|
|
|
|
{
|
2019-03-02 00:56:08 +00:00
|
|
|
mDepthWrite = on;
|
2019-03-03 21:25:38 +00:00
|
|
|
mNeedApply = true;
|
2019-02-21 21:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VkRenderState::SetDepthFunc(int func)
|
|
|
|
{
|
2019-03-03 21:25:38 +00:00
|
|
|
mDepthFunc = func;
|
|
|
|
mNeedApply = true;
|
2019-02-21 21:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VkRenderState::SetDepthRange(float min, float max)
|
|
|
|
{
|
2019-02-28 14:45:59 +00:00
|
|
|
mViewportDepthMin = min;
|
|
|
|
mViewportDepthMax = max;
|
|
|
|
mViewportChanged = true;
|
2019-03-03 21:25:38 +00:00
|
|
|
mNeedApply = true;
|
2019-02-21 21:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VkRenderState::SetColorMask(bool r, bool g, bool b, bool a)
|
|
|
|
{
|
2019-03-03 21:25:38 +00:00
|
|
|
int rr = r, gg = g, bb = b, aa = a;
|
2019-03-04 00:54:57 +00:00
|
|
|
mColorMask = (aa << 3) | (bb << 2) | (gg << 1) | rr;
|
2019-03-03 21:25:38 +00:00
|
|
|
mNeedApply = true;
|
2019-02-21 21:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VkRenderState::EnableDrawBufferAttachments(bool on)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void VkRenderState::SetStencil(int offs, int op, int flags)
|
|
|
|
{
|
2019-03-03 21:25:38 +00:00
|
|
|
mStencilRef = screen->stencilValue + offs;
|
|
|
|
mStencilRefChanged = true;
|
|
|
|
mStencilOp = op;
|
|
|
|
|
|
|
|
if (flags != -1)
|
|
|
|
{
|
|
|
|
bool cmon = !(flags & SF_ColorMaskOff);
|
|
|
|
SetColorMask(cmon, cmon, cmon, cmon); // don't write to the graphics buffer
|
2019-03-04 00:23:13 +00:00
|
|
|
mDepthWrite = !(flags & SF_DepthMaskOff);
|
2019-03-03 21:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mNeedApply = true;
|
2019-02-21 21:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VkRenderState::SetCulling(int mode)
|
|
|
|
{
|
2019-03-03 21:25:38 +00:00
|
|
|
mCullMode = mode;
|
|
|
|
mNeedApply = true;
|
2019-02-21 21:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VkRenderState::EnableClipDistance(int num, bool state)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void VkRenderState::Clear(int targets)
|
|
|
|
{
|
2019-03-01 20:34:08 +00:00
|
|
|
// We need an active render pass, and it must have a depth attachment..
|
|
|
|
bool lastDepthTest = mDepthTest;
|
|
|
|
bool lastDepthWrite = mDepthWrite;
|
|
|
|
if (targets & (CT_Depth | CT_Stencil))
|
|
|
|
{
|
|
|
|
mDepthTest = true;
|
|
|
|
mDepthWrite = true;
|
|
|
|
}
|
|
|
|
Apply(DT_TriangleFan);
|
|
|
|
mDepthTest = lastDepthTest;
|
|
|
|
mDepthWrite = lastDepthWrite;
|
|
|
|
|
|
|
|
VkClearAttachment attachments[2] = { };
|
|
|
|
VkClearRect rects[2] = { };
|
|
|
|
|
|
|
|
for (int i = 0; i < 2; i++)
|
|
|
|
{
|
|
|
|
rects[i].layerCount = 1;
|
|
|
|
if (mScissorWidth >= 0)
|
|
|
|
{
|
|
|
|
rects[0].rect.offset.x = mScissorX;
|
|
|
|
rects[0].rect.offset.y = mScissorY;
|
|
|
|
rects[0].rect.extent.width = mScissorWidth;
|
|
|
|
rects[0].rect.extent.height = mScissorHeight;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rects[0].rect.offset.x = 0;
|
|
|
|
rects[0].rect.offset.y = 0;
|
|
|
|
rects[0].rect.extent.width = SCREENWIDTH;
|
|
|
|
rects[0].rect.extent.height = SCREENHEIGHT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (targets & CT_Depth)
|
|
|
|
{
|
|
|
|
attachments[1].aspectMask |= VK_IMAGE_ASPECT_DEPTH_BIT;
|
|
|
|
attachments[1].clearValue.depthStencil.depth = 1.0f;
|
|
|
|
}
|
|
|
|
if (targets & CT_Stencil)
|
|
|
|
{
|
|
|
|
attachments[1].aspectMask |= VK_IMAGE_ASPECT_STENCIL_BIT;
|
|
|
|
attachments[1].clearValue.depthStencil.stencil = 0;
|
|
|
|
}
|
|
|
|
if (targets & CT_Color)
|
|
|
|
{
|
|
|
|
attachments[0].aspectMask |= VK_IMAGE_ASPECT_COLOR_BIT;
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
attachments[0].clearValue.color.float32[i] = screen->mSceneClearColor[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((targets & CT_Color) && (targets & CT_Stencil) && (targets & CT_Depth))
|
|
|
|
mCommandBuffer->clearAttachments(2, attachments, 2, rects);
|
|
|
|
else if (targets & (CT_Stencil | CT_Depth))
|
|
|
|
mCommandBuffer->clearAttachments(1, attachments + 1, 1, rects + 1);
|
|
|
|
else if (targets & CT_Color)
|
|
|
|
mCommandBuffer->clearAttachments(1, attachments, 1, rects);
|
2019-02-21 21:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VkRenderState::EnableStencil(bool on)
|
|
|
|
{
|
2019-03-03 21:25:38 +00:00
|
|
|
mStencilTest = on;
|
|
|
|
mNeedApply = true;
|
2019-02-21 21:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VkRenderState::SetScissor(int x, int y, int w, int h)
|
|
|
|
{
|
2019-02-28 14:45:59 +00:00
|
|
|
mScissorX = x;
|
|
|
|
mScissorY = y;
|
|
|
|
mScissorWidth = w;
|
|
|
|
mScissorHeight = h;
|
|
|
|
mScissorChanged = true;
|
2019-03-03 21:25:38 +00:00
|
|
|
mNeedApply = true;
|
2019-02-21 21:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VkRenderState::SetViewport(int x, int y, int w, int h)
|
|
|
|
{
|
2019-02-28 14:45:59 +00:00
|
|
|
mViewportX = x;
|
|
|
|
mViewportY = y;
|
|
|
|
mViewportWidth = w;
|
|
|
|
mViewportHeight = h;
|
|
|
|
mViewportChanged = true;
|
2019-03-03 21:25:38 +00:00
|
|
|
mNeedApply = true;
|
2019-02-21 21:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VkRenderState::EnableDepthTest(bool on)
|
|
|
|
{
|
2019-03-01 20:34:08 +00:00
|
|
|
mDepthTest = on;
|
2019-03-03 21:25:38 +00:00
|
|
|
mNeedApply = true;
|
2019-02-21 21:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VkRenderState::EnableMultisampling(bool on)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void VkRenderState::EnableLineSmooth(bool on)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-03-02 22:39:44 +00:00
|
|
|
void VkRenderState::Apply(int dt)
|
2019-03-01 19:15:56 +00:00
|
|
|
{
|
2019-03-02 22:39:44 +00:00
|
|
|
ApplyRenderPass(dt);
|
|
|
|
ApplyScissor();
|
|
|
|
ApplyViewport();
|
2019-03-03 21:25:38 +00:00
|
|
|
ApplyStencilRef();
|
2019-03-03 23:14:28 +00:00
|
|
|
ApplyDepthBias();
|
2019-03-02 22:39:44 +00:00
|
|
|
ApplyStreamData();
|
|
|
|
ApplyMatrices();
|
|
|
|
ApplyPushConstants();
|
|
|
|
ApplyVertexBuffers();
|
|
|
|
ApplyDynamicSet();
|
|
|
|
ApplyMaterial();
|
2019-03-03 21:25:38 +00:00
|
|
|
mNeedApply = false;
|
2019-03-01 19:15:56 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 23:14:28 +00:00
|
|
|
void VkRenderState::ApplyDepthBias()
|
|
|
|
{
|
|
|
|
if (mBias.mChanged)
|
|
|
|
{
|
|
|
|
mCommandBuffer->setDepthBias(mBias.mUnits, 0.0f, mBias.mFactor);
|
|
|
|
mBias.mChanged = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 22:39:44 +00:00
|
|
|
void VkRenderState::ApplyRenderPass(int dt)
|
2019-02-21 21:49:00 +00:00
|
|
|
{
|
|
|
|
auto fb = GetVulkanFrameBuffer();
|
2019-02-26 10:27:29 +00:00
|
|
|
auto passManager = fb->GetRenderPassManager();
|
2019-02-21 21:49:00 +00:00
|
|
|
|
2019-02-28 23:42:51 +00:00
|
|
|
// Find a render pass that matches our state
|
|
|
|
VkRenderPassKey passKey;
|
2019-03-01 19:06:20 +00:00
|
|
|
passKey.DrawType = dt;
|
2019-03-01 14:37:13 +00:00
|
|
|
passKey.VertexFormat = static_cast<VKVertexBuffer*>(mVertexBuffer)->VertexFormat;
|
2019-02-28 23:42:51 +00:00
|
|
|
passKey.RenderStyle = mRenderStyle;
|
2019-03-01 20:34:08 +00:00
|
|
|
passKey.DepthTest = mDepthTest;
|
2019-03-02 15:02:27 +00:00
|
|
|
passKey.DepthWrite = mDepthTest && mDepthWrite;
|
2019-03-03 21:25:38 +00:00
|
|
|
passKey.DepthFunc = mDepthFunc;
|
2019-03-03 22:54:13 +00:00
|
|
|
passKey.DepthClamp = mDepthClamp;
|
2019-03-03 23:14:28 +00:00
|
|
|
passKey.DepthBias = !(mBias.mFactor == 0 && mBias.mUnits == 0);
|
2019-03-03 21:25:38 +00:00
|
|
|
passKey.StencilTest = mStencilTest;
|
|
|
|
passKey.StencilPassOp = mStencilOp;
|
|
|
|
passKey.ColorMask = mColorMask;
|
|
|
|
passKey.CullMode = mCullMode;
|
2019-03-01 01:40:02 +00:00
|
|
|
if (mSpecialEffect > EFF_NONE)
|
|
|
|
{
|
|
|
|
passKey.SpecialEffect = mSpecialEffect;
|
|
|
|
passKey.EffectState = 0;
|
|
|
|
passKey.AlphaTest = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int effectState = mMaterial.mOverrideShader >= 0 ? mMaterial.mOverrideShader : (mMaterial.mMaterial ? mMaterial.mMaterial->GetShaderIndex() : 0);
|
|
|
|
passKey.SpecialEffect = EFF_NONE;
|
|
|
|
passKey.EffectState = mTextureEnabled ? effectState : SHADER_NoTexture;
|
|
|
|
passKey.AlphaTest = mAlphaThreshold >= 0.f;
|
|
|
|
}
|
2019-02-28 23:42:51 +00:00
|
|
|
|
|
|
|
// Is this the one we already have or do we need to change render pass?
|
2019-03-02 23:16:00 +00:00
|
|
|
bool changingRenderPass = (passKey != mRenderPassKey);
|
2019-02-21 21:49:00 +00:00
|
|
|
|
|
|
|
if (!mCommandBuffer)
|
|
|
|
{
|
|
|
|
mCommandBuffer = fb->GetDrawCommands();
|
|
|
|
changingRenderPass = true;
|
2019-02-28 14:45:59 +00:00
|
|
|
mScissorChanged = true;
|
|
|
|
mViewportChanged = true;
|
2019-03-03 21:25:38 +00:00
|
|
|
mStencilRefChanged = true;
|
2019-03-03 23:14:28 +00:00
|
|
|
mBias.mChanged = true;
|
2019-02-21 21:49:00 +00:00
|
|
|
}
|
|
|
|
else if (changingRenderPass)
|
|
|
|
{
|
|
|
|
mCommandBuffer->endRenderPass();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (changingRenderPass)
|
|
|
|
{
|
2019-03-02 23:16:00 +00:00
|
|
|
VkRenderPassSetup *passSetup = passManager->GetRenderPass(passKey);
|
|
|
|
|
2019-02-21 21:49:00 +00:00
|
|
|
RenderPassBegin beginInfo;
|
2019-02-26 10:27:29 +00:00
|
|
|
beginInfo.setRenderPass(passSetup->RenderPass.get());
|
2019-02-21 21:49:00 +00:00
|
|
|
beginInfo.setRenderArea(0, 0, SCREENWIDTH, SCREENHEIGHT);
|
2019-02-26 10:27:29 +00:00
|
|
|
beginInfo.setFramebuffer(passSetup->Framebuffer.get());
|
2019-02-21 21:49:00 +00:00
|
|
|
mCommandBuffer->beginRenderPass(beginInfo);
|
2019-02-26 10:27:29 +00:00
|
|
|
mCommandBuffer->bindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, passSetup->Pipeline.get());
|
2019-03-02 23:16:00 +00:00
|
|
|
|
|
|
|
mRenderPassKey = passKey;
|
2019-02-21 21:49:00 +00:00
|
|
|
}
|
2019-03-02 22:39:44 +00:00
|
|
|
}
|
2019-02-21 21:49:00 +00:00
|
|
|
|
2019-03-03 21:25:38 +00:00
|
|
|
void VkRenderState::ApplyStencilRef()
|
|
|
|
{
|
|
|
|
if (mStencilRefChanged)
|
|
|
|
{
|
|
|
|
mCommandBuffer->setStencilReference(VK_STENCIL_FRONT_AND_BACK, mStencilRef);
|
|
|
|
mStencilRefChanged = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 22:39:44 +00:00
|
|
|
void VkRenderState::ApplyScissor()
|
|
|
|
{
|
2019-02-28 14:45:59 +00:00
|
|
|
if (mScissorChanged)
|
|
|
|
{
|
|
|
|
VkRect2D scissor;
|
|
|
|
if (mScissorWidth >= 0)
|
|
|
|
{
|
|
|
|
scissor.offset.x = mScissorX;
|
|
|
|
scissor.offset.y = mScissorY;
|
|
|
|
scissor.extent.width = mScissorWidth;
|
|
|
|
scissor.extent.height = mScissorHeight;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
scissor.offset.x = 0;
|
|
|
|
scissor.offset.y = 0;
|
|
|
|
scissor.extent.width = SCREENWIDTH;
|
|
|
|
scissor.extent.height = SCREENHEIGHT;
|
|
|
|
}
|
|
|
|
mCommandBuffer->setScissor(0, 1, &scissor);
|
|
|
|
mScissorChanged = false;
|
|
|
|
}
|
2019-03-02 22:39:44 +00:00
|
|
|
}
|
2019-02-28 14:45:59 +00:00
|
|
|
|
2019-03-02 22:39:44 +00:00
|
|
|
void VkRenderState::ApplyViewport()
|
|
|
|
{
|
2019-02-28 14:45:59 +00:00
|
|
|
if (mViewportChanged)
|
|
|
|
{
|
|
|
|
VkViewport viewport;
|
|
|
|
if (mViewportWidth >= 0)
|
|
|
|
{
|
|
|
|
viewport.x = (float)mViewportX;
|
|
|
|
viewport.y = (float)mViewportY;
|
|
|
|
viewport.width = (float)mViewportWidth;
|
|
|
|
viewport.height = (float)mViewportHeight;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
viewport.x = 0.0f;
|
|
|
|
viewport.y = 0.0f;
|
|
|
|
viewport.width = (float)SCREENWIDTH;
|
|
|
|
viewport.height = (float)SCREENHEIGHT;
|
|
|
|
}
|
|
|
|
viewport.minDepth = mViewportDepthMin;
|
|
|
|
viewport.maxDepth = mViewportDepthMax;
|
|
|
|
mCommandBuffer->setViewport(0, 1, &viewport);
|
|
|
|
mViewportChanged = false;
|
|
|
|
}
|
2019-03-02 22:39:44 +00:00
|
|
|
}
|
2019-02-28 14:45:59 +00:00
|
|
|
|
2019-03-02 22:39:44 +00:00
|
|
|
void VkRenderState::ApplyStreamData()
|
|
|
|
{
|
|
|
|
auto fb = GetVulkanFrameBuffer();
|
|
|
|
auto passManager = fb->GetRenderPassManager();
|
2019-02-27 23:26:49 +00:00
|
|
|
|
2019-03-02 22:39:44 +00:00
|
|
|
const float normScale = 1.0f / 255.0f;
|
2019-02-27 23:26:49 +00:00
|
|
|
|
2019-03-02 22:20:29 +00:00
|
|
|
mStreamData.uDesaturationFactor = mDesaturation * normScale;
|
|
|
|
mStreamData.uFogColor = { mFogColor.r * normScale, mFogColor.g * normScale, mFogColor.b * normScale, mFogColor.a * normScale };
|
|
|
|
mStreamData.uAddColor = { mAddColor.r * normScale, mAddColor.g * normScale, mAddColor.b * normScale, mAddColor.a * normScale };
|
|
|
|
mStreamData.uObjectColor = { mObjectColor.r * normScale, mObjectColor.g * normScale, mObjectColor.b * normScale, mObjectColor.a * normScale };
|
|
|
|
mStreamData.uDynLightColor = mDynColor.vec;
|
|
|
|
mStreamData.uInterpolationFactor = mInterpolationFactor;
|
2019-02-27 23:26:49 +00:00
|
|
|
|
2019-03-02 22:20:29 +00:00
|
|
|
mStreamData.useVertexData = passManager->VertexFormats[static_cast<VKVertexBuffer*>(mVertexBuffer)->VertexFormat].UseVertexData;
|
|
|
|
mStreamData.uVertexColor = mColor.vec;
|
|
|
|
mStreamData.uVertexNormal = mNormal.vec;
|
2019-03-02 00:56:08 +00:00
|
|
|
|
2019-03-02 22:20:29 +00:00
|
|
|
mStreamData.timer = 0.0f; // static_cast<float>((double)(screen->FrameTime - firstFrame) * (double)mShaderTimer / 1000.);
|
2019-02-27 23:26:49 +00:00
|
|
|
|
|
|
|
if (mGlowEnabled)
|
|
|
|
{
|
2019-03-02 22:20:29 +00:00
|
|
|
mStreamData.uGlowTopPlane = mGlowTopPlane.vec;
|
|
|
|
mStreamData.uGlowTopColor = mGlowTop.vec;
|
|
|
|
mStreamData.uGlowBottomPlane = mGlowBottomPlane.vec;
|
|
|
|
mStreamData.uGlowBottomColor = mGlowBottom.vec;
|
2019-03-02 23:06:17 +00:00
|
|
|
mLastGlowEnabled = true;
|
2019-02-27 23:26:49 +00:00
|
|
|
}
|
2019-03-02 23:06:17 +00:00
|
|
|
else if (mLastGlowEnabled)
|
2019-02-27 23:26:49 +00:00
|
|
|
{
|
2019-03-02 22:20:29 +00:00
|
|
|
mStreamData.uGlowTopColor = { 0.0f, 0.0f, 0.0f, 0.0f };
|
|
|
|
mStreamData.uGlowBottomColor = { 0.0f, 0.0f, 0.0f, 0.0f };
|
2019-03-02 23:06:17 +00:00
|
|
|
mLastGlowEnabled = false;
|
2019-02-27 23:26:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mGradientEnabled)
|
|
|
|
{
|
2019-03-02 22:20:29 +00:00
|
|
|
mStreamData.uObjectColor2 = { mObjectColor2.r * normScale, mObjectColor2.g * normScale, mObjectColor2.b * normScale, mObjectColor2.a * normScale };
|
|
|
|
mStreamData.uGradientTopPlane = mGradientTopPlane.vec;
|
|
|
|
mStreamData.uGradientBottomPlane = mGradientBottomPlane.vec;
|
2019-03-02 23:06:17 +00:00
|
|
|
mLastGradientEnabled = true;
|
2019-02-27 23:26:49 +00:00
|
|
|
}
|
2019-03-02 23:06:17 +00:00
|
|
|
else if (mLastGradientEnabled)
|
2019-02-27 23:26:49 +00:00
|
|
|
{
|
2019-03-02 22:20:29 +00:00
|
|
|
mStreamData.uObjectColor2 = { 0.0f, 0.0f, 0.0f, 0.0f };
|
2019-03-02 23:06:17 +00:00
|
|
|
mLastGradientEnabled = false;
|
2019-02-27 23:26:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mSplitEnabled)
|
|
|
|
{
|
2019-03-02 22:20:29 +00:00
|
|
|
mStreamData.uSplitTopPlane = mSplitTopPlane.vec;
|
|
|
|
mStreamData.uSplitBottomPlane = mSplitBottomPlane.vec;
|
2019-03-02 23:06:17 +00:00
|
|
|
mLastSplitEnabled = true;
|
2019-02-27 23:26:49 +00:00
|
|
|
}
|
2019-03-02 23:06:17 +00:00
|
|
|
else if (mLastSplitEnabled)
|
2019-02-27 23:26:49 +00:00
|
|
|
{
|
2019-03-02 22:20:29 +00:00
|
|
|
mStreamData.uSplitTopPlane = { 0.0f, 0.0f, 0.0f, 0.0f };
|
|
|
|
mStreamData.uSplitBottomPlane = { 0.0f, 0.0f, 0.0f, 0.0f };
|
2019-03-02 23:06:17 +00:00
|
|
|
mLastSplitEnabled = false;
|
2019-02-27 23:26:49 +00:00
|
|
|
}
|
|
|
|
|
2019-03-02 22:39:44 +00:00
|
|
|
mDataIndex++;
|
|
|
|
if (mDataIndex == MAX_STREAM_DATA)
|
|
|
|
{
|
|
|
|
mDataIndex = 0;
|
|
|
|
mStreamDataOffset += sizeof(StreamUBO);
|
|
|
|
}
|
|
|
|
uint8_t *ptr = (uint8_t*)fb->StreamUBO->Memory();
|
|
|
|
memcpy(ptr + mStreamDataOffset + sizeof(StreamData) * mDataIndex, &mStreamData, sizeof(StreamData));
|
|
|
|
}
|
|
|
|
|
|
|
|
void VkRenderState::ApplyPushConstants()
|
|
|
|
{
|
|
|
|
int fogset = 0;
|
|
|
|
if (mFogEnabled)
|
|
|
|
{
|
|
|
|
if (mFogEnabled == 2)
|
|
|
|
{
|
|
|
|
fogset = -3; // 2D rendering with 'foggy' overlay.
|
|
|
|
}
|
|
|
|
else if ((mFogColor & 0xffffff) == 0)
|
|
|
|
{
|
|
|
|
fogset = gl_fogmode;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fogset = -gl_fogmode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int tempTM = TM_NORMAL;
|
|
|
|
if (mMaterial.mMaterial && mMaterial.mMaterial->tex->isHardwareCanvas())
|
|
|
|
tempTM = TM_OPAQUE;
|
|
|
|
|
|
|
|
mPushConstants.uFogEnabled = fogset;
|
|
|
|
mPushConstants.uTextureMode = mTextureMode == TM_NORMAL && tempTM == TM_OPAQUE ? TM_OPAQUE : mTextureMode;
|
|
|
|
mPushConstants.uLightDist = mLightParms[0];
|
|
|
|
mPushConstants.uLightFactor = mLightParms[1];
|
|
|
|
mPushConstants.uFogDensity = mLightParms[2];
|
|
|
|
mPushConstants.uLightLevel = mLightParms[3];
|
|
|
|
mPushConstants.uAlphaThreshold = mAlphaThreshold;
|
|
|
|
mPushConstants.uClipSplit = { mClipSplit[0], mClipSplit[1] };
|
|
|
|
|
|
|
|
//if (mMaterial.mMaterial)
|
|
|
|
// mPushConstants.uSpecularMaterial = { mMaterial.mMaterial->tex->Glossiness, mMaterial.mMaterial->tex->SpecularLevel };
|
|
|
|
|
|
|
|
mPushConstants.uLightIndex = screen->mLights->BindUBO(mLightIndex);
|
|
|
|
mPushConstants.uDataIndex = mDataIndex;
|
|
|
|
|
|
|
|
auto fb = GetVulkanFrameBuffer();
|
|
|
|
auto passManager = fb->GetRenderPassManager();
|
|
|
|
mCommandBuffer->pushConstants(passManager->PipelineLayout.get(), VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, 0, (uint32_t)sizeof(PushConstants), &mPushConstants);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
static void CopyToBuffer(uint32_t &offset, const T &data, VKDataBuffer *buffer)
|
|
|
|
{
|
|
|
|
if (offset + (UniformBufferAlignment<T>() << 1) < buffer->Size())
|
|
|
|
{
|
|
|
|
offset += UniformBufferAlignment<T>();
|
|
|
|
memcpy(static_cast<uint8_t*>(buffer->Memory()) + offset, &data, sizeof(T));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 22:47:56 +00:00
|
|
|
template<typename T>
|
|
|
|
static void BufferedSet(bool &modified, T &dst, const T &src)
|
|
|
|
{
|
|
|
|
if (dst == src)
|
|
|
|
return;
|
|
|
|
dst = src;
|
|
|
|
modified = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void BufferedSet(bool &modified, VSMatrix &dst, const VSMatrix &src)
|
|
|
|
{
|
|
|
|
if (memcmp(dst.get(), src.get(), sizeof(FLOATTYPE) * 16) == 0)
|
|
|
|
return;
|
|
|
|
dst = src;
|
|
|
|
modified = true;
|
|
|
|
}
|
|
|
|
|
2019-03-02 22:39:44 +00:00
|
|
|
void VkRenderState::ApplyMatrices()
|
|
|
|
{
|
2019-03-02 22:47:56 +00:00
|
|
|
bool modified = (mMatricesOffset == 0); // always modified first call
|
2019-02-27 23:26:49 +00:00
|
|
|
if (mTextureMatrixEnabled)
|
|
|
|
{
|
2019-03-02 22:47:56 +00:00
|
|
|
BufferedSet(modified, mMatrices.TextureMatrix, mTextureMatrix);
|
2019-02-27 23:26:49 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-02 22:47:56 +00:00
|
|
|
BufferedSet(modified, mMatrices.TextureMatrix, mIdentityMatrix);
|
2019-02-27 23:26:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mModelMatrixEnabled)
|
|
|
|
{
|
2019-03-02 22:47:56 +00:00
|
|
|
BufferedSet(modified, mMatrices.ModelMatrix, mModelMatrix);
|
|
|
|
if (modified)
|
|
|
|
mMatrices.NormalModelMatrix.computeNormalMatrix(mModelMatrix);
|
2019-02-27 23:26:49 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-02 22:47:56 +00:00
|
|
|
BufferedSet(modified, mMatrices.ModelMatrix, mIdentityMatrix);
|
|
|
|
BufferedSet(modified, mMatrices.NormalModelMatrix, mIdentityMatrix);
|
2019-02-27 23:26:49 +00:00
|
|
|
}
|
|
|
|
|
2019-03-02 22:47:56 +00:00
|
|
|
if (modified)
|
|
|
|
{
|
|
|
|
auto fb = GetVulkanFrameBuffer();
|
|
|
|
CopyToBuffer(mMatricesOffset, mMatrices, fb->MatricesUBO);
|
|
|
|
}
|
2019-03-02 22:39:44 +00:00
|
|
|
}
|
2019-03-02 22:20:29 +00:00
|
|
|
|
2019-03-02 22:39:44 +00:00
|
|
|
void VkRenderState::ApplyVertexBuffers()
|
|
|
|
{
|
2019-03-02 22:56:06 +00:00
|
|
|
if (mVertexBuffer != mLastVertexBuffer && mVertexBuffer)
|
|
|
|
{
|
|
|
|
VkBuffer vertexBuffers[] = { static_cast<VKVertexBuffer*>(mVertexBuffer)->mBuffer->buffer };
|
|
|
|
VkDeviceSize offsets[] = { 0 };
|
|
|
|
mCommandBuffer->bindVertexBuffers(0, 1, vertexBuffers, offsets);
|
|
|
|
mLastVertexBuffer = mVertexBuffer;
|
|
|
|
}
|
2019-02-26 10:27:29 +00:00
|
|
|
|
2019-03-02 22:56:06 +00:00
|
|
|
if (mIndexBuffer != mLastIndexBuffer && mIndexBuffer)
|
|
|
|
{
|
2019-03-01 18:15:52 +00:00
|
|
|
mCommandBuffer->bindIndexBuffer(static_cast<VKIndexBuffer*>(mIndexBuffer)->mBuffer->buffer, 0, VK_INDEX_TYPE_UINT32);
|
2019-03-02 22:56:06 +00:00
|
|
|
mLastIndexBuffer = mIndexBuffer;
|
|
|
|
}
|
2019-03-02 22:39:44 +00:00
|
|
|
}
|
2019-02-26 10:27:29 +00:00
|
|
|
|
2019-03-02 22:39:44 +00:00
|
|
|
void VkRenderState::ApplyMaterial()
|
|
|
|
{
|
|
|
|
if (mMaterial.mChanged && mMaterial.mMaterial)
|
2019-02-26 14:29:08 +00:00
|
|
|
{
|
|
|
|
auto base = static_cast<VkHardwareTexture*>(mMaterial.mMaterial->GetLayer(0, mMaterial.mTranslation));
|
|
|
|
if (base)
|
2019-03-02 22:39:44 +00:00
|
|
|
{
|
|
|
|
auto fb = GetVulkanFrameBuffer();
|
|
|
|
auto passManager = fb->GetRenderPassManager();
|
2019-02-26 14:29:08 +00:00
|
|
|
mCommandBuffer->bindDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, passManager->PipelineLayout.get(), 1, base->GetDescriptorSet(mMaterial));
|
2019-03-02 22:39:44 +00:00
|
|
|
}
|
2019-02-26 14:29:08 +00:00
|
|
|
|
|
|
|
mMaterial.mChanged = false;
|
|
|
|
}
|
2019-02-26 10:27:29 +00:00
|
|
|
}
|
|
|
|
|
2019-03-02 22:39:44 +00:00
|
|
|
void VkRenderState::ApplyDynamicSet()
|
2019-02-26 10:27:29 +00:00
|
|
|
{
|
2019-03-05 01:06:20 +00:00
|
|
|
if (mViewpointOffset != mLastViewpointOffset || mLightBufferOffset != mLastLightBufferOffset || mMatricesOffset != mLastMatricesOffset || mStreamDataOffset != mLastStreamDataOffset)
|
2019-03-02 22:39:44 +00:00
|
|
|
{
|
|
|
|
auto fb = GetVulkanFrameBuffer();
|
|
|
|
auto passManager = fb->GetRenderPassManager();
|
2019-02-26 10:27:29 +00:00
|
|
|
|
2019-03-02 22:39:44 +00:00
|
|
|
uint32_t offsets[4] = { mViewpointOffset, mLightBufferOffset, mMatricesOffset, mStreamDataOffset };
|
|
|
|
mCommandBuffer->bindDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, passManager->PipelineLayout.get(), 0, passManager->DynamicSet.get(), 4, offsets);
|
|
|
|
|
|
|
|
mLastViewpointOffset = mViewpointOffset;
|
|
|
|
mLastLightBufferOffset = mLightBufferOffset;
|
2019-03-05 01:06:20 +00:00
|
|
|
mLastMatricesOffset = mMatricesOffset;
|
|
|
|
mLastStreamDataOffset = mStreamDataOffset;
|
2019-03-02 22:39:44 +00:00
|
|
|
}
|
2019-02-26 10:27:29 +00:00
|
|
|
}
|
|
|
|
|
2019-03-02 22:56:06 +00:00
|
|
|
void VkRenderState::Bind(int bindingpoint, uint32_t offset)
|
|
|
|
{
|
|
|
|
if (bindingpoint == VIEWPOINT_BINDINGPOINT)
|
|
|
|
{
|
|
|
|
mViewpointOffset = offset;
|
2019-03-03 21:25:38 +00:00
|
|
|
mNeedApply = true;
|
2019-03-02 22:56:06 +00:00
|
|
|
}
|
|
|
|
else if (bindingpoint == LIGHTBUF_BINDINGPOINT)
|
|
|
|
{
|
|
|
|
mLightBufferOffset = offset;
|
2019-03-03 21:25:38 +00:00
|
|
|
mNeedApply = true;
|
2019-03-02 22:56:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 10:27:29 +00:00
|
|
|
void VkRenderState::EndRenderPass()
|
|
|
|
{
|
|
|
|
if (mCommandBuffer)
|
|
|
|
{
|
|
|
|
mCommandBuffer->endRenderPass();
|
|
|
|
mCommandBuffer = nullptr;
|
2019-03-02 23:16:00 +00:00
|
|
|
mRenderPassKey = {};
|
2019-02-27 23:26:49 +00:00
|
|
|
|
|
|
|
mMatricesOffset = 0;
|
2019-03-02 22:20:29 +00:00
|
|
|
mStreamDataOffset = 0;
|
|
|
|
mDataIndex = -1;
|
2019-03-02 22:39:44 +00:00
|
|
|
mLastViewpointOffset = 0xffffffff;
|
|
|
|
mLastLightBufferOffset = 0xffffffff;
|
2019-03-02 22:56:06 +00:00
|
|
|
mLastVertexBuffer = nullptr;
|
|
|
|
mLastIndexBuffer = nullptr;
|
2019-03-02 23:06:17 +00:00
|
|
|
mLastGlowEnabled = true;
|
|
|
|
mLastGradientEnabled = true;
|
|
|
|
mLastSplitEnabled = true;
|
|
|
|
mLastModelMatrixEnabled = true;
|
|
|
|
mLastTextureMatrixEnabled = true;
|
2019-02-26 10:27:29 +00:00
|
|
|
}
|
2019-02-21 21:49:00 +00:00
|
|
|
}
|