- add vulkan renderstate and shadermanager classes

This commit is contained in:
Magnus Norddahl 2019-02-21 22:49:00 +01:00
parent c606346ae3
commit a97d736bd9
7 changed files with 301 additions and 21 deletions

View file

@ -712,6 +712,8 @@ file( GLOB HEADER_FILES
rendering/hwrenderer/utility/*.h
rendering/vulkan/*.h
rendering/vulkan/system/*.h
rendering/vulkan/renderer/*.h
rendering/vulkan/shaders/*.h
rendering/vulkan/textures/*.h
rendering/gl/*.h
rendering/gl/models/*.h
@ -898,6 +900,8 @@ set( FASTMATH_SOURCES
rendering/vulkan/system/vk_builders.cpp
rendering/vulkan/system/vk_framebuffer.cpp
rendering/vulkan/system/vk_buffers.cpp
rendering/vulkan/renderer/vk_renderstate.cpp
rendering/vulkan/shaders/vk_shader.cpp
rendering/vulkan/textures/vk_samplers.cpp
rendering/vulkan/textures/vk_hwtexture.cpp
rendering/vulkan/thirdparty/volk/volk.c
@ -1494,6 +1498,8 @@ source_group("Rendering\\Hardware Renderer\\System" REGULAR_EXPRESSION "^${CMAKE
source_group("Rendering\\Hardware Renderer\\Textures" REGULAR_EXPRESSION "^${CMAKE_CURRENT_SOURCE_DIR}/rendering/hwrenderer/textures/.+")
source_group("Rendering\\Hardware Renderer\\Utilities" REGULAR_EXPRESSION "^${CMAKE_CURRENT_SOURCE_DIR}/rendering/hwrenderer/utility/.+")
source_group("Rendering\\Vulkan Renderer\\System" REGULAR_EXPRESSION "^${CMAKE_CURRENT_SOURCE_DIR}/rendering/vulkan/system/.+")
source_group("Rendering\\Vulkan Renderer\\Renderer" REGULAR_EXPRESSION "^${CMAKE_CURRENT_SOURCE_DIR}/rendering/vulkan/renderer/.+")
source_group("Rendering\\Vulkan Renderer\\Shaders" REGULAR_EXPRESSION "^${CMAKE_CURRENT_SOURCE_DIR}/rendering/vulkan/shaders/.+")
source_group("Rendering\\Vulkan Renderer\\Textures" REGULAR_EXPRESSION "^${CMAKE_CURRENT_SOURCE_DIR}/rendering/vulkan/textures/.+")
source_group("Rendering\\Vulkan Renderer\\Third Party" REGULAR_EXPRESSION "^${CMAKE_CURRENT_SOURCE_DIR}/rendering/vulkan/thirdparty/.+")
source_group("Rendering\\Vulkan Renderer\\Third Party\\Volk" REGULAR_EXPRESSION "^${CMAKE_CURRENT_SOURCE_DIR}/rendering/vulkan/thirdparty/volk/.+")

View file

@ -0,0 +1,149 @@
#include "vk_renderstate.h"
#include "vulkan/system/vk_framebuffer.h"
#include "templates.h"
#include "doomstat.h"
#include "r_data/colormaps.h"
#include "hwrenderer/scene/hw_skydome.h"
#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"
void VkRenderState::ClearScreen()
{
screen->mViewpoints->Set2D(*this, SCREENWIDTH, SCREENHEIGHT);
SetColor(0, 0, 0);
Apply(DT_TriangleStrip);
/*
glDisable(GL_MULTISAMPLE);
glDisable(GL_DEPTH_TEST);
mCommandBuffer->draw(4, 1, FFlatVertexBuffer::FULLSCREEN_INDEX, 0);
glEnable(GL_DEPTH_TEST);
*/
}
void VkRenderState::Draw(int dt, int index, int count, bool apply)
{
if (apply)
{
Apply(dt);
}
drawcalls.Clock();
//mCommandBuffer->draw(count, 1, index, 0);
drawcalls.Unclock();
}
void VkRenderState::DrawIndexed(int dt, int index, int count, bool apply)
{
if (apply)
{
Apply(dt);
}
drawcalls.Clock();
//mCommandBuffer->drawIndexed(count, 1, 0, index * (int)sizeof(uint32_t), 0);
drawcalls.Unclock();
}
bool VkRenderState::SetDepthClamp(bool on)
{
bool lastValue = mLastDepthClamp;
mLastDepthClamp = on;
return lastValue;
}
void VkRenderState::SetDepthMask(bool on)
{
}
void VkRenderState::SetDepthFunc(int func)
{
}
void VkRenderState::SetDepthRange(float min, float max)
{
}
void VkRenderState::SetColorMask(bool r, bool g, bool b, bool a)
{
}
void VkRenderState::EnableDrawBufferAttachments(bool on)
{
}
void VkRenderState::SetStencil(int offs, int op, int flags)
{
}
void VkRenderState::SetCulling(int mode)
{
}
void VkRenderState::EnableClipDistance(int num, bool state)
{
}
void VkRenderState::Clear(int targets)
{
}
void VkRenderState::EnableStencil(bool on)
{
}
void VkRenderState::SetScissor(int x, int y, int w, int h)
{
}
void VkRenderState::SetViewport(int x, int y, int w, int h)
{
}
void VkRenderState::EnableDepthTest(bool on)
{
}
void VkRenderState::EnableMultisampling(bool on)
{
}
void VkRenderState::EnableLineSmooth(bool on)
{
}
void VkRenderState::Apply(int dt)
{
#if 0
auto fb = GetVulkanFrameBuffer();
bool changingRenderPass = false; // To do: decide if the state matches current bound renderpass
if (!mCommandBuffer)
{
mCommandBuffer = fb->GetDrawCommands();
changingRenderPass = true;
}
else if (changingRenderPass)
{
mCommandBuffer->endRenderPass();
}
if (changingRenderPass)
{
RenderPassBegin beginInfo;
beginInfo.setRenderPass(renderPass);
beginInfo.setRenderArea(0, 0, SCREENWIDTH, SCREENHEIGHT);
beginInfo.setFramebuffer(framebuffer);
beginInfo.addClearColor(0.0f, 0.0f, 0.0f, 1.0f);
beginInfo.addClearDepthStencil(0.0f, 0);
mCommandBuffer->beginRenderPass(beginInfo);
mCommandBuffer->bindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
}
// To do: maybe only push the subset that changed?
mCommandBuffer->pushConstants(pipelineLayout, VK_PIPELINE_STAGE_VERTEX_SHADER_BIT | VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, 0, (uint32_t)sizeof(PushConstants), &mPushConstants);
#endif
}

