Skip to content

Commit

Permalink
add gl_wireframe and gl_wireframecolor
Browse files Browse the repository at this point in the history
  • Loading branch information
RicardoLuis0 committed Jan 9, 2025
1 parent d8c0e42 commit 0c6c3fc
Show file tree
Hide file tree
Showing 14 changed files with 151 additions and 64 deletions.
2 changes: 2 additions & 0 deletions libraries/ZVulkan/include/zvulkan/vulkanbuilders.h
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,8 @@ class GraphicsPipelineBuilder

GraphicsPipelineBuilder& AddDynamicState(VkDynamicState state);

GraphicsPipelineBuilder& PolygonMode(VkPolygonMode mode) {rasterizer.polygonMode = mode; return *this;};

GraphicsPipelineBuilder& DebugName(const char* name) { debugName = name; return *this; }

std::unique_ptr<VulkanPipeline> Create(VulkanDevice *device);
Expand Down
16 changes: 16 additions & 0 deletions src/common/console/c_cvars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,22 @@ int FColorCVar::ToInt2 (UCVarValue value, ECVarType type)
return ret;
}

DVector4 FColorCVar::asDV4() const
{
PalEntry col;
col.d = Value;

return {col.r / 255.0, col.g / 255.0, col.b / 255.0, col.a / 255.0};
}

FVector4 FColorCVar::asFV4() const
{
PalEntry col;
col.d = Value;

return {col.r / 255.0f, col.g / 255.0f, col.b / 255.0f, col.a / 255.0f};
}

//
// More base cvar stuff
//
Expand Down
5 changes: 5 additions & 0 deletions src/common/console/c_cvars.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "autosegs.h"
#include "name.h"
#include "dobjgc.h"
#include "vectors.h"

