chore: make all code conventions consistent, slightly clean up some things and fix bugs where some stuff would visually look messed up

This commit is contained in:
3UR
2026-03-03 00:23:38 +10:00
parent 26f8477e4b
commit 3b406a063d
7 changed files with 556 additions and 575 deletions
+84 -106
View File
@@ -1,13 +1,12 @@
#pragma once
#include "stdafx.h"
#include "Renderer.h"
#include <cstddef>
#include <cstring>
#include <limits>
ID3D11BlendState *Renderer::GetManagedBlendState()
{
Context &c = this->getContext();
Context &c = getContext();
const D3D11_RENDER_TARGET_BLEND_DESC &rtBlend = c.blendDesc.RenderTarget[0];
const int key = (rtBlend.BlendEnable ? 1 : 0) | ((static_cast<int>(rtBlend.SrcBlend) & 0x1F) << 1) |
@@ -15,11 +14,9 @@ ID3D11BlendState *Renderer::GetManagedBlendState()
auto it = managedBlendStates.find(key);
if (it != managedBlendStates.end())
{
return it->second;
}
ID3D11BlendState *state = nullptr;
ID3D11BlendState *state = NULL;
m_pDevice->CreateBlendState(&c.blendDesc, &state);
managedBlendStates.emplace(key, state);
return state;
@@ -27,18 +24,16 @@ ID3D11BlendState *Renderer::GetManagedBlendState()
ID3D11DepthStencilState *Renderer::GetManagedDepthStencilState()
{
Context &c = this->getContext();
Context &c = getContext();
const int key = (c.depthStencilDesc.DepthEnable ? 2 : 0) | ((static_cast<int>(c.depthStencilDesc.DepthFunc) & 0x0F) << 2) |
(c.depthStencilDesc.DepthWriteMask == D3D11_DEPTH_WRITE_MASK_ALL ? 1 : 0);
auto it = managedDepthStencilStates.find(key);
if (it != managedDepthStencilStates.end())
{
return it->second;
}
ID3D11DepthStencilState *state = nullptr;
ID3D11DepthStencilState *state = NULL;
m_pDevice->CreateDepthStencilState(&c.depthStencilDesc, &state);
managedDepthStencilStates.emplace(key, state);
return state;
@@ -46,7 +41,7 @@ ID3D11DepthStencilState *Renderer::GetManagedDepthStencilState()
ID3D11RasterizerState *Renderer::GetManagedRasterizerState()
{
Context &c = this->getContext();
Context &c = getContext();
const int key = (static_cast<std::uint8_t>(c.rasterizerDesc.DepthBias)) |
(static_cast<std::uint8_t>(static_cast<int>(c.rasterizerDesc.SlopeScaledDepthBias)) << 8) |
@@ -54,11 +49,9 @@ ID3D11RasterizerState *Renderer::GetManagedRasterizerState()
auto it = managedRasterizerStates.find(key);
if (it != managedRasterizerStates.end())
{
return it->second;
}
ID3D11RasterizerState *state = nullptr;
ID3D11RasterizerState *state = NULL;
m_pDevice->CreateRasterizerState(&c.rasterizerDesc, &state);
managedRasterizerStates.emplace(key, state);
return state;
@@ -66,14 +59,12 @@ ID3D11RasterizerState *Renderer::GetManagedRasterizerState()
ID3D11SamplerState *Renderer::GetManagedSamplerState()
{
Context &c = this->getContext();
Context &c = getContext();
const int key = m_textures[c.textureIdx].samplerParams;
auto it = managedSamplerStates.find(key);
if (it != managedSamplerStates.end())
{
return it->second;
}
const bool clampU = (key & 0x01) != 0;
const bool clampV = (key & 0x02) != 0;
@@ -96,7 +87,7 @@ ID3D11SamplerState *Renderer::GetManagedSamplerState()
desc.MinLOD = -(std::numeric_limits<float>::max)();
desc.MaxLOD = (std::numeric_limits<float>::max)();
ID3D11SamplerState *state = nullptr;
ID3D11SamplerState *state = NULL;
m_pDevice->CreateSamplerState(&desc, &state);
managedSamplerStates.emplace(key, state);
return state;
@@ -104,37 +95,37 @@ ID3D11SamplerState *Renderer::GetManagedSamplerState()
void Renderer::StateSetFogEnable(bool enable)
{
Context &c = this->getContext();
c.fogEnabled = enable ? 1 : 0;
Context &c = getContext();
c.fogEnabled = enable ? TRUE : FALSE;
}
void Renderer::StateSetFogMode(int mode)
{
Context &c = this->getContext();
Context &c = getContext();
c.fogMode = mode;
}
void Renderer::StateSetFogNearDistance(float dist)
{
Context &c = this->getContext();
Context &c = getContext();
c.fogNearDistance = dist;
}
void Renderer::StateSetFogFarDistance(float dist)
{
Context &c = this->getContext();
Context &c = getContext();
c.fogFarDistance = dist;
}
void Renderer::StateSetFogDensity(float density)
{
Context &c = this->getContext();
Context &c = getContext();
c.fogDensity = density;
}
void Renderer::StateSetFogColour(float red, float green, float blue)
{
Context &c = this->getContext();
Context &c = getContext();
c.fogColourRed = red;
c.fogColourBlue = blue;
c.fogColourGreen = green;
@@ -144,25 +135,23 @@ void Renderer::UpdateViewportState() {}
void Renderer::StateSetLightingEnable(bool enable)
{
Context &c = this->getContext();
if (c.commandBuffer != nullptr && c.commandBuffer->isActive != 0)
Context &c = getContext();
if (c.commandBuffer != NULL && c.commandBuffer->isActive != 0)
{
c.commandBuffer->SetLightingEnable(enable);
return;
}
c.lightingEnabled = enable ? 1 : 0;
c.lightingEnabled = enable ? TRUE : FALSE;
}
void Renderer::StateSetLightColour(int light, float red, float green, float blue)
{
if (light >= 2)
{
return;
}
Context &c = this->getContext();
if (c.commandBuffer != nullptr && c.commandBuffer->isActive != 0)
Context &c = getContext();
if (c.commandBuffer != NULL && c.commandBuffer->isActive != 0)
{
c.commandBuffer->SetLightColour(light, red, green, blue);
return;
@@ -172,13 +161,13 @@ void Renderer::StateSetLightColour(int light, float red, float green, float blue
c.lightColour[light].y = green;
c.lightColour[light].z = blue;
c.lightColour[light].w = 1.0f;
c.lightingDirty = 1;
c.lightingDirty = TRUE;
}
void Renderer::StateSetLightAmbientColour(float red, float green, float blue)
{
Context &c = this->getContext();
if (c.commandBuffer != nullptr && c.commandBuffer->isActive != 0)
Context &c = getContext();
if (c.commandBuffer != NULL && c.commandBuffer->isActive != 0)
{
c.commandBuffer->SetLightAmbientColour(red, green, blue);
return;
@@ -188,31 +177,29 @@ void Renderer::StateSetLightAmbientColour(float red, float green, float blue)
c.lightAmbientColour.y = green;
c.lightAmbientColour.z = blue;
c.lightAmbientColour.w = 1.0f;
c.lightingDirty = 1;
c.lightingDirty = TRUE;
}
void Renderer::StateSetLightEnable(int light, bool enable)
{
if (light >= 2)
{
return;
}
Context &c = this->getContext();
if (c.commandBuffer != nullptr && c.commandBuffer->isActive != 0)
Context &c = getContext();
if (c.commandBuffer != NULL && c.commandBuffer->isActive != 0)
{
c.commandBuffer->SetLightEnable(light, enable);
return;
}
c.lightEnabled[light] = enable ? 1 : 0;
c.lightingDirty = 1;
c.lightEnabled[light] = enable ? TRUE : FALSE;
c.lightingDirty = TRUE;
}
void Renderer::StateSetColour(float r, float g, float b, float a)
{
Context &c = this->getContext();
if (c.commandBuffer != nullptr && c.commandBuffer->isActive != 0)
Context &c = getContext();
if (c.commandBuffer != NULL && c.commandBuffer->isActive != 0)
{
c.commandBuffer->SetColor(r, g, b, a);
return;
@@ -229,35 +216,35 @@ void Renderer::StateSetColour(float r, float g, float b, float a)
void Renderer::StateSetDepthMask(bool enable)
{
Context &c = this->getContext();
if (c.commandBuffer != nullptr && c.commandBuffer->isActive != 0)
Context &c = getContext();
if (c.commandBuffer != NULL && c.commandBuffer->isActive != 0)
{
c.commandBuffer->SetDepthMask(enable);
return;
}
c.depthStencilDesc.DepthWriteMask = enable ? D3D11_DEPTH_WRITE_MASK_ALL : D3D11_DEPTH_WRITE_MASK_ZERO;
c.m_pDeviceContext->OMSetDepthStencilState(this->GetManagedDepthStencilState(), 0);
c.depthWriteEnabled = enable ? 1 : 0;
c.m_pDeviceContext->OMSetDepthStencilState(GetManagedDepthStencilState(), 0);
c.depthWriteEnabled = enable ? TRUE : FALSE;
}
void Renderer::StateSetBlendEnable(bool enable)
{
Context &c = this->getContext();
if (c.commandBuffer != nullptr && c.commandBuffer->isActive != 0)
Context &c = getContext();
if (c.commandBuffer != NULL && c.commandBuffer->isActive != 0)
{
c.commandBuffer->SetBlendEnable(enable);
return;
}
c.blendDesc.RenderTarget[0].BlendEnable = enable ? TRUE : FALSE;
c.m_pDeviceContext->OMSetBlendState(this->GetManagedBlendState(), c.blendFactor, 0xFFFFFFFFu);
c.m_pDeviceContext->OMSetBlendState(GetManagedBlendState(), c.blendFactor, 0xFFFFFFFFu);
}
void Renderer::StateSetBlendFunc(int src, int dst)
{
Context &c = this->getContext();
if (c.commandBuffer != nullptr && c.commandBuffer->isActive != 0)
Context &c = getContext();
if (c.commandBuffer != NULL && c.commandBuffer->isActive != 0)
{
c.commandBuffer->SetBlendFunc(src, dst);
return;
@@ -265,13 +252,13 @@ void Renderer::StateSetBlendFunc(int src, int dst)
c.blendDesc.RenderTarget[0].SrcBlend = static_cast<D3D11_BLEND>(src);
c.blendDesc.RenderTarget[0].DestBlend = static_cast<D3D11_BLEND>(dst);
c.m_pDeviceContext->OMSetBlendState(this->GetManagedBlendState(), c.blendFactor, 0xFFFFFFFFu);
c.m_pDeviceContext->OMSetBlendState(GetManagedBlendState(), c.blendFactor, 0xFFFFFFFFu);
}
void Renderer::StateSetBlendFactor(unsigned int colour)
{
Context &c = this->getContext();
if (c.commandBuffer != nullptr && c.commandBuffer->isActive != 0)
Context &c = getContext();
if (c.commandBuffer != NULL && c.commandBuffer->isActive != 0)
{
c.commandBuffer->SetBlendFactor(colour);
return;
@@ -282,12 +269,12 @@ void Renderer::StateSetBlendFactor(unsigned int colour)
c.blendFactor[1] = static_cast<float>((colour >> 8) & 0xFFu) / scale;
c.blendFactor[2] = static_cast<float>((colour >> 16) & 0xFFu) / scale;
c.blendFactor[3] = static_cast<float>((colour >> 24) & 0xFFu) / scale;
c.m_pDeviceContext->OMSetBlendState(this->GetManagedBlendState(), c.blendFactor, 0xFFFFFFFFu);
c.m_pDeviceContext->OMSetBlendState(GetManagedBlendState(), c.blendFactor, 0xFFFFFFFFu);
}
void Renderer::StateSetAlphaFunc(int, float param)
{
Context &c = this->getContext();
Context &c = getContext();
c.alphaReference = param;
const float alpha[4] = {0.0f, 0.0f, 0.0f, c.alphaTestEnabled ? c.alphaReference : 0.0f};
@@ -299,51 +286,47 @@ void Renderer::StateSetAlphaFunc(int, float param)
void Renderer::StateSetDepthFunc(int func)
{
Context &c = this->getContext();
if (c.commandBuffer != nullptr && c.commandBuffer->isActive != 0)
Context &c = getContext();
if (c.commandBuffer != NULL && c.commandBuffer->isActive != 0)
{
c.commandBuffer->SetDepthFunc(func);
return;
}
c.depthStencilDesc.DepthFunc = static_cast<D3D11_COMPARISON_FUNC>(func);
c.m_pDeviceContext->OMSetDepthStencilState(this->GetManagedDepthStencilState(), 0);
c.m_pDeviceContext->OMSetDepthStencilState(GetManagedDepthStencilState(), 0);
}
void Renderer::StateSetFaceCull(bool enable)
{
Context &c = this->getContext();
if (c.commandBuffer != nullptr && c.commandBuffer->isActive != 0)
Context &c = getContext();
if (c.commandBuffer != NULL && c.commandBuffer->isActive != 0)
{
c.commandBuffer->SetFaceCull(enable);
return;
}
c.rasterizerDesc.CullMode = enable ? D3D11_CULL_BACK : D3D11_CULL_NONE;
c.m_pDeviceContext->RSSetState(this->GetManagedRasterizerState());
c.faceCullEnabled = enable ? 1 : 0;
c.m_pDeviceContext->RSSetState(GetManagedRasterizerState());
c.faceCullEnabled = enable ? TRUE : FALSE;
}
void Renderer::StateSetFaceCullCW(bool enable)
{
Context &c = this->getContext();
Context &c = getContext();
if (c.faceCullEnabled)
{
c.rasterizerDesc.CullMode = enable ? D3D11_CULL_BACK : D3D11_CULL_FRONT;
}
else
{
c.rasterizerDesc.CullMode = D3D11_CULL_NONE;
}
c.m_pDeviceContext->RSSetState(this->GetManagedRasterizerState());
c.m_pDeviceContext->RSSetState(GetManagedRasterizerState());
}
void Renderer::StateSetLineWidth(float) {}
void Renderer::StateSetWriteEnable(bool red, bool green, bool blue, bool alpha)
{
Context &c = this->getContext();
Context &c = getContext();
std::uint8_t mask = 0;
mask |= red ? 0x1 : 0;
@@ -352,27 +335,27 @@ void Renderer::StateSetWriteEnable(bool red, bool green, bool blue, bool alpha)
mask |= alpha ? 0x8 : 0;
c.blendDesc.RenderTarget[0].RenderTargetWriteMask = mask;
c.m_pDeviceContext->OMSetBlendState(this->GetManagedBlendState(), c.blendFactor, 0xFFFFFFFFu);
c.m_pDeviceContext->OMSetBlendState(GetManagedBlendState(), c.blendFactor, 0xFFFFFFFFu);
}
void Renderer::StateSetDepthTestEnable(bool enable)
{
Context &c = this->getContext();
if (c.commandBuffer != nullptr && c.commandBuffer->isActive != 0)
Context &c = getContext();
if (c.commandBuffer != NULL && c.commandBuffer->isActive != 0)
{
c.commandBuffer->SetDepthTestEnable(enable);
return;
}
c.depthStencilDesc.DepthEnable = enable ? TRUE : FALSE;
c.m_pDeviceContext->OMSetDepthStencilState(this->GetManagedDepthStencilState(), 0);
c.depthTestEnabled = enable ? 1 : 0;
c.m_pDeviceContext->OMSetDepthStencilState(GetManagedDepthStencilState(), 0);
c.depthTestEnabled = enable ? TRUE : FALSE;
}
void Renderer::StateSetAlphaTestEnable(bool enable)
{
Context &c = this->getContext();
c.alphaTestEnabled = enable ? 1 : 0;
Context &c = getContext();
c.alphaTestEnabled = enable ? TRUE : FALSE;
const float alpha[4] = {0.0f, 0.0f, 0.0f, enable ? c.alphaReference : 0.0f};
D3D11_MAPPED_SUBRESOURCE mapped = {};
@@ -383,17 +366,17 @@ void Renderer::StateSetAlphaTestEnable(bool enable)
void Renderer::StateSetDepthSlopeAndBias(float slope, float bias)
{
Context &c = this->getContext();
Context &c = getContext();
const float scale = 65536.0f;
c.rasterizerDesc.DepthBias = static_cast<int>(bias * scale);
c.rasterizerDesc.SlopeScaledDepthBias = slope * scale;
c.m_pDeviceContext->RSSetState(this->GetManagedRasterizerState());
c.m_pDeviceContext->RSSetState(GetManagedRasterizerState());
}
void Renderer::UpdateFogState()
{
Context &c = this->getContext();
Context &c = getContext();
ID3D11DeviceContext *d3d11 = c.m_pDeviceContext;
float fogParams[4] = {};
@@ -426,7 +409,7 @@ void Renderer::UpdateFogState()
void Renderer::StateSetVertexTextureUV(float u, float v)
{
Context &c = this->getContext();
Context &c = getContext();
const float texgen[4] = {u - 1.0f, v - 1.0f, 0.0f, 0.0f};
D3D11_MAPPED_SUBRESOURCE mapped = {};
@@ -437,7 +420,7 @@ void Renderer::StateSetVertexTextureUV(float u, float v)
void Renderer::UpdateTexGenState()
{
Context &c = this->getContext();
Context &c = getContext();
D3D11_MAPPED_SUBRESOURCE mapped = {};
c.m_pDeviceContext->Map(c.m_texGenMatricesBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped);
@@ -447,7 +430,7 @@ void Renderer::UpdateTexGenState()
void Renderer::UpdateLightingState()
{
Context &c = this->getContext();
Context &c = getContext();
if (!c.lightingDirty || !c.lightingEnabled)
{
return;
@@ -471,18 +454,16 @@ void Renderer::UpdateLightingState()
std::memcpy(mapped.pData, c.lightDirection, lightingBytes);
c.m_pDeviceContext->Unmap(c.m_lightingStateBuffer, 0);
c.lightingDirty = 0;
c.lightingDirty = FALSE;
}
void Renderer::StateSetLightDirection(int light, float x, float y, float z)
{
if (light >= 2)
{
return;
}
Context &c = this->getContext();
if (c.commandBuffer != nullptr && c.commandBuffer->isActive != 0)
Context &c = getContext();
if (c.commandBuffer != NULL && c.commandBuffer->isActive != 0)
{
c.commandBuffer->SetLightDirection(light, x, y, z);
return;
@@ -495,12 +476,12 @@ void Renderer::StateSetLightDirection(int light, float x, float y, float z)
const DirectX::XMVECTOR normalized = DirectX::XMVector3Normalize(transformed);
DirectX::XMStoreFloat4(&c.lightDirection[light], normalized);
c.lightingDirty = 1;
c.lightingDirty = TRUE;
}
void Renderer::StateSetViewport(C4JRender::eViewportType viewportType)
{
this->getContext();
getContext();
m_ViewportType = viewportType;
const float fullWidth = static_cast<float>(backBufferWidth);
@@ -569,14 +550,14 @@ void Renderer::StateSetEnableViewportClipPlanes(bool) {}
void Renderer::StateSetTexGenCol(int col, float x, float y, float z, float w, bool eyeSpace)
{
Context &c = this->getContext();
Context &c = getContext();
DirectX::XMVECTOR plane = DirectX::XMVectorSet(x, y, z, w);
if (eyeSpace)
{
DirectX::XMFLOAT4X4 modelView;
std::memset(&modelView, 0, sizeof(modelView));
std::memcpy(&modelView, this->MatrixGet(MATRIX_MODE_MODELVIEW), sizeof(modelView));
std::memcpy(&modelView, MatrixGet(MATRIX_MODE_MODELVIEW), sizeof(modelView));
DirectX::XMVECTOR determinant = DirectX::XMVectorZero();
const DirectX::XMMATRIX inverse = DirectX::XMMatrixInverse(&determinant, DirectX::XMLoadFloat4x4(&modelView));
@@ -604,7 +585,7 @@ void Renderer::StateSetTexGenCol(int col, float x, float y, float z, float w, bo
void Renderer::StateSetStencil(D3D11_COMPARISON_FUNC function, uint8_t stencil_ref, uint8_t stencil_func_mask, uint8_t stencil_write_mask)
{
Context &c = this->getContext();
Context &c = getContext();
D3D11_DEPTH_STENCIL_DESC desc = c.depthStencilDesc;
desc.StencilEnable = TRUE;
@@ -613,26 +594,23 @@ void Renderer::StateSetStencil(D3D11_COMPARISON_FUNC function, uint8_t stencil_r
desc.FrontFace.StencilFunc = function;
desc.BackFace.StencilFunc = function;
ID3D11DepthStencilState *state = nullptr;
ID3D11DepthStencilState *state = NULL;
m_pDevice->CreateDepthStencilState(&desc, &state);
m_pDeviceContext->OMSetDepthStencilState(state, stencil_ref);
if (state != nullptr)
{
state->Release();
}
if (state != NULL) state->Release();
}
void Renderer::StateSetForceLOD(int LOD)
{
Context &c = this->getContext();
Context &c = getContext();
c.forcedLOD = LOD;
}
void Renderer::StateUpdate()
{
Context &c = this->getContext();
this->StateSetFaceCull(c.faceCullEnabled != 0);
this->StateSetDepthMask(c.depthWriteEnabled != 0);
this->StateSetDepthTestEnable(c.depthTestEnabled != 0);
this->StateSetAlphaTestEnable(c.alphaTestEnabled != 0);
Context &c = getContext();
StateSetFaceCull(c.faceCullEnabled);
StateSetDepthMask(c.depthWriteEnabled);
StateSetDepthTestEnable(c.depthTestEnabled);
StateSetAlphaTestEnable(c.alphaTestEnabled);
}