View file

@ -0,0 +1,45 @@
#pragma once
#include "vulkan/system/vk_buffers.h"
#include "vulkan/shaders/vk_shader.h"
#include "name.h"
#include "hwrenderer/scene/hw_drawstructs.h"
#include "hwrenderer/scene/hw_renderstate.h"
#include "hwrenderer/textures/hw_material.h"
class VkRenderState : public FRenderState
{
public:
// Draw commands
void ClearScreen() override;
void Draw(int dt, int index, int count, bool apply = true) override;
void DrawIndexed(int dt, int index, int count, bool apply = true) override;
// Immediate render state change commands. These only change infrequently and should not clutter the render state.
bool SetDepthClamp(bool on) override;
void SetDepthMask(bool on) override;
void SetDepthFunc(int func) override;
void SetDepthRange(float min, float max) override;
void SetColorMask(bool r, bool g, bool b, bool a) override;
void EnableDrawBufferAttachments(bool on) override;
void SetStencil(int offs, int op, int flags = -1) override;
void SetCulling(int mode) override;
void EnableClipDistance(int num, bool state) override;
void Clear(int targets) override;
void EnableStencil(bool on) override;
void SetScissor(int x, int y, int w, int h) override;
void SetViewport(int x, int y, int w, int h) override;
void EnableDepthTest(bool on) override;
void EnableMultisampling(bool on) override;
void EnableLineSmooth(bool on) override;
private:
void Apply(int dt);
bool mLastDepthClamp = true;
VulkanCommandBuffer *mCommandBuffer = nullptr;
PushConstants mPushConstants;
};

View file

@ -0,0 +1,32 @@
#include "vk_shader.h"
#include "vulkan/system/vk_builders.h"
#include <ShaderLang.h>
VkShaderManager::VkShaderManager()
{
ShInitialize();
#if 0
{
const char *lumpName = "shaders/glsl/screenquad.vp";
int lump = Wads.CheckNumForFullName(lumpName, 0);
if (lump == -1) I_FatalError("Unable to load '%s'", lumpName);
FString code = Wads.ReadLump(lump).GetString().GetChars();
FString patchedCode;
patchedCode.AppendFormat("#version %d\n", 450);
patchedCode << "#line 1\n";
patchedCode << code;
ShaderBuilder builder;
builder.setVertexShader(patchedCode);
auto shader = builder.create(dev);
}
#endif
}
VkShaderManager::~VkShaderManager()
{
ShFinalize();
}

