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
+61 -95
View File
@@ -1,17 +1,19 @@
#pragma once
#include "stdafx.h"
#include "Renderer.h"
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <new>
Renderer::CommandBuffer::CommandBuffer(bool full)
: m_vertexBuffer(nullptr), m_vertexData(nullptr), m_vertexDataLength(0), m_commands(), m_allocated(0x1000), isActive(full ? 1 : 0)
: m_vertexBuffer(NULL)
, m_vertexData(NULL)
, m_vertexDataLength(0)
, m_commands()
, m_allocated(0x1000)
, isActive(full ? 1 : 0)
{
m_vertexData = std::malloc(m_allocated);
m_vertexData = malloc(m_allocated);
EnterCriticalSection(&Renderer::totalAllocCS);
Renderer::totalAlloc += static_cast<int>(m_allocated);
LeaveCriticalSection(&Renderer::totalAllocCS);
@@ -20,11 +22,9 @@ Renderer::CommandBuffer::CommandBuffer(bool full)
Renderer::CommandBuffer::~CommandBuffer()
{
if (m_vertexBuffer)
{
m_vertexBuffer->Release();
}
std::free(m_vertexData);
free(m_vertexData);
EnterCriticalSection(&Renderer::totalAllocCS);
Renderer::totalAlloc -= static_cast<int>(m_allocated);
@@ -38,7 +38,7 @@ void Renderer::CommandBuffer::EndRecording(ID3D11Device *device)
if (m_vertexDataLength != 0)
{
D3D11_BUFFER_DESC desc = {};
desc.ByteWidth = m_vertexDataLength;
desc.ByteWidth = static_cast<UINT>(m_vertexDataLength);
desc.Usage = D3D11_USAGE_IMMUTABLE;
desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
@@ -47,8 +47,8 @@ void Renderer::CommandBuffer::EndRecording(ID3D11Device *device)
device->CreateBuffer(&desc, &data, &m_vertexBuffer);
}
std::free(m_vertexData);
m_vertexData = nullptr;
free(m_vertexData);
m_vertexData = NULL;
}
std::uint64_t Renderer::CommandBuffer::GetAllocated()
@@ -65,7 +65,7 @@ void Renderer::CommandBuffer::AddMatrix(const float *matrix)
{
Command command = {};
command.m_command_type = COMMAND_ADD_MATRIX;
std::memcpy(command.add_matrix.m_matrix, matrix, sizeof(command.add_matrix.m_matrix));
memcpy(command.add_matrix.m_matrix, matrix, sizeof(command.add_matrix.m_matrix));
m_commands.push_back(command);
}
@@ -73,7 +73,7 @@ void Renderer::CommandBuffer::AddVertices(unsigned int stride, unsigned int coun
{
if (c.matrixDirty[MATRIX_MODE_MODELVIEW_CBUFF])
{
this->AddMatrix(InternalRenderManager.MatrixGet(MATRIX_MODE_MODELVIEW_CBUFF));
AddMatrix(InternalRenderManager.MatrixGet(MATRIX_MODE_MODELVIEW_CBUFF));
c.matrixDirty[MATRIX_MODE_MODELVIEW_CBUFF] = false;
}
@@ -92,7 +92,7 @@ void Renderer::CommandBuffer::AddVertices(unsigned int stride, unsigned int coun
Renderer::totalAlloc -= static_cast<int>(m_allocated);
LeaveCriticalSection(&Renderer::totalAllocCS);
m_allocated = ((m_vertexDataLength + (0x1000u - 1u)) & ~(0x1000u - 1u));
m_allocated = ((m_vertexDataLength + (0x1000 - 1)) & ~(0x1000 - 1));
m_vertexData = std::realloc(m_vertexData, m_allocated);
EnterCriticalSection(&Renderer::totalAllocCS);
@@ -101,7 +101,7 @@ void Renderer::CommandBuffer::AddVertices(unsigned int stride, unsigned int coun
}
const std::size_t byteCount = std::size_t(stride) * std::size_t(count);
std::memcpy(static_cast<std::uint8_t *>(m_vertexData) + vertexOffset, dataIn, byteCount);
memcpy(static_cast<std::uint8_t *>(m_vertexData) + vertexOffset, dataIn, byteCount);
m_commands.push_back(command);
}
@@ -244,9 +244,7 @@ void Renderer::CommandBuffer::SetFaceCull(bool enable)
void Renderer::CommandBuffer::Render(C4JRender::eVertexType vType, Renderer::Context &c, int primitiveType)
{
if (!m_vertexBuffer)
{
return;
}
int drawVertexType = vType;
int shaderVertexType = drawVertexType;
@@ -265,14 +263,14 @@ void Renderer::CommandBuffer::Render(C4JRender::eVertexType vType, Renderer::Con
command.add_matrix.m_matrix[15]};
D3D11_MAPPED_SUBRESOURCE mappedAux0 = {};
c.m_pDeviceContext->Map(c.m_compressedTranslationBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedAux0);
std::memcpy(mappedAux0.pData, row, sizeof(row));
memcpy(mappedAux0.pData, row, sizeof(row));
c.m_pDeviceContext->Unmap(c.m_compressedTranslationBuffer, 0);
}
else
{
D3D11_MAPPED_SUBRESOURCE mappedMatrix1 = {};
c.m_pDeviceContext->Map(c.m_localTransformMatrix, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedMatrix1);
std::memcpy(mappedMatrix1.pData, command.add_matrix.m_matrix, sizeof(command.add_matrix.m_matrix));
memcpy(mappedMatrix1.pData, command.add_matrix.m_matrix, sizeof(command.add_matrix.m_matrix));
c.m_pDeviceContext->Unmap(c.m_localTransformMatrix, 0);
matrixOverride = true;
}
@@ -300,7 +298,7 @@ void Renderer::CommandBuffer::Render(C4JRender::eVertexType vType, Renderer::Con
if (static_cast<DWORD>(drawVertexType) != InternalRenderManager.activeVertexType)
{
c.m_pDeviceContext->VSSetShader(InternalRenderManager.vertexShaderTable[shaderVertexType], nullptr, 0);
c.m_pDeviceContext->VSSetShader(InternalRenderManager.vertexShaderTable[shaderVertexType], NULL, 0);
c.m_pDeviceContext->IASetInputLayout(InternalRenderManager.inputLayoutTable[shaderVertexType]);
InternalRenderManager.activeVertexType = drawVertexType;
}
@@ -310,12 +308,12 @@ void Renderer::CommandBuffer::Render(C4JRender::eVertexType vType, Renderer::Con
bool drawIndexed = false;
if (primitiveType == C4JRender::PRIMITIVE_TYPE_QUAD_LIST)
{
drawCount = (drawCount * 6u) / 4u;
drawCount = (drawCount * 6) / 4;
drawIndexed = true;
}
else if (primitiveType == C4JRender::PRIMITIVE_TYPE_TRIANGLE_FAN)
{
drawCount = (drawCount - 2u) * 3u;
drawCount = (drawCount - 2) * 3;
drawIndexed = true;
}
@@ -325,13 +323,9 @@ void Renderer::CommandBuffer::Render(C4JRender::eVertexType vType, Renderer::Con
c.m_pDeviceContext->IASetVertexBuffers(0, 1, &buffer, &stride, &offset);
if (drawIndexed)
{
c.m_pDeviceContext->DrawIndexed(drawCount, 0, 0);
}
else
{
c.m_pDeviceContext->Draw(drawCount, 0);
}
break;
}
case COMMAND_BIND_TEXTURE:
@@ -346,7 +340,7 @@ void Renderer::CommandBuffer::Render(C4JRender::eVertexType vType, Renderer::Con
{
D3D11_MAPPED_SUBRESOURCE mappedColour = {};
c.m_pDeviceContext->Map(c.m_tintColorBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedColour);
std::memcpy(mappedColour.pData, command.set_color.m_color, sizeof(command.set_color.m_color));
memcpy(mappedColour.pData, command.set_color.m_color, sizeof(command.set_color.m_color));
c.m_pDeviceContext->Unmap(c.m_tintColorBuffer, 0);
break;
}
@@ -358,21 +352,21 @@ void Renderer::CommandBuffer::Render(C4JRender::eVertexType vType, Renderer::Con
}
case COMMAND_SET_DEPTH_MASK:
{
c.depthWriteEnabled = command.set_depth_mask.m_enable ? 1 : 0;
c.depthWriteEnabled = command.set_depth_mask.m_enable ? TRUE : FALSE;
c.depthStencilDesc.DepthWriteMask = command.set_depth_mask.m_enable ? D3D11_DEPTH_WRITE_MASK_ALL : D3D11_DEPTH_WRITE_MASK_ZERO;
c.m_pDeviceContext->OMSetDepthStencilState(InternalRenderManager.GetManagedDepthStencilState(), 0);
break;
}
case COMMAND_SET_DEPTH_TEST:
{
c.depthTestEnabled = command.set_depth_test.m_enable ? 1 : 0;
c.depthTestEnabled = command.set_depth_test.m_enable ? TRUE : FALSE;
c.depthStencilDesc.DepthEnable = command.set_depth_test.m_enable ? TRUE : FALSE;
c.m_pDeviceContext->OMSetDepthStencilState(InternalRenderManager.GetManagedDepthStencilState(), 0);
break;
}
case COMMAND_SET_LIGHTING_ENABLE:
{
c.lightingEnabled = command.set_lighting_enable.m_enable ? 1 : 0;
c.lightingEnabled = command.set_lighting_enable.m_enable ? TRUE : FALSE;
break;
}
case COMMAND_SET_LIGHT_ENABLE:
@@ -380,8 +374,8 @@ void Renderer::CommandBuffer::Render(C4JRender::eVertexType vType, Renderer::Con
const int light = command.set_light_enable.m_light_index;
if (light >= 0 && light < 2)
{
c.lightEnabled[light] = command.set_light_enable.m_enable ? 1 : 0;
c.lightingDirty = 1;
c.lightEnabled[light] = command.set_light_enable.m_enable ? TRUE : FALSE;
c.lightingDirty = TRUE;
}
break;
}
@@ -394,7 +388,7 @@ void Renderer::CommandBuffer::Render(C4JRender::eVertexType vType, Renderer::Con
c.lightDirection[light].y = command.set_light_direction.m_direction[1];
c.lightDirection[light].z = command.set_light_direction.m_direction[2];
c.lightDirection[light].w = command.set_light_direction.m_direction[3];
c.lightingDirty = 1;
c.lightingDirty = TRUE;
}
break;
}
@@ -407,7 +401,7 @@ void Renderer::CommandBuffer::Render(C4JRender::eVertexType vType, Renderer::Con
c.lightColour[light].y = command.set_light_colour.m_color[1];
c.lightColour[light].z = command.set_light_colour.m_color[2];
c.lightColour[light].w = 1.0f;
c.lightingDirty = 1;
c.lightingDirty = TRUE;
}
break;
}
@@ -417,7 +411,7 @@ void Renderer::CommandBuffer::Render(C4JRender::eVertexType vType, Renderer::Con
c.lightAmbientColour.y = command.set_light_ambient_colour.m_color[1];
c.lightAmbientColour.z = command.set_light_ambient_colour.m_color[2];
c.lightAmbientColour.w = 1.0f;
c.lightingDirty = 1;
c.lightingDirty = TRUE;
break;
}
case COMMAND_SET_BLEND_ENABLE:
@@ -446,7 +440,7 @@ void Renderer::CommandBuffer::Render(C4JRender::eVertexType vType, Renderer::Con
{
c.rasterizerDesc.CullMode = command.set_face_cull.m_enable ? D3D11_CULL_BACK : D3D11_CULL_NONE;
c.m_pDeviceContext->RSSetState(InternalRenderManager.GetManagedRasterizerState());
c.faceCullEnabled = command.set_face_cull.m_enable ? 1 : 0;
c.faceCullEnabled = command.set_face_cull.m_enable ? TRUE : FALSE;
break;
}
default:
@@ -459,7 +453,7 @@ void Renderer::CommandBuffer::Render(C4JRender::eVertexType vType, Renderer::Con
const DirectX::XMMATRIX identity = DirectX::XMMatrixIdentity();
D3D11_MAPPED_SUBRESOURCE mappedIdentity = {};
c.m_pDeviceContext->Map(c.m_localTransformMatrix, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedIdentity);
std::memcpy(mappedIdentity.pData, &identity, sizeof(identity));
memcpy(mappedIdentity.pData, &identity, sizeof(identity));
c.m_pDeviceContext->Unmap(c.m_localTransformMatrix, 0);
}
}
@@ -472,7 +466,7 @@ bool Renderer::CBuffCall(int index, bool full)
const int commandIndex = m_commandHandleToIndex[index];
if (commandIndex >= 0)
{
Renderer::Context &c = this->getContext();
Renderer::Context &c = getContext();
const std::uint8_t vertexType = m_commandVertexTypes[commandIndex];
const std::uint8_t primitiveType = m_commandPrimitiveTypes[commandIndex];
@@ -482,7 +476,7 @@ bool Renderer::CBuffCall(int index, bool full)
{
D3D11_MAPPED_SUBRESOURCE mappedMatrix0 = {};
c.m_pDeviceContext->Map(c.m_modelViewMatrix, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedMatrix0);
std::memcpy(mappedMatrix0.pData, this->MatrixGet(MATRIX_MODE_MODELVIEW), sizeof(DirectX::XMMATRIX));
memcpy(mappedMatrix0.pData, MatrixGet(MATRIX_MODE_MODELVIEW), sizeof(DirectX::XMMATRIX));
c.m_pDeviceContext->Unmap(c.m_modelViewMatrix, 0);
c.matrixDirty[MATRIX_MODE_MODELVIEW] = false;
}
@@ -491,7 +485,7 @@ bool Renderer::CBuffCall(int index, bool full)
{
D3D11_MAPPED_SUBRESOURCE mappedMatrix2 = {};
c.m_pDeviceContext->Map(c.m_projectionMatrix, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedMatrix2);
std::memcpy(mappedMatrix2.pData, this->MatrixGet(MATRIX_MODE_MODELVIEW_PROJECTION), sizeof(DirectX::XMMATRIX));
memcpy(mappedMatrix2.pData, MatrixGet(MATRIX_MODE_MODELVIEW_PROJECTION), sizeof(DirectX::XMMATRIX));
c.m_pDeviceContext->Unmap(c.m_projectionMatrix, 0);
c.matrixDirty[MATRIX_MODE_MODELVIEW_PROJECTION] = false;
}
@@ -500,19 +494,19 @@ bool Renderer::CBuffCall(int index, bool full)
{
D3D11_MAPPED_SUBRESOURCE mappedMatrix3 = {};
c.m_pDeviceContext->Map(c.m_textureMatrix, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedMatrix3);
std::memcpy(mappedMatrix3.pData, this->MatrixGet(MATRIX_MODE_MODELVIEW_TEXTURE), sizeof(DirectX::XMMATRIX));
memcpy(mappedMatrix3.pData, MatrixGet(MATRIX_MODE_MODELVIEW_TEXTURE), sizeof(DirectX::XMMATRIX));
c.m_pDeviceContext->Unmap(c.m_textureMatrix, 0);
c.matrixDirty[MATRIX_MODE_MODELVIEW_TEXTURE] = false;
}
this->UpdateFogState();
this->UpdateLightingState();
this->UpdateViewportState();
this->UpdateTexGenState();
UpdateFogState();
UpdateLightingState();
UpdateViewportState();
UpdateTexGenState();
if (vertexType != activeVertexType)
{
c.m_pDeviceContext->VSSetShader(vertexShaderTable[vertexType], nullptr, 0);
c.m_pDeviceContext->VSSetShader(vertexShaderTable[vertexType], NULL, 0);
c.m_pDeviceContext->IASetInputLayout(inputLayoutTable[vertexType]);
activeVertexType = vertexType;
}
@@ -523,28 +517,24 @@ bool Renderer::CBuffCall(int index, bool full)
const float forcedLod[4] = {static_cast<float>(static_cast<int>(c.forcedLOD)), 0.0f, 0.0f, 0.0f};
D3D11_MAPPED_SUBRESOURCE mappedAux4 = {};
c.m_pDeviceContext->Map(c.m_forcedLODBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedAux4);
std::memcpy(mappedAux4.pData, forcedLod, sizeof(forcedLod));
memcpy(mappedAux4.pData, forcedLod, sizeof(forcedLod));
c.m_pDeviceContext->Unmap(c.m_forcedLODBuffer, 0);
pixelType = C4JRender::PIXEL_SHADER_TYPE_FORCELOD;
}
if (static_cast<DWORD>(pixelType) != activePixelType)
{
c.m_pDeviceContext->PSSetShader(pixelShaderTable[pixelType], nullptr, 0);
c.m_pDeviceContext->PSSetShader(pixelShaderTable[pixelType], NULL, 0);
activePixelType = pixelType;
}
c.m_pDeviceContext->IASetPrimitiveTopology(m_Topologies[primitiveType]);
c.m_pDeviceContext->IASetPrimitiveTopology(g_topologies[primitiveType]);
ID3D11Buffer *indexBuffer = nullptr;
ID3D11Buffer *indexBuffer = NULL;
if (primitiveType == C4JRender::PRIMITIVE_TYPE_QUAD_LIST)
{
indexBuffer = quadIndexBuffer;
}
else if (primitiveType == C4JRender::PRIMITIVE_TYPE_TRIANGLE_FAN)
{
indexBuffer = fanIndexBuffer;
}
c.m_pDeviceContext->IASetIndexBuffer(indexBuffer, DXGI_FORMAT_R16_UINT, 0);
}
@@ -553,7 +543,7 @@ bool Renderer::CBuffCall(int index, bool full)
if (full)
{
this->MultWithStack(m_commandMatrices[commandIndex]);
MultWithStack(m_commandMatrices[commandIndex]);
c.matrixStacks[MATRIX_MODE_MODELVIEW_CBUFF][0] = DirectX::XMMatrixIdentity();
c.matrixDirty[MATRIX_MODE_MODELVIEW_CBUFF] = true;
}
@@ -573,7 +563,7 @@ void Renderer::CBuffClear(int index)
const int internalIndex = externalToInternal[index];
if (internalIndex >= 0)
{
this->DeleteInternalBuffer(internalIndex);
DeleteInternalBuffer(internalIndex);
externalToInternal[index] = static_cast<std::int16_t>(-2);
}
@@ -600,9 +590,7 @@ int Renderer::CBuffCreate(int count)
}
if (cursor >= end)
{
break;
}
++first;
++probe;
@@ -618,14 +606,10 @@ int Renderer::CBuffCreate(int count)
{
const int allocationEnd = first + count;
for (int i = first; i < allocationEnd; ++i)
{
m_commandHandleToIndex[i] = static_cast<std::int16_t>(-2);
}
if (reservedRendererByte1)
{
reservedRendererDword1 = allocationEnd;
}
}
}
else
@@ -639,28 +623,22 @@ int Renderer::CBuffCreate(int count)
void Renderer::CBuffDeferredModeEnd()
{
Renderer::Context &c = this->getContext();
Renderer::Context &c = getContext();
if (!c.deferredModeEnabled)
{
return;
}
EnterCriticalSection(&rtl_critical_section100);
c.deferredModeEnabled = 0;
c.deferredModeEnabled = false;
for (std::vector<Renderer::DeferredCBuff>::const_iterator it = c.deferredBuffers.begin(); it != c.deferredBuffers.end(); ++it)
{
const Renderer::DeferredCBuff &deferred = *it;
const int existingIndex = m_commandHandleToIndex[deferred.m_vertex_index];
if (existingIndex >= 0)
{
this->DeleteInternalBuffer(existingIndex);
}
DeleteInternalBuffer(existingIndex);
if (static_cast<int>(reservedRendererDword2 + reservedRendererDword3 + 10u) > MAX_COMMAND_BUFFERS)
{
if (static_cast<int>(reservedRendererDword2 + reservedRendererDword3 + 10) > MAX_COMMAND_BUFFERS)
DebugBreak();
}
const int internalSlot = reservedRendererDword2;
++reservedRendererDword2;
@@ -679,7 +657,7 @@ void Renderer::CBuffDeferredModeEnd()
void Renderer::CBuffDeferredModeStart()
{
this->getContext().deferredModeEnabled = 1;
getContext().deferredModeEnabled = true;
}
void Renderer::CBuffDelete(int first, int count)
@@ -691,9 +669,7 @@ void Renderer::CBuffDelete(int first, int count)
{
const int internalIndex = m_commandHandleToIndex[i];
if (internalIndex >= 0)
{
this->DeleteInternalBuffer(internalIndex);
}
DeleteInternalBuffer(internalIndex);
m_commandHandleToIndex[i] = static_cast<std::int16_t>(-1);
}
@@ -703,7 +679,7 @@ void Renderer::CBuffDelete(int first, int count)
void Renderer::CBuffEnd()
{
Renderer::Context &c = this->getContext();
Renderer::Context &c = getContext();
assert(c.stackType == MATRIX_MODE_MODELVIEW_CBUFF);
assert(c.stackPos[MATRIX_MODE_MODELVIEW_CBUFF] == 0);
@@ -724,14 +700,10 @@ void Renderer::CBuffEnd()
{
const int existingIndex = m_commandHandleToIndex[c.recordingBufferIndex];
if (existingIndex >= 0)
{
this->DeleteInternalBuffer(existingIndex);
}
DeleteInternalBuffer(existingIndex);
if (static_cast<int>(reservedRendererDword2 + reservedRendererDword3 + 10u) > MAX_COMMAND_BUFFERS)
{
if (static_cast<int>(reservedRendererDword2 + reservedRendererDword3 + 10) > MAX_COMMAND_BUFFERS)
DebugBreak();
}
const int internalSlot = reservedRendererDword2;
++reservedRendererDword2;
@@ -746,7 +718,7 @@ void Renderer::CBuffEnd()
c.stackType = MATRIX_MODE_MODELVIEW;
c.commandBuffer->EndRecording(m_pDevice);
c.commandBuffer = nullptr;
c.commandBuffer = NULL;
LeaveCriticalSection(&rtl_critical_section100);
}
@@ -759,24 +731,20 @@ void Renderer::CBuffLockStaticCreations()
int Renderer::CBuffSize(int index)
{
if (index == -1)
{
return totalAlloc < 0 ? 0 : totalAlloc;
}
unsigned int size = 0;
EnterCriticalSection(&rtl_critical_section100);
const int commandIndex = m_commandHandleToIndex[index];
if (commandIndex >= 0)
{
size = m_commandBuffers[commandIndex]->GetAllocated();
}
size = static_cast<unsigned int>(m_commandBuffers[commandIndex]->GetAllocated());
LeaveCriticalSection(&rtl_critical_section100);
return size;
}
void Renderer::CBuffStart(int index, bool full)
{
Renderer::Context &c = this->getContext();
Renderer::Context &c = getContext();
c.commandBuffer = new (std::nothrow) Renderer::CommandBuffer(full);
c.recordingBufferIndex = index;
@@ -797,10 +765,8 @@ void Renderer::CBuffTick()
{
Renderer::CommandBuffer *buffer = m_commandBuffers[i];
if (buffer)
{
delete buffer;
}
m_commandBuffers[i] = nullptr;
m_commandBuffers[i] = NULL;
}
reservedRendererDword3 = 0;
@@ -832,4 +798,4 @@ void Renderer::DeleteInternalBuffer(int index)
}
LeaveCriticalSection(&rtl_critical_section100);
}
}