class FSerializer; // this needs to go away.
/*
Expand Down Expand Up @@ -452,6 +453,10 @@ class FColorCVar : public FIntCVar
inline operator uint32_t () const { return Value; }
inline uint32_t operator *() const { return Value; }

DVector4 asDV4() const;

FVector4 asFV4() const;

protected:
virtual void DoSet (UCVarValue value, ECVarType type);

Expand Down
5 changes: 4 additions & 1 deletion src/common/rendering/hwrenderer/data/hw_cvars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,7 @@ CUSTOM_CVAR(Int, gl_shadowmap_quality, 512, CVAR_ARCHIVE | CVAR_GLOBALCONFIG)
}
}

CVAR(Bool, gl_strict_gldefs, true, CVAR_GLOBALCONFIG | CVAR_ARCHIVE)
CVAR(Bool, gl_strict_gldefs, true, CVAR_GLOBALCONFIG | CVAR_ARCHIVE)

CVAR(Int, gl_wireframe, 0, CVAR_GLOBALCONFIG | CVAR_ARCHIVE)
CVAR(Color, gl_wireframecolor, -1, CVAR_GLOBALCONFIG | CVAR_ARCHIVE)
4 changes: 3 additions & 1 deletion src/common/rendering/hwrenderer/data/hw_cvars.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,6 @@ EXTERN_CVAR(Bool, gl_notexturefill)
EXTERN_CVAR(Bool, r_radarclipper)
EXTERN_CVAR(Bool, r_dithertransparency)

EXTERN_CVAR(Bool, gl_strict_gldefs)
EXTERN_CVAR(Bool, gl_strict_gldefs)
EXTERN_CVAR(Int, gl_wireframe)
EXTERN_CVAR(Color, gl_wireframecolor)
4 changes: 2 additions & 2 deletions src/common/rendering/hwrenderer/data/hw_meshbuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void MeshBuilder::SetShadowData(const TArray<FFlatVertex>& vertices, const TArra
mIndexes = indexes;
}

void MeshBuilder::Draw(int dt, int index, int count, bool apply)
void MeshBuilder::DoDraw(int dt, int index, int count, bool apply)
{
if (apply)
Apply();
Expand All @@ -37,7 +37,7 @@ void MeshBuilder::Draw(int dt, int index, int count, bool apply)
mDrawLists->mDraws.Push(command);
}

void MeshBuilder::DrawIndexed(int dt, int index, int count, bool apply)
void MeshBuilder::DoDrawIndexed(int dt, int index, int count, bool apply)
{
if (apply)
Apply();
Expand Down
4 changes: 2 additions & 2 deletions src/common/rendering/hwrenderer/data/hw_meshbuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ class MeshBuilder : public FRenderState
int UploadFogballs(const TArray<Fogball>& balls) override { return -1; }

// Draw commands
void Draw(int dt, int index, int count, bool apply = true) override;
void DrawIndexed(int dt, int index, int count, bool apply = true) override;
void DoDraw(int dt, int index, int count, bool apply = true) override;
void DoDrawIndexed(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.
void SetDepthFunc(int func) override;
Expand Down
73 changes: 69 additions & 4 deletions src/common/rendering/hwrenderer/data/hw_renderstate.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ class FRenderState
uint8_t mGradientEnabled : 1;
uint8_t mSplitEnabled : 1;
uint8_t mBrightmapEnabled : 1;
uint8_t mWireframe : 2;

FVector4 mWireframeColor;
FVector4 uObjectColor;

int mLightIndex;
int mBoneIndexBase;
Expand Down Expand Up @@ -166,12 +170,16 @@ class FRenderState

EPassType mPassType = NORMAL_PASS;

virtual void DoDraw(int dt, int index, int count, bool apply) = 0;
virtual void DoDrawIndexed(int dt, int index, int count, bool apply) = 0;
public:

uint64_t firstFrame = 0;

void Reset()
{
mWireframe = 0;
mWireframeColor = toFVector4(PalEntry(0xffffffff));
mTextureEnabled = true;
mBrightmapEnabled = mGradientEnabled = mFogEnabled = mGlowEnabled = false;
mFogColor = 0xffffffff;
Expand All @@ -183,7 +191,7 @@ class FRenderState
mSurfaceUniforms.uAlphaThreshold = 0.5f;
mSplitEnabled = false;
mSurfaceUniforms.uAddColor = toFVector4(PalEntry(0));
mSurfaceUniforms.uObjectColor = toFVector4(PalEntry(0xffffffff));
uObjectColor = toFVector4(PalEntry(0xffffffff));
mSurfaceUniforms.uObjectColor2 = toFVector4(PalEntry(0));
mSurfaceUniforms.uTextureBlendColor = toFVector4(PalEntry(0));
mSurfaceUniforms.uTextureAddColor = toFVector4(PalEntry(0));
Expand Down Expand Up @@ -233,6 +241,12 @@ class FRenderState
mSurfaceUniforms.uVertexNormal = { norm.X, norm.Y, norm.Z, 0.f };
}

void SetWireframe(int mode, FVector4 color)
{
mWireframe = mode;
mWireframeColor = color;
}

void SetNormal(float x, float y, float z)
{
mSurfaceUniforms.uVertexNormal = { x, y, z, 0.f };
Expand Down Expand Up @@ -409,7 +423,7 @@ class FRenderState

void SetObjectColor(PalEntry pe)
{
mSurfaceUniforms.uObjectColor = toFVector4(pe);
uObjectColor = toFVector4(pe);
}

void SetObjectColor2(PalEntry pe)
Expand Down Expand Up @@ -672,8 +686,59 @@ class FRenderState

// Draw commands
virtual void ClearScreen() = 0;
virtual void Draw(int dt, int index, int count, bool apply = true) = 0;
virtual void DrawIndexed(int dt, int index, int count, bool apply = true) = 0;

void Draw(int dt, int index, int count, bool apply = true)
{
if(mWireframe == 0)
{
mSurfaceUniforms.uObjectColor = uObjectColor;
DoDraw(dt, index, count, apply);
}
else if(mWireframe == 1)
{
mSurfaceUniforms.uObjectColor = mWireframeColor;
DoDraw(dt, index, count, apply);
}
else //if(mWireframe == 2)
{
mWireframe = 0;
mSurfaceUniforms.uObjectColor = uObjectColor;
DoDraw(dt, index, count, true);

mWireframe = 1;
mSurfaceUniforms.uObjectColor = mWireframeColor;
DoDraw(dt, index, count, true);

mWireframe = 2;
}
}

void DrawIndexed(int dt, int index, int count, bool apply = true)
{
if(mWireframe == 0)
{
mSurfaceUniforms.uObjectColor = uObjectColor;
DoDrawIndexed(dt, index, count, apply);
}
else if(mWireframe == 1)
{
mSurfaceUniforms.uObjectColor = mWireframeColor;
DoDrawIndexed(dt, index, count, apply);
}
else //if(mWireframe == 2)
{
mWireframe = 0;
mSurfaceUniforms.uObjectColor = uObjectColor;
DoDrawIndexed(dt, index, count, true);

mWireframe = 1;
mSurfaceUniforms.uObjectColor = mWireframeColor;
DoDrawIndexed(dt, index, count, true);

mWireframe = 2;
}
}


// Immediate render state change commands. These only change infrequently and should not clutter the render state.
virtual bool SetDepthClamp(bool on) = 0; // Deactivated only by skyboxes.
Expand Down
2 changes: 2 additions & 0 deletions src/common/rendering/vulkan/pipelines/vk_renderpass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,8 @@ std::unique_ptr<VulkanPipeline> VkRenderPassSetup::CreatePipeline(const VkPipeli
GraphicsPipelineBuilder builder;
builder.Cache(fb->GetRenderPassManager()->GetCache());

builder.PolygonMode(key.DrawLine ? VK_POLYGON_MODE_LINE : VK_POLYGON_MODE_FILL);

VkShaderProgram *program = fb->GetShaderManager()->Get(key.ShaderKey);
builder.AddVertexShader(program->vert.get());
if (program->frag)
Expand Down
3 changes: 2 additions & 1 deletion src/common/rendering/vulkan/pipelines/vk_renderpass.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class VkPipelineKey
uint64_t DepthFunc : 2;
uint64_t StencilTest : 1;
uint64_t StencilPassOp : 2;
uint64_t Unused : 46;
uint64_t DrawLine : 1;
uint64_t Unused : 45;
};
uint64_t AsQWORD = 0;
};
Expand Down
4 changes: 0 additions & 4 deletions src/common/rendering/vulkan/vk_renderdevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,7 @@ void VulkanRenderDevice::InitializeState()

mDescriptorSetManager->Init();

#ifdef __APPLE__
mRenderState = std::make_unique<VkRenderStateMolten>(this);
#else
mRenderState = std::make_unique<VkRenderState>(this);
#endif
}

void VulkanRenderDevice::Update()
Expand Down
73 changes: 35 additions & 38 deletions src/common/rendering/vulkan/vk_renderstate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,37 @@ void VkRenderState::ClearScreen()
mCommandBuffer->draw(4, 1, vertices.second, 0);
}

void VkRenderState::Draw(int dt, int index, int count, bool apply)
void VkRenderState::DoDraw(int dt, int index, int count, bool apply)
{
if (apply || mNeedApply)
Apply(dt);
#ifdef __APPLE__
// moltenvk doesn't support drawing triangle fans
if (dt == DT_TriangleFan)
{
IBuffer* oldIndexBuffer = mIndexBuffer;
mIndexBuffer = fb->GetBufferManager()->FanToTrisIndexBuffer.get();

if (apply || mNeedApply)
Apply(DT_Triangles);
else
ApplyVertexBuffers();

mCommandBuffer->drawIndexed((count - 2) * 3, 1, 0, index, 0);

mIndexBuffer = oldIndexBuffer;
}
else
{
#endif
if (apply || mNeedApply)
Apply(dt);

mCommandBuffer->draw(count, 1, index, 0);
mCommandBuffer->draw(count, 1, index, 0);
#ifdef __APPLE__
}
#endif
}

void VkRenderState::DrawIndexed(int dt, int index, int count, bool apply)
void VkRenderState::DoDrawIndexed(int dt, int index, int count, bool apply)
{
if (apply || mNeedApply)
Apply(dt);
Expand Down Expand Up @@ -226,10 +248,11 @@ void VkRenderState::ApplyRenderPass(int dt)
// Find a pipeline that matches our state
VkPipelineKey pipelineKey;
pipelineKey.DrawType = dt;
pipelineKey.DrawLine = mDrawLine || mWireframe;
pipelineKey.VertexFormat = mVertexBuffer ? static_cast<VkHardwareVertexBuffer*>(mVertexBuffer)->VertexFormat : mRSBuffers->Flatbuffer.VertexFormat;
pipelineKey.RenderStyle = mRenderStyle;
pipelineKey.DepthTest = mDepthTest;
pipelineKey.DepthWrite = mDepthTest && mDepthWrite;
pipelineKey.DepthTest = mDepthTest && !mWireframe;
pipelineKey.DepthWrite = mDepthTest && !mWireframe && mDepthWrite;
pipelineKey.DepthFunc = mDepthFunc;
pipelineKey.DepthClamp = mDepthClamp;
pipelineKey.DepthBias = !(mBias.mFactor == 0 && mBias.mUnits == 0);
Expand All @@ -247,7 +270,7 @@ void VkRenderState::ApplyRenderPass(int dt)
{
int effectState = mMaterial.mOverrideShader >= 0 ? mMaterial.mOverrideShader : (mMaterial.mMaterial ? mMaterial.mMaterial->GetShaderIndex() : 0);
pipelineKey.ShaderKey.SpecialEffect = EFF_NONE;
pipelineKey.ShaderKey.EffectState = mTextureEnabled ? effectState : SHADER_NoTexture;
pipelineKey.ShaderKey.EffectState = (mTextureEnabled && !mWireframe) ? effectState : SHADER_NoTexture;
if (r_skipmats && pipelineKey.ShaderKey.EffectState >= 3 && pipelineKey.ShaderKey.EffectState <= 4)
pipelineKey.ShaderKey.EffectState = 0;
pipelineKey.ShaderKey.AlphaTest = mSurfaceUniforms.uAlphaThreshold >= 0.f;
Expand Down Expand Up @@ -1080,15 +1103,16 @@ void VkRenderState::ApplyLevelMeshPipeline(VulkanCommandBuffer* cmdbuffer, VkPip
pipelineKey.ShaderKey.GBufferPass = mRenderTarget.DrawBuffers > 1;

// State overridden by the renderstate drawing the mesh
pipelineKey.DepthTest = mDepthTest;
pipelineKey.DepthWrite = mDepthTest && mDepthWrite;
pipelineKey.DrawLine = mDrawLine || mWireframe;
pipelineKey.DepthTest = mDepthTest && !mWireframe;
pipelineKey.DepthWrite = mDepthTest && !mWireframe && mDepthWrite;
pipelineKey.DepthClamp = mDepthClamp;
pipelineKey.DepthBias = !(mBias.mFactor == 0 && mBias.mUnits == 0);
pipelineKey.StencilTest = mStencilTest;
pipelineKey.StencilPassOp = mStencilOp;
pipelineKey.ColorMask = mColorMask;
pipelineKey.CullMode = mCullMode;
if (!mTextureEnabled)
if (!mTextureEnabled || mWireframe)
pipelineKey.ShaderKey.EffectState = SHADER_NoTexture;

mPipelineKey = pipelineKey;
Expand All @@ -1110,30 +1134,3 @@ void VkRenderState::ApplyLevelMeshPipeline(VulkanCommandBuffer* cmdbuffer, VkPip
cmdbuffer->bindDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, layout, 2, descriptors->GetBindlessSet());
cmdbuffer->pushConstants(layout, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, 0, (uint32_t)sizeof(PushConstants), &pushConstants);
}

/////////////////////////////////////////////////////////////////////////////

void VkRenderStateMolten::Draw(int dt, int index, int count, bool apply)
{
if (dt == DT_TriangleFan)
{
IBuffer* oldIndexBuffer = mIndexBuffer;
mIndexBuffer = fb->GetBufferManager()->FanToTrisIndexBuffer.get();

if (apply || mNeedApply)
Apply(DT_Triangles);
else
ApplyVertexBuffers();

mCommandBuffer->drawIndexed((count - 2) * 3, 1, 0, index, 0);

mIndexBuffer = oldIndexBuffer;
}
else
{
if (apply || mNeedApply)
Apply(dt);

mCommandBuffer->draw(count, 1, index, 0);
}
}
Loading

0 comments on commit 0c6c3fc

Please sign in to comment.