View file

@ -0,0 +1,59 @@
#pragma once
#include "utility/vectors.h"
#include "r_data/matrix.h"
#include "name.h"
struct PushConstants
{
int uTextureMode;
FVector2 uClipSplit;
float uAlphaThreshold;
// colors
FVector4 uObjectColor;
FVector4 uObjectColor2;
FVector4 uDynLightColor;
FVector4 uAddColor;
FVector4 uFogColor;
float uDesaturationFactor;
float uInterpolationFactor;
// Glowing walls stuff
FVector4 uGlowTopPlane;
FVector4 uGlowTopColor;
FVector4 uGlowBottomPlane;
FVector4 uGlowBottomColor;
FVector4 uGradientTopPlane;
FVector4 uGradientBottomPlane;
FVector4 uSplitTopPlane;
FVector4 uSplitBottomPlane;
// Lighting + Fog
float uLightLevel;
float uFogDensity;
float uLightFactor;
float uLightDist;
int uFogEnabled;
// dynamic lights
int uLightIndex;
// Blinn glossiness and specular level
FVector2 uSpecularMaterial;
// matrices
VSMatrix ModelMatrix;
VSMatrix NormalModelMatrix;
VSMatrix TextureMatrix;
};
class VkShaderManager
{
public:
VkShaderManager();
~VkShaderManager();
};

View file

@ -38,12 +38,14 @@
#include "vk_framebuffer.h"
#include "vk_buffers.h"
#include "vulkan/renderer/vk_renderstate.h"
#include "vulkan/shaders/vk_shader.h"
#include "vulkan/textures/vk_samplers.h"
#include "vulkan/system/vk_builders.h"
#include "vulkan/system/vk_swapchain.h"
#include "doomerrors.h"
#include <ShaderLang.h>
void Draw2D(F2DDrawer *drawer, FRenderState &state);
EXTERN_CVAR(Bool, vid_vsync)
EXTERN_CVAR(Bool, r_drawvoxels)
@ -58,7 +60,6 @@ VulkanFrameBuffer::VulkanFrameBuffer(void *hMonitor, bool fullscreen, VulkanDevi
VulkanFrameBuffer::~VulkanFrameBuffer()
{
ShFinalize();
}
void VulkanFrameBuffer::InitializeState()
@ -70,26 +71,9 @@ void VulkanFrameBuffer::InitializeState()
mViewpoints = new GLViewpointBuffer;
mLights = new FLightBuffer();
ShInitialize();
mShaderManager.reset(new VkShaderManager());
mSamplerManager.reset(new VkSamplerManager(device));
#if 0
{
const char *lumpName = "shaders/glsl/screenquad.vp";
int lump = Wads.CheckNumForFullName(lumpName, 0);
if (lump == -1) I_FatalError("Unable to load '%s'", lumpName);
FString code = Wads.ReadLump(lump).GetString().GetChars();
FString patchedCode;
patchedCode.AppendFormat("#version %d\n", 450);
patchedCode << "#line 1\n";
patchedCode << code;
ShaderBuilder builder;
builder.setVertexShader(patchedCode);
auto shader = builder.create(dev);
}
#endif
mRenderState.reset(new VkRenderState());
}
void VulkanFrameBuffer::Update()
@ -235,6 +219,7 @@ void VulkanFrameBuffer::BeginFrame()
void VulkanFrameBuffer::Draw2D()
{
::Draw2D(&m2DDrawer, *mRenderState);
}
VulkanCommandBuffer *VulkanFrameBuffer::GetUploadCommands()

View file

@ -5,6 +5,8 @@
#include "vk_objects.h"
class VkSamplerManager;
class VkShaderManager;
class VkRenderState;
class VulkanFrameBuffer : public SystemBaseFrameBuffer
{
@ -52,10 +54,12 @@ public:
void Draw2D() override;
private:
std::unique_ptr<VkShaderManager> mShaderManager;
std::unique_ptr<VkSamplerManager> mSamplerManager;
std::unique_ptr<VulkanCommandPool> mGraphicsCommandPool;
std::unique_ptr<VulkanCommandBuffer> mUploadCommands;
std::unique_ptr<VulkanCommandBuffer> mPresentCommands;
std::unique_ptr<VkRenderState> mRenderState;
int camtexcount = 0;