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 committed 2026-03-03 00:23:38 +10:00
1 parent 26f8477e4b
commit 3b406a063d
7 files changed
+556 -575

No files matched your search

+14 -17
View File
@@ -138,8 +138,6 @@ private:
ID3D11SamplerState *GetManagedSamplerState(); ID3D11SamplerState *GetManagedSamplerState();
void DeleteInternalBuffer(int index); void DeleteInternalBuffer(int index);
Renderer::Context &getContext(); Renderer::Context &getContext();
static D3D11_PRIMITIVE_TOPOLOGY *m_Topologies;
static DXGI_FORMAT textureFormats[C4JRender::MAX_TEXTURE_FORMATS];
public: public:
struct Texture struct Texture
{ {
@@ -333,12 +331,12 @@ public:
DWORD stackPos[MATRIX_MODE_MODELVIEW_MAX]; DWORD stackPos[MATRIX_MODE_MODELVIEW_MAX];
DWORD stackType; DWORD stackType;
DWORD textureIdx; DWORD textureIdx;
BYTE faceCullEnabled; bool faceCullEnabled;
BYTE depthTestEnabled; bool depthTestEnabled;
BYTE alphaTestEnabled; bool alphaTestEnabled;
float alphaReference; float alphaReference;
BYTE depthWriteEnabled; bool depthWriteEnabled;
BYTE fogEnabled; bool fogEnabled;
float fogNearDistance; float fogNearDistance;
float fogFarDistance; float fogFarDistance;
float fogDensity; float fogDensity;
@@ -346,9 +344,9 @@ public:
float fogColourBlue; float fogColourBlue;
float fogColourGreen; float fogColourGreen;
DWORD fogMode; DWORD fogMode;
BYTE lightingEnabled; bool lightingEnabled;
BYTE lightEnabled[2]; bool lightEnabled[2];
BYTE lightingDirty; bool lightingDirty;
DWORD forcedLOD; DWORD forcedLOD;
BYTE paddingAfterForceLOD[4]; BYTE paddingAfterForceLOD[4];
DirectX::XMFLOAT4 lightDirection[2]; DirectX::XMFLOAT4 lightDirection[2];
@@ -378,7 +376,7 @@ public:
DWORD recordingBufferIndex; DWORD recordingBufferIndex;
DWORD recordingVertexType; DWORD recordingVertexType;
DWORD recordingPrimitiveType; DWORD recordingPrimitiveType;
BYTE deferredModeEnabled; bool deferredModeEnabled;
std::vector<DeferredCBuff> deferredBuffers; std::vector<DeferredCBuff> deferredBuffers;
D3D11_BLEND_DESC blendDesc; D3D11_BLEND_DESC blendDesc;
D3D11_DEPTH_STENCIL_DESC depthStencilDesc; D3D11_DEPTH_STENCIL_DESC depthStencilDesc;
@@ -387,13 +385,12 @@ public:
}; };
static DWORD tlsIdx; static DWORD tlsIdx;
static unsigned int s_auiWidths[MAX_MIP_LEVELS + 1];
static unsigned int s_auiHeights[MAX_MIP_LEVELS + 1];
static D3D11_INPUT_ELEMENT_DESC g_vertex_PTN_Elements_PF3_TF2_CB4_NB4_XW1[5];
static D3D11_INPUT_ELEMENT_DESC g_vertex_PTN_Elements_Compressed[2];
static D3D11_PRIMITIVE_TOPOLOGY g_topologies[C4JRender::PRIMITIVE_TYPE_COUNT];
static int totalAlloc;
static _RTL_CRITICAL_SECTION totalAllocCS; static _RTL_CRITICAL_SECTION totalAllocCS;
static DWORD s_auiWidths[];
static DWORD s_auiHeights[];
static DXGI_FORMAT textureFormats[];
static D3D_PRIMITIVE_TOPOLOGY g_topologies[];
static int totalAlloc;
float m_fClearColor[4]; float m_fClearColor[4];
ID3D11Device *m_pDevice; ID3D11Device *m_pDevice;
+61 -95
View File
@@ -1,17 +1,19 @@
#pragma once #include "stdafx.h"
#include "Renderer.h" #include "Renderer.h"
#include <algorithm>
#include <cstddef>
#include <cstdint> #include <cstdint>
#include <cstdlib>
#include <cstring> #include <cstring>
#include <new> #include <new>
Renderer::CommandBuffer::CommandBuffer(bool full) 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); EnterCriticalSection(&Renderer::totalAllocCS);
Renderer::totalAlloc += static_cast<int>(m_allocated); Renderer::totalAlloc += static_cast<int>(m_allocated);
LeaveCriticalSection(&Renderer::totalAllocCS); LeaveCriticalSection(&Renderer::totalAllocCS);
@@ -20,11 +22,9 @@ Renderer::CommandBuffer::CommandBuffer(bool full)
Renderer::CommandBuffer::~CommandBuffer() Renderer::CommandBuffer::~CommandBuffer()
{ {
if (m_vertexBuffer) if (m_vertexBuffer)
{
m_vertexBuffer->Release(); m_vertexBuffer->Release();
}
std::free(m_vertexData); free(m_vertexData);
EnterCriticalSection(&Renderer::totalAllocCS); EnterCriticalSection(&Renderer::totalAllocCS);
Renderer::totalAlloc -= static_cast<int>(m_allocated); Renderer::totalAlloc -= static_cast<int>(m_allocated);
@@ -38,7 +38,7 @@ void Renderer::CommandBuffer::EndRecording(ID3D11Device *device)
if (m_vertexDataLength != 0) if (m_vertexDataLength != 0)
{ {
D3D11_BUFFER_DESC desc = {}; D3D11_BUFFER_DESC desc = {};
desc.ByteWidth = m_vertexDataLength; desc.ByteWidth = static_cast<UINT>(m_vertexDataLength);
desc.Usage = D3D11_USAGE_IMMUTABLE; desc.Usage = D3D11_USAGE_IMMUTABLE;
desc.BindFlags = D3D11_BIND_VERTEX_BUFFER; desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
@@ -47,8 +47,8 @@ void Renderer::CommandBuffer::EndRecording(ID3D11Device *device)
device->CreateBuffer(&desc, &data, &m_vertexBuffer); device->CreateBuffer(&desc, &data, &m_vertexBuffer);
} }
std::free(m_vertexData); free(m_vertexData);
m_vertexData = nullptr; m_vertexData = NULL;
} }
std::uint64_t Renderer::CommandBuffer::GetAllocated() std::uint64_t Renderer::CommandBuffer::GetAllocated()
@@ -65,7 +65,7 @@ void Renderer::CommandBuffer::AddMatrix(const float *matrix)
{ {
Command command = {}; Command command = {};
command.m_command_type = COMMAND_ADD_MATRIX; 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); 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]) 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; 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); Renderer::totalAlloc -= static_cast<int>(m_allocated);
LeaveCriticalSection(&Renderer::totalAllocCS); 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); m_vertexData = std::realloc(m_vertexData, m_allocated);
EnterCriticalSection(&Renderer::totalAllocCS); 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); 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); 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) void Renderer::CommandBuffer::Render(C4JRender::eVertexType vType, Renderer::Context &c, int primitiveType)
{ {
if (!m_vertexBuffer) if (!m_vertexBuffer)
{
return; return;
}
int drawVertexType = vType; int drawVertexType = vType;
int shaderVertexType = drawVertexType; int shaderVertexType = drawVertexType;
@@ -265,14 +263,14 @@ void Renderer::CommandBuffer::Render(C4JRender::eVertexType vType, Renderer::Con
command.add_matrix.m_matrix[15]}; command.add_matrix.m_matrix[15]};
D3D11_MAPPED_SUBRESOURCE mappedAux0 = {}; D3D11_MAPPED_SUBRESOURCE mappedAux0 = {};
c.m_pDeviceContext->Map(c.m_compressedTranslationBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &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); c.m_pDeviceContext->Unmap(c.m_compressedTranslationBuffer, 0);
} }
else else
{ {
D3D11_MAPPED_SUBRESOURCE mappedMatrix1 = {}; D3D11_MAPPED_SUBRESOURCE mappedMatrix1 = {};
c.m_pDeviceContext->Map(c.m_localTransformMatrix, 0, D3D11_MAP_WRITE_DISCARD, 0, &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); c.m_pDeviceContext->Unmap(c.m_localTransformMatrix, 0);
matrixOverride = true; matrixOverride = true;
} }
@@ -300,7 +298,7 @@ void Renderer::CommandBuffer::Render(C4JRender::eVertexType vType, Renderer::Con
if (static_cast<DWORD>(drawVertexType) != InternalRenderManager.activeVertexType) 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]); c.m_pDeviceContext->IASetInputLayout(InternalRenderManager.inputLayoutTable[shaderVertexType]);
InternalRenderManager.activeVertexType = drawVertexType; InternalRenderManager.activeVertexType = drawVertexType;
} }
@@ -310,12 +308,12 @@ void Renderer::CommandBuffer::Render(C4JRender::eVertexType vType, Renderer::Con
bool drawIndexed = false; bool drawIndexed = false;
if (primitiveType == C4JRender::PRIMITIVE_TYPE_QUAD_LIST) if (primitiveType == C4JRender::PRIMITIVE_TYPE_QUAD_LIST)
{ {
drawCount = (drawCount * 6u) / 4u; drawCount = (drawCount * 6) / 4;
drawIndexed = true; drawIndexed = true;
} }
else if (primitiveType == C4JRender::PRIMITIVE_TYPE_TRIANGLE_FAN) else if (primitiveType == C4JRender::PRIMITIVE_TYPE_TRIANGLE_FAN)
{ {
drawCount = (drawCount - 2u) * 3u; drawCount = (drawCount - 2) * 3;
drawIndexed = true; drawIndexed = true;
} }
@@ -325,13 +323,9 @@ void Renderer::CommandBuffer::Render(C4JRender::eVertexType vType, Renderer::Con
c.m_pDeviceContext->IASetVertexBuffers(0, 1, &buffer, &stride, &offset); c.m_pDeviceContext->IASetVertexBuffers(0, 1, &buffer, &stride, &offset);
if (drawIndexed) if (drawIndexed)
{
c.m_pDeviceContext->DrawIndexed(drawCount, 0, 0); c.m_pDeviceContext->DrawIndexed(drawCount, 0, 0);
}
else else
{
c.m_pDeviceContext->Draw(drawCount, 0); c.m_pDeviceContext->Draw(drawCount, 0);
}
break; break;
} }
case COMMAND_BIND_TEXTURE: case COMMAND_BIND_TEXTURE:
@@ -346,7 +340,7 @@ void Renderer::CommandBuffer::Render(C4JRender::eVertexType vType, Renderer::Con
{ {
D3D11_MAPPED_SUBRESOURCE mappedColour = {}; D3D11_MAPPED_SUBRESOURCE mappedColour = {};
c.m_pDeviceContext->Map(c.m_tintColorBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &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); c.m_pDeviceContext->Unmap(c.m_tintColorBuffer, 0);
break; break;
} }
@@ -358,21 +352,21 @@ void Renderer::CommandBuffer::Render(C4JRender::eVertexType vType, Renderer::Con
} }
case COMMAND_SET_DEPTH_MASK: 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.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); c.m_pDeviceContext->OMSetDepthStencilState(InternalRenderManager.GetManagedDepthStencilState(), 0);
break; break;
} }
case COMMAND_SET_DEPTH_TEST: 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.depthStencilDesc.DepthEnable = command.set_depth_test.m_enable ? TRUE : FALSE;
c.m_pDeviceContext->OMSetDepthStencilState(InternalRenderManager.GetManagedDepthStencilState(), 0); c.m_pDeviceContext->OMSetDepthStencilState(InternalRenderManager.GetManagedDepthStencilState(), 0);
break; break;
} }
case COMMAND_SET_LIGHTING_ENABLE: 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; break;
} }
case COMMAND_SET_LIGHT_ENABLE: 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; const int light = command.set_light_enable.m_light_index;
if (light >= 0 && light < 2) if (light >= 0 && light < 2)
{ {
c.lightEnabled[light] = command.set_light_enable.m_enable ? 1 : 0; c.lightEnabled[light] = command.set_light_enable.m_enable ? TRUE : FALSE;
c.lightingDirty = 1; c.lightingDirty = TRUE;
} }
break; 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].y = command.set_light_direction.m_direction[1];
c.lightDirection[light].z = command.set_light_direction.m_direction[2]; c.lightDirection[light].z = command.set_light_direction.m_direction[2];
c.lightDirection[light].w = command.set_light_direction.m_direction[3]; c.lightDirection[light].w = command.set_light_direction.m_direction[3];
c.lightingDirty = 1; c.lightingDirty = TRUE;
} }
break; 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].y = command.set_light_colour.m_color[1];
c.lightColour[light].z = command.set_light_colour.m_color[2]; c.lightColour[light].z = command.set_light_colour.m_color[2];
c.lightColour[light].w = 1.0f; c.lightColour[light].w = 1.0f;
c.lightingDirty = 1; c.lightingDirty = TRUE;
} }
break; 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.y = command.set_light_ambient_colour.m_color[1];
c.lightAmbientColour.z = command.set_light_ambient_colour.m_color[2]; c.lightAmbientColour.z = command.set_light_ambient_colour.m_color[2];
c.lightAmbientColour.w = 1.0f; c.lightAmbientColour.w = 1.0f;
c.lightingDirty = 1; c.lightingDirty = TRUE;
break; break;
} }
case COMMAND_SET_BLEND_ENABLE: 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.rasterizerDesc.CullMode = command.set_face_cull.m_enable ? D3D11_CULL_BACK : D3D11_CULL_NONE;
c.m_pDeviceContext->RSSetState(InternalRenderManager.GetManagedRasterizerState()); 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; break;
} }
default: default:
@@ -459,7 +453,7 @@ void Renderer::CommandBuffer::Render(C4JRender::eVertexType vType, Renderer::Con
const DirectX::XMMATRIX identity = DirectX::XMMatrixIdentity(); const DirectX::XMMATRIX identity = DirectX::XMMatrixIdentity();
D3D11_MAPPED_SUBRESOURCE mappedIdentity = {}; D3D11_MAPPED_SUBRESOURCE mappedIdentity = {};
c.m_pDeviceContext->Map(c.m_localTransformMatrix, 0, D3D11_MAP_WRITE_DISCARD, 0, &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); 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]; const int commandIndex = m_commandHandleToIndex[index];
if (commandIndex >= 0) if (commandIndex >= 0)
{ {
Renderer::Context &c = this->getContext(); Renderer::Context &c = getContext();
const std::uint8_t vertexType = m_commandVertexTypes[commandIndex]; const std::uint8_t vertexType = m_commandVertexTypes[commandIndex];
const std::uint8_t primitiveType = m_commandPrimitiveTypes[commandIndex]; const std::uint8_t primitiveType = m_commandPrimitiveTypes[commandIndex];
@@ -482,7 +476,7 @@ bool Renderer::CBuffCall(int index, bool full)
{ {
D3D11_MAPPED_SUBRESOURCE mappedMatrix0 = {}; D3D11_MAPPED_SUBRESOURCE mappedMatrix0 = {};
c.m_pDeviceContext->Map(c.m_modelViewMatrix, 0, D3D11_MAP_WRITE_DISCARD, 0, &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.m_pDeviceContext->Unmap(c.m_modelViewMatrix, 0);
c.matrixDirty[MATRIX_MODE_MODELVIEW] = false; c.matrixDirty[MATRIX_MODE_MODELVIEW] = false;
} }
@@ -491,7 +485,7 @@ bool Renderer::CBuffCall(int index, bool full)
{ {
D3D11_MAPPED_SUBRESOURCE mappedMatrix2 = {}; D3D11_MAPPED_SUBRESOURCE mappedMatrix2 = {};
c.m_pDeviceContext->Map(c.m_projectionMatrix, 0, D3D11_MAP_WRITE_DISCARD, 0, &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.m_pDeviceContext->Unmap(c.m_projectionMatrix, 0);
c.matrixDirty[MATRIX_MODE_MODELVIEW_PROJECTION] = false; c.matrixDirty[MATRIX_MODE_MODELVIEW_PROJECTION] = false;
} }
@@ -500,19 +494,19 @@ bool Renderer::CBuffCall(int index, bool full)
{ {
D3D11_MAPPED_SUBRESOURCE mappedMatrix3 = {}; D3D11_MAPPED_SUBRESOURCE mappedMatrix3 = {};
c.m_pDeviceContext->Map(c.m_textureMatrix, 0, D3D11_MAP_WRITE_DISCARD, 0, &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.m_pDeviceContext->Unmap(c.m_textureMatrix, 0);
c.matrixDirty[MATRIX_MODE_MODELVIEW_TEXTURE] = false; c.matrixDirty[MATRIX_MODE_MODELVIEW_TEXTURE] = false;
} }
this->UpdateFogState(); UpdateFogState();
this->UpdateLightingState(); UpdateLightingState();
this->UpdateViewportState(); UpdateViewportState();
this->UpdateTexGenState(); UpdateTexGenState();
if (vertexType != activeVertexType) 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]); c.m_pDeviceContext->IASetInputLayout(inputLayoutTable[vertexType]);
activeVertexType = 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}; const float forcedLod[4] = {static_cast<float>(static_cast<int>(c.forcedLOD)), 0.0f, 0.0f, 0.0f};
D3D11_MAPPED_SUBRESOURCE mappedAux4 = {}; D3D11_MAPPED_SUBRESOURCE mappedAux4 = {};
c.m_pDeviceContext->Map(c.m_forcedLODBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &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); c.m_pDeviceContext->Unmap(c.m_forcedLODBuffer, 0);
pixelType = C4JRender::PIXEL_SHADER_TYPE_FORCELOD; pixelType = C4JRender::PIXEL_SHADER_TYPE_FORCELOD;
} }
if (static_cast<DWORD>(pixelType) != activePixelType) if (static_cast<DWORD>(pixelType) != activePixelType)
{ {
c.m_pDeviceContext->PSSetShader(pixelShaderTable[pixelType], nullptr, 0); c.m_pDeviceContext->PSSetShader(pixelShaderTable[pixelType], NULL, 0);
activePixelType = pixelType; 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) if (primitiveType == C4JRender::PRIMITIVE_TYPE_QUAD_LIST)
{
indexBuffer = quadIndexBuffer; indexBuffer = quadIndexBuffer;
}
else if (primitiveType == C4JRender::PRIMITIVE_TYPE_TRIANGLE_FAN) else if (primitiveType == C4JRender::PRIMITIVE_TYPE_TRIANGLE_FAN)
{
indexBuffer = fanIndexBuffer; indexBuffer = fanIndexBuffer;
}
c.m_pDeviceContext->IASetIndexBuffer(indexBuffer, DXGI_FORMAT_R16_UINT, 0); c.m_pDeviceContext->IASetIndexBuffer(indexBuffer, DXGI_FORMAT_R16_UINT, 0);
} }
@@ -553,7 +543,7 @@ bool Renderer::CBuffCall(int index, bool full)
if (full) if (full)
{ {
this->MultWithStack(m_commandMatrices[commandIndex]); MultWithStack(m_commandMatrices[commandIndex]);
c.matrixStacks[MATRIX_MODE_MODELVIEW_CBUFF][0] = DirectX::XMMatrixIdentity(); c.matrixStacks[MATRIX_MODE_MODELVIEW_CBUFF][0] = DirectX::XMMatrixIdentity();
c.matrixDirty[MATRIX_MODE_MODELVIEW_CBUFF] = true; c.matrixDirty[MATRIX_MODE_MODELVIEW_CBUFF] = true;
} }
@@ -573,7 +563,7 @@ void Renderer::CBuffClear(int index)
const int internalIndex = externalToInternal[index]; const int internalIndex = externalToInternal[index];
if (internalIndex >= 0) if (internalIndex >= 0)
{ {
this->DeleteInternalBuffer(internalIndex); DeleteInternalBuffer(internalIndex);
externalToInternal[index] = static_cast<std::int16_t>(-2); externalToInternal[index] = static_cast<std::int16_t>(-2);
} }
@@ -600,9 +590,7 @@ int Renderer::CBuffCreate(int count)
} }
if (cursor >= end) if (cursor >= end)
{
break; break;
}
++first; ++first;
++probe; ++probe;
@@ -618,14 +606,10 @@ int Renderer::CBuffCreate(int count)
{ {
const int allocationEnd = first + count; const int allocationEnd = first + count;
for (int i = first; i < allocationEnd; ++i) for (int i = first; i < allocationEnd; ++i)
{
m_commandHandleToIndex[i] = static_cast<std::int16_t>(-2); m_commandHandleToIndex[i] = static_cast<std::int16_t>(-2);
}
if (reservedRendererByte1) if (reservedRendererByte1)
{
reservedRendererDword1 = allocationEnd; reservedRendererDword1 = allocationEnd;
}
} }
} }
else else
@@ -639,28 +623,22 @@ int Renderer::CBuffCreate(int count)
void Renderer::CBuffDeferredModeEnd() void Renderer::CBuffDeferredModeEnd()
{ {
Renderer::Context &c = this->getContext(); Renderer::Context &c = getContext();
if (!c.deferredModeEnabled) if (!c.deferredModeEnabled)
{
return; return;
}
EnterCriticalSection(&rtl_critical_section100); 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) for (std::vector<Renderer::DeferredCBuff>::const_iterator it = c.deferredBuffers.begin(); it != c.deferredBuffers.end(); ++it)
{ {
const Renderer::DeferredCBuff &deferred = *it; const Renderer::DeferredCBuff &deferred = *it;
const int existingIndex = m_commandHandleToIndex[deferred.m_vertex_index]; const int existingIndex = m_commandHandleToIndex[deferred.m_vertex_index];
if (existingIndex >= 0) if (existingIndex >= 0)
{ DeleteInternalBuffer(existingIndex);
this->DeleteInternalBuffer(existingIndex);
}
if (static_cast<int>(reservedRendererDword2 + reservedRendererDword3 + 10u) > MAX_COMMAND_BUFFERS) if (static_cast<int>(reservedRendererDword2 + reservedRendererDword3 + 10) > MAX_COMMAND_BUFFERS)
{
DebugBreak(); DebugBreak();
}
const int internalSlot = reservedRendererDword2; const int internalSlot = reservedRendererDword2;
++reservedRendererDword2; ++reservedRendererDword2;
@@ -679,7 +657,7 @@ void Renderer::CBuffDeferredModeEnd()
void Renderer::CBuffDeferredModeStart() void Renderer::CBuffDeferredModeStart()
{ {
this->getContext().deferredModeEnabled = 1; getContext().deferredModeEnabled = true;
} }
void Renderer::CBuffDelete(int first, int count) void Renderer::CBuffDelete(int first, int count)
@@ -691,9 +669,7 @@ void Renderer::CBuffDelete(int first, int count)
{ {
const int internalIndex = m_commandHandleToIndex[i]; const int internalIndex = m_commandHandleToIndex[i];
if (internalIndex >= 0) if (internalIndex >= 0)
{ DeleteInternalBuffer(internalIndex);
this->DeleteInternalBuffer(internalIndex);
}
m_commandHandleToIndex[i] = static_cast<std::int16_t>(-1); m_commandHandleToIndex[i] = static_cast<std::int16_t>(-1);
} }
@@ -703,7 +679,7 @@ void Renderer::CBuffDelete(int first, int count)
void Renderer::CBuffEnd() void Renderer::CBuffEnd()
{ {
Renderer::Context &c = this->getContext(); Renderer::Context &c = getContext();
assert(c.stackType == MATRIX_MODE_MODELVIEW_CBUFF); assert(c.stackType == MATRIX_MODE_MODELVIEW_CBUFF);
assert(c.stackPos[MATRIX_MODE_MODELVIEW_CBUFF] == 0); assert(c.stackPos[MATRIX_MODE_MODELVIEW_CBUFF] == 0);
@@ -724,14 +700,10 @@ void Renderer::CBuffEnd()
{ {
const int existingIndex = m_commandHandleToIndex[c.recordingBufferIndex]; const int existingIndex = m_commandHandleToIndex[c.recordingBufferIndex];
if (existingIndex >= 0) if (existingIndex >= 0)
{ DeleteInternalBuffer(existingIndex);
this->DeleteInternalBuffer(existingIndex);
}
if (static_cast<int>(reservedRendererDword2 + reservedRendererDword3 + 10u) > MAX_COMMAND_BUFFERS) if (static_cast<int>(reservedRendererDword2 + reservedRendererDword3 + 10) > MAX_COMMAND_BUFFERS)
{
DebugBreak(); DebugBreak();
}
const int internalSlot = reservedRendererDword2; const int internalSlot = reservedRendererDword2;
++reservedRendererDword2; ++reservedRendererDword2;
@@ -746,7 +718,7 @@ void Renderer::CBuffEnd()
c.stackType = MATRIX_MODE_MODELVIEW; c.stackType = MATRIX_MODE_MODELVIEW;
c.commandBuffer->EndRecording(m_pDevice); c.commandBuffer->EndRecording(m_pDevice);
c.commandBuffer = nullptr; c.commandBuffer = NULL;
LeaveCriticalSection(&rtl_critical_section100); LeaveCriticalSection(&rtl_critical_section100);
} }
@@ -759,24 +731,20 @@ void Renderer::CBuffLockStaticCreations()
int Renderer::CBuffSize(int index) int Renderer::CBuffSize(int index)
{ {
if (index == -1) if (index == -1)
{
return totalAlloc < 0 ? 0 : totalAlloc; return totalAlloc < 0 ? 0 : totalAlloc;
}
unsigned int size = 0; unsigned int size = 0;
EnterCriticalSection(&rtl_critical_section100); EnterCriticalSection(&rtl_critical_section100);
const int commandIndex = m_commandHandleToIndex[index]; const int commandIndex = m_commandHandleToIndex[index];
if (commandIndex >= 0) if (commandIndex >= 0)
{ size = static_cast<unsigned int>(m_commandBuffers[commandIndex]->GetAllocated());
size = m_commandBuffers[commandIndex]->GetAllocated();
}
LeaveCriticalSection(&rtl_critical_section100); LeaveCriticalSection(&rtl_critical_section100);
return size; return size;
} }
void Renderer::CBuffStart(int index, bool full) 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.commandBuffer = new (std::nothrow) Renderer::CommandBuffer(full);
c.recordingBufferIndex = index; c.recordingBufferIndex = index;
@@ -797,10 +765,8 @@ void Renderer::CBuffTick()
{ {
Renderer::CommandBuffer *buffer = m_commandBuffers[i]; Renderer::CommandBuffer *buffer = m_commandBuffers[i];
if (buffer) if (buffer)
{
delete buffer; delete buffer;
} m_commandBuffers[i] = NULL;
m_commandBuffers[i] = nullptr;
} }
reservedRendererDword3 = 0; reservedRendererDword3 = 0;
@@ -832,4 +798,4 @@ void Renderer::DeleteInternalBuffer(int index)
} }
LeaveCriticalSection(&rtl_critical_section100); LeaveCriticalSection(&rtl_critical_section100);
} }
+329 -283
View File
@@ -1,39 +1,25 @@
#pragma once #include "stdafx.h"
#include "CompiledShaders.h"
#include "Renderer.h" #include "Renderer.h"
#include "CompiledShaders.h"
#include <cstdio>
#include <cstring>
#include <limits>
#include <new>
Renderer InternalRenderManager; Renderer InternalRenderManager;
DWORD Renderer::tlsIdx = TlsAlloc(); DWORD Renderer::tlsIdx = TlsAlloc();
_RTL_CRITICAL_SECTION Renderer::totalAllocCS;
DWORD Renderer::s_auiWidths[] = { 1920, 512, 256, 128, 64, 0 };
DWORD Renderer::s_auiHeights[] = { 1080, 512, 256, 128, 64 };
int Renderer::totalAlloc = 0; int Renderer::totalAlloc = 0;
_RTL_CRITICAL_SECTION Renderer::totalAllocCS = {0};
static const unsigned int kVertexBufferSize = 0x100000u; D3D11_INPUT_ELEMENT_DESC g_vertex_PTN_Elements_PF3_TF2_CB4_NB4_XW1[] = {
static const float kAspectRatio = 1.7777778f; {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
static const unsigned int kScreenGrabWidth = 1920u; {"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
static const unsigned int kScreenGrabHeight = 1080u; {"COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, 20, D3D11_INPUT_PER_VERTEX_DATA, 0},
static const unsigned int kThumbnailSize = 64u; {"NORMAL", 0, DXGI_FORMAT_R8G8B8A8_SNORM, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"TEXCOORD", 1, DXGI_FORMAT_R16G16_SINT, 0, 28, D3D11_INPUT_PER_VERTEX_DATA, 0},
static const unsigned int g_vertexStrides[C4JRender::VERTEX_TYPE_COUNT] = {32u, 16u, 32u, 32u};
unsigned int Renderer::s_auiWidths[MAX_MIP_LEVELS + 1] = {1920u, 512u, 256u, 128u, 64u, 0u};
unsigned int Renderer::s_auiHeights[MAX_MIP_LEVELS + 1] = {1080u, 512u, 256u, 128u, 64u, 0xFFFFFFFFu};
D3D11_INPUT_ELEMENT_DESC Renderer::g_vertex_PTN_Elements_PF3_TF2_CB4_NB4_XW1[5] = {
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, 20, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"NORMAL", 0, DXGI_FORMAT_R8G8B8A8_SNORM, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"TEXCOORD", 1, DXGI_FORMAT_R16G16_SINT, 0, 28, D3D11_INPUT_PER_VERTEX_DATA, 0},
}; };
D3D11_INPUT_ELEMENT_DESC Renderer::g_vertex_PTN_Elements_Compressed[2] = { D3D11_INPUT_ELEMENT_DESC g_vertex_PTN_Elements_Compressed[] = {
{"POSITION", 0, DXGI_FORMAT_R16G16B16A16_SINT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0}, {"POSITION", 0, DXGI_FORMAT_R16G16B16A16_SINT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"TEXCOORD", 0, DXGI_FORMAT_R16G16B16A16_SINT, 0, 8, D3D11_INPUT_PER_VERTEX_DATA, 0}, {"TEXCOORD", 0, DXGI_FORMAT_R16G16B16A16_SINT, 0, 8, D3D11_INPUT_PER_VERTEX_DATA, 0},
}; };
@@ -43,35 +29,80 @@ D3D11_PRIMITIVE_TOPOLOGY Renderer::g_topologies[C4JRender::PRIMITIVE_TYPE_COUNT]
D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST, D3D11_PRIMITIVE_TOPOLOGY_LINELIST, D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST, D3D11_PRIMITIVE_TOPOLOGY_LINELIST, D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP,
}; };
Renderer::Context::Context(ID3D11Device *device, ID3D11DeviceContext *deviceContext) static const unsigned int kVertexBufferSize = 0x100000;
: m_pDeviceContext(deviceContext), userAnnotation(nullptr), annotateDepth(0), stackType(0), textureIdx(0), faceCullEnabled(1), static const unsigned int kScreenGrabWidth = 1920;
depthTestEnabled(1), alphaTestEnabled(0), alphaReference(1.0f), depthWriteEnabled(1), fogEnabled(0), fogNearDistance(0.0f), static const unsigned int kScreenGrabHeight = 1080;
fogFarDistance(0.0f), fogDensity(0.0f), fogColourRed(0.0f), fogColourBlue(0.0f), fogColourGreen(0.0f), fogMode(0), lightingEnabled(0), static const unsigned int kThumbnailSize = 64;
lightingDirty(0), forcedLOD(0xFFFFFFFFu), m_modelViewMatrix(nullptr), m_localTransformMatrix(nullptr), m_projectionMatrix(nullptr),
m_textureMatrix(nullptr), m_vertexTexcoordBuffer(nullptr), m_fogParamsBuffer(nullptr), m_lightingStateBuffer(nullptr), m_texGenMatricesBuffer(nullptr), static const unsigned int g_vertexStrides[C4JRender::VERTEX_TYPE_COUNT] = { 32, 16, 32, 32 };
m_compressedTranslationBuffer(nullptr), m_thumbnailBoundsBuffer(nullptr), m_tintColorBuffer(nullptr), m_fogColourBuffer(nullptr), m_unkColorBuffer(nullptr), m_alphaTestBuffer(nullptr),
m_clearColorBuffer(nullptr), m_forcedLODBuffer(nullptr), dynamicVertexBase(0), dynamicVertexOffset(0), dynamicVertexBuffer(nullptr), Renderer::Context::Context(ID3D11Device* device, ID3D11DeviceContext* deviceContext)
commandBuffer(nullptr), recordingBufferIndex(0), recordingVertexType(0), recordingPrimitiveType(0), deferredModeEnabled(0), deferredBuffers() : m_pDeviceContext(deviceContext)
, userAnnotation(NULL)
, annotateDepth(0)
, stackType(0)
, textureIdx(0)
, faceCullEnabled(true)
, depthTestEnabled(true)
, depthWriteEnabled(true)
, alphaTestEnabled(false)
, alphaReference(1.0f)
, fogEnabled(false)
, fogNearDistance(0.0f)
, fogFarDistance(0.0f)
, fogDensity(0.0f)
, fogColourRed(0.0f)
, fogColourGreen(0.0f)
, fogColourBlue(0.0f)
, fogMode(0)
, lightingEnabled(false)
, lightingDirty(false)
, forcedLOD(-1)
, m_modelViewMatrix(NULL)
, m_localTransformMatrix(NULL)
, m_projectionMatrix(NULL)
, m_textureMatrix(NULL)
, m_vertexTexcoordBuffer(NULL)
, m_fogParamsBuffer(NULL)
, m_lightingStateBuffer(NULL)
, m_texGenMatricesBuffer(NULL)
, m_compressedTranslationBuffer(NULL)
, m_thumbnailBoundsBuffer(NULL)
, m_tintColorBuffer(NULL)
, m_fogColourBuffer(NULL)
, m_unkColorBuffer(NULL)
, m_alphaTestBuffer(NULL)
, m_clearColorBuffer(NULL)
, m_forcedLODBuffer(NULL)
, dynamicVertexBase(0)
, dynamicVertexOffset(0)
, dynamicVertexBuffer(NULL)
, commandBuffer(NULL)
, recordingBufferIndex(0)
, recordingVertexType(0)
, recordingPrimitiveType(0)
, deferredModeEnabled(false)
, deferredBuffers()
{ {
deviceContext->QueryInterface(IID_PPV_ARGS(&userAnnotation)); deviceContext->QueryInterface(IID_PPV_ARGS(&userAnnotation));
std::memset(matrixStacks, 0, sizeof(matrixStacks)); memset(matrixStacks, 0, sizeof(matrixStacks));
std::memset(matrixDirty, 0, sizeof(matrixDirty)); memset(matrixDirty, 0, sizeof(matrixDirty));
std::memset(stackPos, 0, sizeof(stackPos)); memset(stackPos, 0, sizeof(stackPos));
std::memset(lightEnabled, 0, sizeof(lightEnabled)); memset(lightEnabled, 0, sizeof(lightEnabled));
std::memset(lightDirection, 0, sizeof(lightDirection)); memset(lightDirection, 0, sizeof(lightDirection));
std::memset(lightColour, 0, sizeof(lightColour)); memset(lightColour, 0, sizeof(lightColour));
std::memset(&lightAmbientColour, 0, sizeof(lightAmbientColour)); memset(&lightAmbientColour, 0, sizeof(lightAmbientColour));
std::memset(texGenMatrices, 0, sizeof(texGenMatrices)); memset(texGenMatrices, 0, sizeof(texGenMatrices));
std::memset(&blendDesc, 0, sizeof(blendDesc)); memset(&blendDesc, 0, sizeof(blendDesc));
std::memset(&depthStencilDesc, 0, sizeof(depthStencilDesc)); memset(&depthStencilDesc, 0, sizeof(depthStencilDesc));
std::memset(&rasterizerDesc, 0, sizeof(rasterizerDesc)); memset(&rasterizerDesc, 0, sizeof(rasterizerDesc));
blendFactor[0] = 0.0f; blendFactor[0] = 0.0f;
blendFactor[1] = 0.0f; blendFactor[1] = 0.0f;
blendFactor[2] = 0.0f; blendFactor[2] = 0.0f;
blendFactor[3] = 0.0f; blendFactor[3] = 0.0f;
const DirectX::XMMATRIX identity = DirectX::XMMatrixIdentity(); const DirectX::XMMATRIX identity = DirectX::XMMatrixIdentity();
for (unsigned int i = 0; i < MATRIX_MODE_MODELVIEW_MAX; ++i) for (UINT i = 0; i < MATRIX_MODE_MODELVIEW_MAX; ++i)
{ {
matrixStacks[i][0] = identity; matrixStacks[i][0] = identity;
stackPos[i] = 0; stackPos[i] = 0;
@@ -92,8 +123,8 @@ Renderer::Context::Context(ID3D11Device *device, ID3D11DeviceContext *deviceCont
depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL; depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS; depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS;
depthStencilDesc.StencilEnable = FALSE; depthStencilDesc.StencilEnable = FALSE;
depthStencilDesc.StencilReadMask = 0xFFu; depthStencilDesc.StencilReadMask = 0xFF;
depthStencilDesc.StencilWriteMask = 0xFFu; depthStencilDesc.StencilWriteMask = 0xFF;
depthStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; depthStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP; depthStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; depthStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
@@ -114,10 +145,10 @@ Renderer::Context::Context(ID3D11Device *device, ID3D11DeviceContext *deviceCont
rasterizerDesc.MultisampleEnable = TRUE; rasterizerDesc.MultisampleEnable = TRUE;
rasterizerDesc.AntialiasedLineEnable = FALSE; rasterizerDesc.AntialiasedLineEnable = FALSE;
std::memset(lightDirection, 0, sizeof(lightDirection)); memset(lightDirection, 0, sizeof(lightDirection));
std::memset(lightColour, 0, sizeof(lightColour)); memset(lightColour, 0, sizeof(lightColour));
std::memset(&lightAmbientColour, 0, sizeof(lightAmbientColour)); memset(&lightAmbientColour, 0, sizeof(lightAmbientColour));
std::memset(texGenMatrices, 0, sizeof(texGenMatrices)); memset(texGenMatrices, 0, sizeof(texGenMatrices));
const float zero4[4] = {0.0f, 0.0f, 0.0f, 0.0f}; const float zero4[4] = {0.0f, 0.0f, 0.0f, 0.0f};
const float one4[4] = {1.0f, 1.0f, 1.0f, 1.0f}; const float one4[4] = {1.0f, 1.0f, 1.0f, 1.0f};
@@ -184,7 +215,7 @@ Renderer::Context::Context(ID3D11Device *device, ID3D11DeviceContext *deviceCont
vbDesc.Usage = D3D11_USAGE_DYNAMIC; vbDesc.Usage = D3D11_USAGE_DYNAMIC;
vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; vbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
device->CreateBuffer(&vbDesc, nullptr, &dynamicVertexBuffer); device->CreateBuffer(&vbDesc, NULL, &dynamicVertexBuffer);
} }
void Renderer::BeginConditionalRendering(int) {} void Renderer::BeginConditionalRendering(int) {}
@@ -195,11 +226,11 @@ void Renderer::CaptureScreen(ImageFileBuffer *, XSOCIAL_PREVIEWIMAGE *) {}
void Renderer::Clear(int flags, D3D11_RECT *) void Renderer::Clear(int flags, D3D11_RECT *)
{ {
Renderer::Context &c = this->getContext(); Renderer::Context &c = getContext();
ID3D11BlendState *blendState = nullptr; ID3D11BlendState *blendState = NULL;
ID3D11DepthStencilState *depthState = nullptr; ID3D11DepthStencilState *depthState = NULL;
ID3D11RasterizerState *rasterizerState = nullptr; ID3D11RasterizerState *rasterizerState = NULL;
D3D11_BLEND_DESC blendDesc = {}; D3D11_BLEND_DESC blendDesc = {};
blendDesc.AlphaToCoverageEnable = FALSE; blendDesc.AlphaToCoverageEnable = FALSE;
@@ -238,51 +269,59 @@ void Renderer::Clear(int flags, D3D11_RECT *)
rasterDesc.MultisampleEnable = TRUE; rasterDesc.MultisampleEnable = TRUE;
m_pDevice->CreateRasterizerState(&rasterDesc, &rasterizerState); m_pDevice->CreateRasterizerState(&rasterDesc, &rasterizerState);
c.m_pDeviceContext->VSSetShader(screenClearVertexShader, nullptr, 0); c.m_pDeviceContext->VSSetShader(screenClearVertexShader, NULL, 0);
c.m_pDeviceContext->IASetInputLayout(nullptr); c.m_pDeviceContext->IASetInputLayout(NULL);
c.m_pDeviceContext->PSSetShader(screenClearPixelShader, nullptr, 0); c.m_pDeviceContext->PSSetShader(screenClearPixelShader, NULL, 0);
c.m_pDeviceContext->OMSetRenderTargets(1, &renderTargetView, depthStencilView); c.m_pDeviceContext->OMSetRenderTargets(1, &renderTargetView, depthStencilView);
c.m_pDeviceContext->OMSetBlendState(blendState, nullptr, 0xFFFFFFFFu); c.m_pDeviceContext->OMSetBlendState(blendState, NULL, 0xFFFFFFFF);
c.m_pDeviceContext->OMSetDepthStencilState(depthState, 0); c.m_pDeviceContext->OMSetDepthStencilState(depthState, 0);
c.m_pDeviceContext->RSSetState(rasterizerState); c.m_pDeviceContext->RSSetState(rasterizerState);
c.m_pDeviceContext->PSSetShaderResources(0, 0, nullptr); c.m_pDeviceContext->PSSetShaderResources(0, 0, NULL);
c.m_pDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); c.m_pDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
c.m_pDeviceContext->Draw(4, 0); c.m_pDeviceContext->Draw(4, 0);
if (blendState) if (blendState)
{ {
blendState->Release(); blendState->Release();
blendState = nullptr; blendState = NULL;
} }
if (depthState) if (depthState)
{ {
depthState->Release(); depthState->Release();
depthState = nullptr; depthState = NULL;
} }
if (rasterizerState) if (rasterizerState)
{ {
rasterizerState->Release(); rasterizerState->Release();
rasterizerState = nullptr; rasterizerState = NULL;
} }
c.m_pDeviceContext->OMSetBlendState(this->GetManagedBlendState(), c.blendFactor, 0xFFFFFFFFu); c.m_pDeviceContext->OMSetBlendState(GetManagedBlendState(), c.blendFactor, 0xFFFFFFFF);
c.m_pDeviceContext->OMSetDepthStencilState(this->GetManagedDepthStencilState(), 0); c.m_pDeviceContext->OMSetDepthStencilState(GetManagedDepthStencilState(), 0);
c.m_pDeviceContext->RSSetState(this->GetManagedRasterizerState()); c.m_pDeviceContext->RSSetState(GetManagedRasterizerState());
c.m_pDeviceContext->OMSetRenderTargets(1, &renderTargetView, depthStencilView); c.m_pDeviceContext->OMSetRenderTargets(1, &renderTargetView, depthStencilView);
activeVertexType = 0xFFFFFFFFu;
activePixelType = 0xFFFFFFFFu; activeVertexType = -1;
activePixelType = -1;
} }
void Renderer::ConvertLinearToPng(ImageFileBuffer *pngOut, unsigned char *linearData, unsigned int width, unsigned int height) void Renderer::ConvertLinearToPng(ImageFileBuffer *pngOut, unsigned char *linearData, unsigned int width, unsigned int height)
{ {
const size_t dataSize = size_t(width) * size_t(height) * 4u; const size_t dataSize = static_cast<size_t>(width) * static_cast<size_t>(height) * 4;
const size_t outputCapacity = ((dataSize * 24u) / 20u) + 256u; const size_t outputCapacity = (dataSize * 24) / 20 + 256;
const int outputCapacityInt = int(outputCapacity);
const int widthInt = int(width); void *outputBuffer = malloc(outputCapacity);
const int heightInt = int(height);
void *outputBuffer = std::malloc(outputCapacity);
int outputLength = 0; int outputLength = 0;
this->SaveTextureDataToMemory(outputBuffer, outputCapacityInt, &outputLength, widthInt, heightInt, reinterpret_cast<int *>(linearData));
SaveTextureDataToMemory(
outputBuffer,
static_cast<int>(outputCapacity),
&outputLength,
static_cast<int>(width),
static_cast<int>(height),
reinterpret_cast<int *>(linearData)
);
pngOut->m_type = ImageFileBuffer::e_typePNG; pngOut->m_type = ImageFileBuffer::e_typePNG;
pngOut->m_pBuffer = outputBuffer; pngOut->m_pBuffer = outputBuffer;
pngOut->m_bufferSize = outputLength; pngOut->m_bufferSize = outputLength;
@@ -312,7 +351,6 @@ void Renderer::EndEvent()
if (c.m_pDeviceContext->GetType() != D3D11_DEVICE_CONTEXT_DEFERRED && c.userAnnotation) if (c.m_pDeviceContext->GetType() != D3D11_DEVICE_CONTEXT_DEFERRED && c.userAnnotation)
{ {
c.userAnnotation->EndEvent(); c.userAnnotation->EndEvent();
--c.annotateDepth; --c.annotateDepth;
assert(c.annotateDepth >= 0); assert(c.annotateDepth >= 0);
} }
@@ -321,7 +359,7 @@ void Renderer::EndEvent()
void Renderer::Initialise(ID3D11Device *pDevice, IDXGISwapChain *pSwapChain) void Renderer::Initialise(ID3D11Device *pDevice, IDXGISwapChain *pSwapChain)
{ {
m_pDevice = pDevice; m_pDevice = pDevice;
m_pDeviceContext = this->InitialiseContext(true); m_pDeviceContext = InitialiseContext(true);
m_pSwapChain = pSwapChain; m_pSwapChain = pSwapChain;
m_commandHandleToIndex = new int16_t[NUM_COMMAND_HANDLES]; m_commandHandleToIndex = new int16_t[NUM_COMMAND_HANDLES];
@@ -331,24 +369,24 @@ void Renderer::Initialise(ID3D11Device *pDevice, IDXGISwapChain *pSwapChain)
m_commandVertexTypes = new uint8_t[MAX_COMMAND_BUFFERS]; m_commandVertexTypes = new uint8_t[MAX_COMMAND_BUFFERS];
m_commandPrimitiveTypes = new uint8_t[MAX_COMMAND_BUFFERS]; m_commandPrimitiveTypes = new uint8_t[MAX_COMMAND_BUFFERS];
std::memset(m_commandHandleToIndex, 0xFF, NUM_COMMAND_HANDLES); memset(m_commandHandleToIndex, 0xFF, NUM_COMMAND_HANDLES * sizeof(int16_t));
std::memset(m_commandBuffers, 0, MAX_COMMAND_BUFFERS); memset(m_commandBuffers, 0, MAX_COMMAND_BUFFERS * sizeof(CommandBuffer*));
std::memset(m_commandIndexToHandle, 0, MAX_COMMAND_BUFFERS); memset(m_commandIndexToHandle, 0, MAX_COMMAND_BUFFERS * sizeof(int));
std::memset(m_commandVertexTypes, 0, MAX_COMMAND_BUFFERS); memset(m_commandVertexTypes, 0, MAX_COMMAND_BUFFERS * sizeof(uint8_t));
std::memset(m_commandPrimitiveTypes, 0, MAX_COMMAND_BUFFERS); memset(m_commandPrimitiveTypes, 0, MAX_COMMAND_BUFFERS * sizeof(uint8_t));
reservedRendererDword3 = 0; reservedRendererDword3 = 0;
m_bShouldScreenGrabNextFrame = false; m_bShouldScreenGrabNextFrame = false;
m_bSuspended = false; m_bSuspended = false;
this->SetupShaders(); SetupShaders();
const float clearColour[4] = {0.0f, 0.0f, 0.0f, 0.0f}; const float clearColour[4] = {0.0f, 0.0f, 0.0f, 0.0f};
this->SetClearColour(clearColour); SetClearColour(clearColour);
UINT backBufferSampleCount = 1; UINT backBufferSampleCount = 1;
UINT backBufferSampleQuality = 0; UINT backBufferSampleQuality = 0;
ID3D11Texture2D *backBuffer = nullptr;
ID3D11Texture2D *backBuffer = NULL;
pSwapChain->GetBuffer(0, IID_PPV_ARGS(&backBuffer)); pSwapChain->GetBuffer(0, IID_PPV_ARGS(&backBuffer));
if (backBuffer) if (backBuffer)
{ {
@@ -358,25 +396,24 @@ void Renderer::Initialise(ID3D11Device *pDevice, IDXGISwapChain *pSwapChain)
backBufferHeight = backDesc.Height; backBufferHeight = backDesc.Height;
backBufferSampleCount = backDesc.SampleDesc.Count; backBufferSampleCount = backDesc.SampleDesc.Count;
backBufferSampleQuality = backDesc.SampleDesc.Quality; backBufferSampleQuality = backDesc.SampleDesc.Quality;
m_pDevice->CreateRenderTargetView(backBuffer, nullptr, &renderTargetView);
m_pDevice->CreateRenderTargetView(backBuffer, NULL, &renderTargetView);
D3D11_TEXTURE2D_DESC srvDesc = backDesc; D3D11_TEXTURE2D_DESC srvDesc = backDesc;
srvDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE; srvDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
srvDesc.MiscFlags = 0; srvDesc.MiscFlags = 0;
ID3D11Texture2D *srvTexture = nullptr;
m_pDevice->CreateTexture2D(&srvDesc, nullptr, &srvTexture); ID3D11Texture2D *srvTexture = NULL;
m_pDevice->CreateShaderResourceView(srvTexture, nullptr, &renderTargetShaderResourceView); m_pDevice->CreateTexture2D(&srvDesc, NULL, &srvTexture);
m_pDevice->CreateShaderResourceView(srvTexture, NULL, &renderTargetShaderResourceView);
srvTexture->Release(); srvTexture->Release();
backBuffer->Release(); backBuffer->Release();
} }
ID3D11RenderTargetView *boundRTV = nullptr; ID3D11RenderTargetView *boundRTV = NULL;
m_pDeviceContext->OMGetRenderTargets(1, &boundRTV, &depthStencilView); m_pDeviceContext->OMGetRenderTargets(1, &boundRTV, &depthStencilView);
if (boundRTV) if (boundRTV) boundRTV->Release();
{
boundRTV->Release();
}
if (!depthStencilView && backBufferWidth != 0 && backBufferHeight != 0) if (!depthStencilView && backBufferWidth != 0 && backBufferHeight != 0)
{ {
@@ -391,15 +428,15 @@ void Renderer::Initialise(ID3D11Device *pDevice, IDXGISwapChain *pSwapChain)
depthDesc.Usage = D3D11_USAGE_DEFAULT; depthDesc.Usage = D3D11_USAGE_DEFAULT;
depthDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL; depthDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
ID3D11Texture2D *depthTexture = nullptr; ID3D11Texture2D *depthTexture = NULL;
if (SUCCEEDED(m_pDevice->CreateTexture2D(&depthDesc, nullptr, &depthTexture))) if (SUCCEEDED(m_pDevice->CreateTexture2D(&depthDesc, NULL, &depthTexture)))
{ {
m_pDevice->CreateDepthStencilView(depthTexture, nullptr, &depthStencilView); m_pDevice->CreateDepthStencilView(depthTexture, NULL, &depthStencilView);
depthTexture->Release(); depthTexture->Release();
} }
} }
for (unsigned int i = 0; i < MAX_MIP_LEVELS - 1; ++i) for (UINT i = 0; i < MAX_MIP_LEVELS - 1; ++i)
{ {
D3D11_TEXTURE2D_DESC desc = {}; D3D11_TEXTURE2D_DESC desc = {};
desc.Width = s_auiWidths[i + 1]; desc.Width = s_auiWidths[i + 1];
@@ -411,98 +448,97 @@ void Renderer::Initialise(ID3D11Device *pDevice, IDXGISwapChain *pSwapChain)
desc.Usage = D3D11_USAGE_DEFAULT; desc.Usage = D3D11_USAGE_DEFAULT;
desc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE; desc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
HRESULT hr = 0; HRESULT hr = m_pDevice->CreateTexture2D(&desc, NULL, &renderTargetTextures[i]);
hr = m_pDevice->CreateTexture2D(&desc, nullptr, &renderTargetTextures[i]);
assert(hr == S_OK); assert(hr == S_OK);
hr = m_pDevice->CreateRenderTargetView(renderTargetTextures[i], nullptr, &renderTargetViews[i]); hr = m_pDevice->CreateRenderTargetView(renderTargetTextures[i], NULL, &renderTargetViews[i]);
assert(hr == S_OK); assert(hr == S_OK);
hr = m_pDevice->CreateShaderResourceView(renderTargetTextures[i], nullptr, &renderTargetShaderResourceViews[i]); hr = m_pDevice->CreateShaderResourceView(renderTargetTextures[i], NULL, &renderTargetShaderResourceViews[i]);
assert(hr == S_OK); assert(hr == S_OK);
} }
std::memset(m_textures, 0, sizeof(m_textures)); memset(m_textures, 0, sizeof(m_textures));
defaultTextureIndex = this->TextureCreate(); defaultTextureIndex = TextureCreate();
this->TextureBind(defaultTextureIndex); TextureBind(defaultTextureIndex);
unsigned char *defaultTextureData = new unsigned char[0x400u]; unsigned char *defaultTextureData = new unsigned char[0x400];
std::memset(defaultTextureData, 0xFF, 0x400u); memset(defaultTextureData, 0xFF, 0x400);
this->TextureData(16, 16, defaultTextureData, 0, C4JRender::TEXTURE_FORMAT_RxGyBzAw); TextureData(16, 16, defaultTextureData, 0, C4JRender::TEXTURE_FORMAT_RxGyBzAw);
delete[] defaultTextureData; delete[] defaultTextureData;
presentCount = 0; presentCount = 0;
rendererFlag0 = 0; rendererFlag0 = 0;
reservedRendererWord0 = 10922; reservedRendererWord0 = 10922;
this->StateSetViewport(C4JRender::VIEWPORT_TYPE_FULLSCREEN); StateSetViewport(C4JRender::VIEWPORT_TYPE_FULLSCREEN);
this->StateSetVertexTextureUV(0.0f, 0.0f); StateSetVertexTextureUV(0.0f, 0.0f);
this->TextureBindVertex(-1); TextureBindVertex(-1);
InitializeCriticalSection(&rtl_critical_section100); InitializeCriticalSection(&rtl_critical_section100);
InitializeCriticalSection(&Renderer::totalAllocCS);
reservedRendererDword1 = 0; reservedRendererDword1 = 0;
activeVertexType = 0xFFFFFFFFu; activeVertexType = -1;
activePixelType = 0xFFFFFFFFu; activePixelType = -1;
reservedRendererByte1 = 1; reservedRendererByte1 = 1;
reservedRendererByte0 = 0; reservedRendererByte0 = 0;
unsigned short *quadIndices = new unsigned short[0x18000u]; unsigned short *quadIndices = new unsigned short[0x18000];
for (unsigned int i = 0; i < 0x4000u; ++i) for (UINT i = 0; i < 0x4000; ++i)
{ {
const unsigned short base = (unsigned short)(i * 4u); unsigned short base = static_cast<unsigned short>(i * 4);
const unsigned int offset = i * 6u; unsigned int offset = i * 6;
quadIndices[offset + 0] = base; quadIndices[offset + 0] = base;
quadIndices[offset + 1] = (unsigned short)(base + 1u); quadIndices[offset + 1] = base + 1;
quadIndices[offset + 2] = (unsigned short)(base + 3u); quadIndices[offset + 2] = base + 3;
quadIndices[offset + 3] = (unsigned short)(base + 1u); quadIndices[offset + 3] = base + 1;
quadIndices[offset + 4] = (unsigned short)(base + 2u); quadIndices[offset + 4] = base + 2;
quadIndices[offset + 5] = (unsigned short)(base + 3u); quadIndices[offset + 5] = base + 3;
} }
D3D11_BUFFER_DESC quadIndexDesc = {}; D3D11_BUFFER_DESC quadIndexDesc = {};
quadIndexDesc.ByteWidth = 0x30000u; quadIndexDesc.ByteWidth = 0x30000;
quadIndexDesc.Usage = D3D11_USAGE_IMMUTABLE; quadIndexDesc.Usage = D3D11_USAGE_IMMUTABLE;
quadIndexDesc.BindFlags = D3D11_BIND_INDEX_BUFFER; quadIndexDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
D3D11_SUBRESOURCE_DATA quadIndexData = {}; D3D11_SUBRESOURCE_DATA quadIndexData = {};
quadIndexData.pSysMem = quadIndices; quadIndexData.pSysMem = quadIndices;
HRESULT hr = m_pDevice->CreateBuffer(&quadIndexDesc, &quadIndexData, &quadIndexBuffer); HRESULT hr = m_pDevice->CreateBuffer(&quadIndexDesc, &quadIndexData, &quadIndexBuffer);
assert(hr >= 0); assert(hr >= 0);
delete[] quadIndices; delete[] quadIndices;
unsigned short *fanIndices = new unsigned short[0x2FFFAu]; unsigned short *fanIndices = new unsigned short[0x2FFFA];
for (unsigned int i = 0; i < 65534u; ++i) for (UINT i = 0; i < 65534; ++i)
{ {
const unsigned int offset = i * 3u; unsigned int offset = i * 3;
fanIndices[offset + 0] = 0u; fanIndices[offset + 0] = 0;
fanIndices[offset + 1] = (unsigned short)(i + 1u); fanIndices[offset + 1] = static_cast<unsigned short>(i + 1);
fanIndices[offset + 2] = (unsigned short)(i + 2u); fanIndices[offset + 2] = static_cast<unsigned short>(i + 2);
} }
D3D11_BUFFER_DESC fanIndexDesc = {}; D3D11_BUFFER_DESC fanIndexDesc = {};
fanIndexDesc.ByteWidth = 0x5FFF4u; fanIndexDesc.ByteWidth = 0x5FFF4;
fanIndexDesc.Usage = D3D11_USAGE_IMMUTABLE; fanIndexDesc.Usage = D3D11_USAGE_IMMUTABLE;
fanIndexDesc.BindFlags = D3D11_BIND_INDEX_BUFFER; fanIndexDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
D3D11_SUBRESOURCE_DATA fanIndexData = {}; D3D11_SUBRESOURCE_DATA fanIndexData = {};
fanIndexData.pSysMem = fanIndices; fanIndexData.pSysMem = fanIndices;
m_pDevice->CreateBuffer(&fanIndexDesc, &fanIndexData, &fanIndexBuffer); m_pDevice->CreateBuffer(&fanIndexDesc, &fanIndexData, &fanIndexBuffer);
delete[] fanIndices; delete[] fanIndices;
InitializeCriticalSection(&Renderer::totalAllocCS);
m_Topologies = g_topologies;
} }
ID3D11DeviceContext *Renderer::InitialiseContext(bool fromPresent) ID3D11DeviceContext *Renderer::InitialiseContext(bool fromPresent)
{ {
ID3D11DeviceContext *deviceContext = nullptr; ID3D11DeviceContext *deviceContext = NULL;
if (fromPresent) if (fromPresent)
{
m_pDevice->GetImmediateContext(&deviceContext); m_pDevice->GetImmediateContext(&deviceContext);
}
else else
{
m_pDevice->CreateDeferredContext(0, &deviceContext); m_pDevice->CreateDeferredContext(0, &deviceContext);
}
Renderer::Context *c = new (std::nothrow) Renderer::Context(m_pDevice, deviceContext); Renderer::Context *c = new (std::nothrow) Renderer::Context(m_pDevice, deviceContext);
TlsSetValue(Renderer::tlsIdx, c); TlsSetValue(Renderer::tlsIdx, c);
return deviceContext; return deviceContext;
} }
@@ -520,9 +556,10 @@ void Renderer::Present()
{ {
if (m_bShouldScreenGrabNextFrame) if (m_bShouldScreenGrabNextFrame)
{ {
int *linearData = new int[kScreenGrabWidth * kScreenGrabHeight]; unsigned char *linearData = new unsigned char[kScreenGrabWidth * kScreenGrabHeight * 4];
ID3D11Texture2D *backBuffer = nullptr;
ID3D11Texture2D *stagingTexture = nullptr; ID3D11Texture2D *backBuffer = NULL;
ID3D11Texture2D *stagingTexture = NULL;
m_pSwapChain->GetBuffer(0, IID_PPV_ARGS(&backBuffer)); m_pSwapChain->GetBuffer(0, IID_PPV_ARGS(&backBuffer));
if (backBuffer) if (backBuffer)
@@ -533,27 +570,28 @@ void Renderer::Present()
desc.BindFlags = 0; desc.BindFlags = 0;
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
desc.MiscFlags = 0; desc.MiscFlags = 0;
m_pDevice->CreateTexture2D(&desc, nullptr, &stagingTexture); m_pDevice->CreateTexture2D(&desc, NULL, &stagingTexture);
} }
if (stagingTexture && backBuffer) if (stagingTexture && backBuffer)
{ {
m_pDeviceContext->CopyResource(stagingTexture, backBuffer); m_pDeviceContext->CopyResource(stagingTexture, backBuffer);
D3D11_MAPPED_SUBRESOURCE mapped = {}; D3D11_MAPPED_SUBRESOURCE mapped = {};
if (SUCCEEDED(m_pDeviceContext->Map(stagingTexture, 0, D3D11_MAP_READ, 0, &mapped))) if (SUCCEEDED(m_pDeviceContext->Map(stagingTexture, 0, D3D11_MAP_READ, 0, &mapped)))
{ {
unsigned char *dst = reinterpret_cast<unsigned char *>(linearData);
const unsigned char *src = reinterpret_cast<const unsigned char *>(mapped.pData); const unsigned char *src = reinterpret_cast<const unsigned char *>(mapped.pData);
for (unsigned int y = 0; y < kScreenGrabHeight; ++y)
for (UINT y = 0; y < kScreenGrabHeight; ++y)
{ {
unsigned char *dstRow = dst + y * (kScreenGrabWidth * 4u); unsigned char *dstRow = linearData + y * kScreenGrabWidth * 4;
const unsigned char *srcRow = src + y * mapped.RowPitch; const unsigned char *srcRow = src + y * mapped.RowPitch;
std::memcpy(dstRow, srcRow, kScreenGrabWidth * 4u); memcpy(dstRow, srcRow, kScreenGrabWidth * 4);
for (unsigned int x = 0; x < kScreenGrabWidth; ++x)
{ for (UINT x = 0; x < kScreenGrabWidth; ++x)
dstRow[x * 4u + 3u] = 0xFF; dstRow[x * 4 + 3] = 0xFF;
}
} }
m_pDeviceContext->Unmap(stagingTexture, 0); m_pDeviceContext->Unmap(stagingTexture, 0);
} }
} }
@@ -561,22 +599,25 @@ void Renderer::Present()
static int count = 0; static int count = 0;
char fileName[304]; char fileName[304];
sprintf_s(fileName, "d:\\screen%d.png", count++); sprintf_s(fileName, "d:\\screen%d.png", count++);
D3DXIMAGE_INFO info; D3DXIMAGE_INFO info;
info.Width = kScreenGrabWidth; info.Width = kScreenGrabWidth;
info.Height = kScreenGrabHeight; info.Height = kScreenGrabHeight;
this->SaveTextureData(fileName, &info, linearData); SaveTextureData(fileName, &info, reinterpret_cast<int *>(linearData));
delete[] linearData; delete[] linearData;
if (stagingTexture) if (stagingTexture)
{ {
stagingTexture->Release(); stagingTexture->Release();
stagingTexture = nullptr; stagingTexture = NULL;
} }
if (backBuffer) if (backBuffer)
{ {
backBuffer->Release(); backBuffer->Release();
backBuffer = nullptr; backBuffer = NULL;
} }
m_bShouldScreenGrabNextFrame = false; m_bShouldScreenGrabNextFrame = false;
} }
@@ -591,15 +632,18 @@ void Renderer::Resume()
void Renderer::SetClearColour(const float colourRGBA[4]) void Renderer::SetClearColour(const float colourRGBA[4])
{ {
std::memcpy(m_fClearColor, colourRGBA, sizeof(m_fClearColor)); for (int i = 0; i < 4; ++i)
m_fClearColor[i] = colourRGBA[i];
Renderer::Context *c = static_cast<Renderer::Context *>(TlsGetValue(Renderer::tlsIdx)); Renderer::Context &c = getContext();
if (c) if (&c)
{ {
D3D11_MAPPED_SUBRESOURCE mapped = {}; D3D11_MAPPED_SUBRESOURCE mapped = {};
c->m_pDeviceContext->Map(c->m_clearColorBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped); if (SUCCEEDED(c.m_pDeviceContext->Map(c.m_clearColorBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped)))
std::memcpy(mapped.pData, colourRGBA, sizeof(float) * 4); {
c->m_pDeviceContext->Unmap(c->m_clearColorBuffer, 0); *(DirectX::XMVECTOR*)mapped.pData = DirectX::XMVectorSet(colourRGBA[0], colourRGBA[1], colourRGBA[2], colourRGBA[3]);
c.m_pDeviceContext->Unmap(c.m_clearColorBuffer, 0);
}
} }
} }
@@ -610,71 +654,64 @@ void Renderer::SetupShaders()
vertexStrideTable = new unsigned int[C4JRender::VERTEX_TYPE_COUNT]; vertexStrideTable = new unsigned int[C4JRender::VERTEX_TYPE_COUNT];
inputLayoutTable = new ID3D11InputLayout *[C4JRender::VERTEX_TYPE_COUNT]; inputLayoutTable = new ID3D11InputLayout *[C4JRender::VERTEX_TYPE_COUNT];
for (unsigned int i = 0; i < C4JRender::VERTEX_TYPE_COUNT; ++i) for (UINT i = 0; i < C4JRender::VERTEX_TYPE_COUNT; ++i)
{ {
vertexShaderTable[i] = nullptr; vertexShaderTable[i] = NULL;
inputLayoutTable[i] = nullptr; inputLayoutTable[i] = NULL;
vertexStrideTable[i] = g_vertexStrides[i]; vertexStrideTable[i] = g_vertexStrides[i];
} }
for (unsigned int i = 0; i < C4JRender::PIXEL_SHADER_COUNT; ++i) for (UINT i = 0; i < C4JRender::PIXEL_SHADER_COUNT; ++i)
{ {
pixelShaderTable[i] = nullptr; pixelShaderTable[i] = NULL;
} }
screenSpaceVertexShader = nullptr; screenSpaceVertexShader = NULL;
screenClearVertexShader = nullptr; screenClearVertexShader = NULL;
screenSpacePixelShader = nullptr; screenSpacePixelShader = NULL;
screenClearPixelShader = nullptr; screenClearPixelShader = NULL;
m_pDevice->CreateVertexShader(g_main_VS_PF3_TF2_CB4_NB4_XW1, sizeof(g_main_VS_PF3_TF2_CB4_NB4_XW1), nullptr, m_pDevice->CreateVertexShader(g_main_VS_PF3_TF2_CB4_NB4_XW1, sizeof(g_main_VS_PF3_TF2_CB4_NB4_XW1), NULL, &vertexShaderTable[C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1]);
&vertexShaderTable[C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1]); m_pDevice->CreateVertexShader(g_main_VS_Compressed, sizeof(g_main_VS_Compressed), NULL, &vertexShaderTable[C4JRender::VERTEX_TYPE_COMPRESSED]);
m_pDevice->CreateVertexShader(g_main_VS_Compressed, sizeof(g_main_VS_Compressed), nullptr, &vertexShaderTable[C4JRender::VERTEX_TYPE_COMPRESSED]); m_pDevice->CreateVertexShader(g_main_VS_PF3_TF2_CB4_NB4_XW1_LIGHTING, sizeof(g_main_VS_PF3_TF2_CB4_NB4_XW1_LIGHTING), NULL, &vertexShaderTable[C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1_LIT]);
m_pDevice->CreateVertexShader(g_main_VS_PF3_TF2_CB4_NB4_XW1_LIGHTING, sizeof(g_main_VS_PF3_TF2_CB4_NB4_XW1_LIGHTING), nullptr, m_pDevice->CreateVertexShader(g_main_VS_PF3_TF2_CB4_NB4_XW1_TEXGEN, sizeof(g_main_VS_PF3_TF2_CB4_NB4_XW1_TEXGEN), NULL, &vertexShaderTable[C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1_TEXGEN]);
&vertexShaderTable[C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1_LIT]); m_pDevice->CreateVertexShader(g_main_VS_ScreenSpace, sizeof(g_main_VS_ScreenSpace), NULL, &screenSpaceVertexShader);
m_pDevice->CreateVertexShader(g_main_VS_PF3_TF2_CB4_NB4_XW1_TEXGEN, sizeof(g_main_VS_PF3_TF2_CB4_NB4_XW1_TEXGEN), nullptr, m_pDevice->CreateVertexShader(g_main_VS_ScreenClear, sizeof(g_main_VS_ScreenClear), NULL, &screenClearVertexShader);
&vertexShaderTable[C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1_TEXGEN]);
m_pDevice->CreateVertexShader(g_main_VS_ScreenSpace, sizeof(g_main_VS_ScreenSpace), nullptr, &screenSpaceVertexShader);
m_pDevice->CreateVertexShader(g_main_VS_ScreenClear, sizeof(g_main_VS_ScreenClear), nullptr, &screenClearVertexShader);
m_pDevice->CreatePixelShader(g_main_PS_Standard, sizeof(g_main_PS_Standard), nullptr, &pixelShaderTable[C4JRender::PIXEL_SHADER_TYPE_STANDARD]); m_pDevice->CreatePixelShader(g_main_PS_Standard, sizeof(g_main_PS_Standard), NULL, &pixelShaderTable[C4JRender::PIXEL_SHADER_TYPE_STANDARD]);
m_pDevice->CreatePixelShader(g_main_PS_TextureProjection, sizeof(g_main_PS_TextureProjection), nullptr, m_pDevice->CreatePixelShader(g_main_PS_TextureProjection, sizeof(g_main_PS_TextureProjection), NULL, &pixelShaderTable[C4JRender::PIXEL_SHADER_TYPE_PROJECTION]);
&pixelShaderTable[C4JRender::PIXEL_SHADER_TYPE_PROJECTION]); m_pDevice->CreatePixelShader(g_main_PS_ForceLOD, sizeof(g_main_PS_ForceLOD), NULL, &pixelShaderTable[C4JRender::PIXEL_SHADER_TYPE_FORCELOD]);
m_pDevice->CreatePixelShader(g_main_PS_ForceLOD, sizeof(g_main_PS_ForceLOD), nullptr, &pixelShaderTable[C4JRender::PIXEL_SHADER_TYPE_FORCELOD]); m_pDevice->CreatePixelShader(g_main_PS_ScreenSpace, sizeof(g_main_PS_ScreenSpace), NULL, &screenSpacePixelShader);
m_pDevice->CreatePixelShader(g_main_PS_ScreenSpace, sizeof(g_main_PS_ScreenSpace), nullptr, &screenSpacePixelShader); m_pDevice->CreatePixelShader(g_main_PS_ScreenClear, sizeof(g_main_PS_ScreenClear), NULL, &screenClearPixelShader);
m_pDevice->CreatePixelShader(g_main_PS_ScreenClear, sizeof(g_main_PS_ScreenClear), nullptr, &screenClearPixelShader);
m_pDevice->CreateInputLayout(g_vertex_PTN_Elements_PF3_TF2_CB4_NB4_XW1, 5, g_main_VS_PF3_TF2_CB4_NB4_XW1, sizeof(g_main_VS_PF3_TF2_CB4_NB4_XW1), m_pDevice->CreateInputLayout(g_vertex_PTN_Elements_PF3_TF2_CB4_NB4_XW1, 5, g_main_VS_PF3_TF2_CB4_NB4_XW1, sizeof(g_main_VS_PF3_TF2_CB4_NB4_XW1), &inputLayoutTable[C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1]);
&inputLayoutTable[C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1]); m_pDevice->CreateInputLayout(g_vertex_PTN_Elements_Compressed, 2, g_main_VS_Compressed, sizeof(g_main_VS_Compressed), &inputLayoutTable[C4JRender::VERTEX_TYPE_COMPRESSED]);
m_pDevice->CreateInputLayout(g_vertex_PTN_Elements_Compressed, 2, g_main_VS_Compressed, sizeof(g_main_VS_Compressed), m_pDevice->CreateInputLayout(g_vertex_PTN_Elements_PF3_TF2_CB4_NB4_XW1, 5, g_main_VS_PF3_TF2_CB4_NB4_XW1_LIGHTING, sizeof(g_main_VS_PF3_TF2_CB4_NB4_XW1_LIGHTING), &inputLayoutTable[C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1_LIT]);
&inputLayoutTable[C4JRender::VERTEX_TYPE_COMPRESSED]); m_pDevice->CreateInputLayout(g_vertex_PTN_Elements_PF3_TF2_CB4_NB4_XW1, 5, g_main_VS_PF3_TF2_CB4_NB4_XW1_TEXGEN, sizeof(g_main_VS_PF3_TF2_CB4_NB4_XW1_TEXGEN), &inputLayoutTable[C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1_TEXGEN]);
m_pDevice->CreateInputLayout(g_vertex_PTN_Elements_PF3_TF2_CB4_NB4_XW1, 5, g_main_VS_PF3_TF2_CB4_NB4_XW1_LIGHTING,
sizeof(g_main_VS_PF3_TF2_CB4_NB4_XW1_LIGHTING), &inputLayoutTable[C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1_LIT]);
m_pDevice->CreateInputLayout(g_vertex_PTN_Elements_PF3_TF2_CB4_NB4_XW1, 5, g_main_VS_PF3_TF2_CB4_NB4_XW1_TEXGEN,
sizeof(g_main_VS_PF3_TF2_CB4_NB4_XW1_TEXGEN), &inputLayoutTable[C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1_TEXGEN]);
} }
void Renderer::StartFrame() void Renderer::StartFrame()
{ {
Renderer::Context &c = this->getContext(); Renderer::Context &c = getContext();
activeVertexType = 0xFFFFFFFFu; activeVertexType = -1;
activePixelType = 0xFFFFFFFFu; activePixelType = -1;
this->TextureBindVertex(-1); TextureBindVertex(-1);
this->TextureBind(-1); TextureBind(-1);
this->StateSetColour(1.0f, 1.0f, 1.0f, 1.0f);
this->StateSetDepthMask(true); StateSetColour(1.0f, 1.0f, 1.0f, 1.0f);
this->StateSetBlendEnable(true); StateSetDepthMask(true);
this->StateSetBlendFunc(D3D11_BLEND_SRC_ALPHA, D3D11_BLEND_INV_SRC_ALPHA); StateSetBlendEnable(true);
this->StateSetBlendFactor(0xFFFFFFFFu); StateSetBlendFunc(D3D11_BLEND_SRC_ALPHA, D3D11_BLEND_INV_SRC_ALPHA);
this->StateSetAlphaFunc(D3D11_COMPARISON_GREATER, 0.1f); StateSetBlendFactor(0xFFFFFFFF);
this->StateSetDepthFunc(D3D11_COMPARISON_LESS_EQUAL); StateSetAlphaFunc(D3D11_COMPARISON_GREATER, 0.1f);
this->StateSetFaceCull(true); StateSetDepthFunc(D3D11_COMPARISON_LESS_EQUAL);
this->StateSetLineWidth(1.0f); StateSetFaceCull(true);
this->StateSetWriteEnable(true, true, true, true); StateSetLineWidth(1.0f);
this->StateSetDepthTestEnable(false); StateSetWriteEnable(true, true, true, true);
this->StateSetAlphaTestEnable(true); StateSetDepthTestEnable(false);
StateSetAlphaTestEnable(true);
c.m_pDeviceContext->VSSetConstantBuffers(0, 10, &c.m_modelViewMatrix); c.m_pDeviceContext->VSSetConstantBuffers(0, 10, &c.m_modelViewMatrix);
c.m_pDeviceContext->PSSetConstantBuffers(0, 6, &c.m_tintColorBuffer); c.m_pDeviceContext->PSSetConstantBuffers(0, 6, &c.m_tintColorBuffer);
@@ -709,16 +746,13 @@ Renderer::Context &Renderer::getContext()
void Renderer::CaptureThumbnail(ImageFileBuffer *pngOut) void Renderer::CaptureThumbnail(ImageFileBuffer *pngOut)
{ {
Renderer::Context &c = this->getContext(); Renderer::Context &c = getContext();
float left; float left = 0.0f;
float bottom; float bottom = 0.0f;
float right; float right = 1.0f;
float top; float top = 1.0f;
left = 0.0f;
bottom = 0.0f;
right = 1.0f;
top = 1.0f;
switch (m_ViewportType) switch (m_ViewportType)
{ {
case C4JRender::VIEWPORT_TYPE_SPLIT_TOP: case C4JRender::VIEWPORT_TYPE_SPLIT_TOP:
@@ -753,33 +787,35 @@ void Renderer::CaptureThumbnail(ImageFileBuffer *pngOut)
break; break;
} }
float leftScaled = left * kAspectRatio; float aspectRatio = IsWidescreen() ? (16.0f / 9.0f) : (4.0f / 3.0f);
float rightScaled = right * kAspectRatio;
const float viewportHeight = top - bottom;
const float viewportScaledWidth = rightScaled - leftScaled;
if (viewportHeight <= viewportScaledWidth) right *= aspectRatio;
left *= aspectRatio;
float width = right - left;
float height = top - bottom;
if (height > width)
{ {
const float pad = (viewportScaledWidth - viewportHeight) * 0.5f; float diff = (height - width) * 0.5f;
leftScaled += pad; bottom += diff;
rightScaled -= pad; top -= diff;
} }
else else
{ {
const float pad = (viewportHeight - viewportScaledWidth) * 0.5f; float diff = (width - height) * 0.5f;
bottom += pad; left += diff;
top -= pad; right -= diff;
} }
const float sampleLeft = leftScaled / kAspectRatio; left /= aspectRatio;
const float sampleWidth = (rightScaled - leftScaled) / kAspectRatio; right /= aspectRatio;
const float sampleHeight = top - bottom;
ID3D11BlendState *blendState = nullptr; ID3D11BlendState *blendState = NULL;
ID3D11DepthStencilState *depthState = nullptr; ID3D11DepthStencilState *depthState = NULL;
ID3D11RasterizerState *rasterizerState = nullptr; ID3D11RasterizerState *rasterizerState = NULL;
ID3D11SamplerState *samplerState = nullptr; ID3D11SamplerState *samplerState = NULL;
ID3D11Texture2D *stagingTexture = nullptr; ID3D11Texture2D *stagingTexture = NULL;
D3D11_BLEND_DESC blendDesc = {}; D3D11_BLEND_DESC blendDesc = {};
blendDesc.RenderTarget[0].BlendEnable = FALSE; blendDesc.RenderTarget[0].BlendEnable = FALSE;
@@ -797,16 +833,13 @@ void Renderer::CaptureThumbnail(ImageFileBuffer *pngOut)
depthDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ZERO; depthDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ZERO;
depthDesc.DepthFunc = D3D11_COMPARISON_ALWAYS; depthDesc.DepthFunc = D3D11_COMPARISON_ALWAYS;
depthDesc.StencilEnable = FALSE; depthDesc.StencilEnable = FALSE;
depthDesc.StencilReadMask = 0xFFu; depthDesc.StencilReadMask = 0xFF;
depthDesc.StencilWriteMask = 0xFFu; depthDesc.StencilWriteMask = 0xFF;
depthDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; depthDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
depthDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP; depthDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP;
depthDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; depthDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
depthDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS; depthDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
depthDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; depthDesc.BackFace = depthDesc.FrontFace;
depthDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP;
depthDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
depthDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
m_pDevice->CreateDepthStencilState(&depthDesc, &depthState); m_pDevice->CreateDepthStencilState(&depthDesc, &depthState);
D3D11_RASTERIZER_DESC rasterDesc = {}; D3D11_RASTERIZER_DESC rasterDesc = {};
@@ -827,14 +860,14 @@ void Renderer::CaptureThumbnail(ImageFileBuffer *pngOut)
samplerDesc.MaxLOD = (std::numeric_limits<float>::max)(); samplerDesc.MaxLOD = (std::numeric_limits<float>::max)();
m_pDevice->CreateSamplerState(&samplerDesc, &samplerState); m_pDevice->CreateSamplerState(&samplerDesc, &samplerState);
c.m_pDeviceContext->VSSetShader(screenSpaceVertexShader, nullptr, 0); c.m_pDeviceContext->VSSetShader(screenSpaceVertexShader, NULL, 0);
c.m_pDeviceContext->IASetInputLayout(nullptr); c.m_pDeviceContext->IASetInputLayout(NULL);
c.m_pDeviceContext->PSSetShader(screenSpacePixelShader, nullptr, 0); c.m_pDeviceContext->PSSetShader(screenSpacePixelShader, NULL, 0);
c.m_pDeviceContext->OMSetBlendState(blendState, nullptr, 0xFFFFFFFFu); c.m_pDeviceContext->OMSetBlendState(blendState, NULL, -1);
c.m_pDeviceContext->OMSetDepthStencilState(depthState, 0); c.m_pDeviceContext->OMSetDepthStencilState(depthState, 0);
c.m_pDeviceContext->RSSetState(rasterizerState); c.m_pDeviceContext->RSSetState(rasterizerState);
for (unsigned int i = 0; i < MAX_MIP_LEVELS - 1; ++i) for (UINT i = 0; i < MAX_MIP_LEVELS - 1; ++i)
{ {
D3D11_VIEWPORT viewport = {}; D3D11_VIEWPORT viewport = {};
viewport.TopLeftX = 0.0f; viewport.TopLeftX = 0.0f;
@@ -844,7 +877,7 @@ void Renderer::CaptureThumbnail(ImageFileBuffer *pngOut)
viewport.MinDepth = 0.0f; viewport.MinDepth = 0.0f;
viewport.MaxDepth = 1.0f; viewport.MaxDepth = 1.0f;
c.m_pDeviceContext->OMSetRenderTargets(1, &renderTargetViews[i], nullptr); c.m_pDeviceContext->OMSetRenderTargets(1, &renderTargetViews[i], NULL);
c.m_pDeviceContext->RSSetViewports(1, &viewport); c.m_pDeviceContext->RSSetViewports(1, &viewport);
ID3D11ShaderResourceView *inputTexture = (i == 0) ? renderTargetShaderResourceView : renderTargetShaderResourceViews[i - 1]; ID3D11ShaderResourceView *inputTexture = (i == 0) ? renderTargetShaderResourceView : renderTargetShaderResourceViews[i - 1];
@@ -854,13 +887,14 @@ void Renderer::CaptureThumbnail(ImageFileBuffer *pngOut)
D3D11_MAPPED_SUBRESOURCE mapped = {}; D3D11_MAPPED_SUBRESOURCE mapped = {};
c.m_pDeviceContext->Map(c.m_thumbnailBoundsBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped); c.m_pDeviceContext->Map(c.m_thumbnailBoundsBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped);
float *constants = static_cast<float *>(mapped.pData);
float *constants = (float *)mapped.pData;
if (i == 0) if (i == 0)
{ {
constants[0] = sampleLeft; constants[0] = left;
constants[1] = bottom; constants[1] = bottom;
constants[2] = sampleWidth; constants[2] = right - left;
constants[3] = sampleHeight; constants[3] = top - bottom;
} }
else else
{ {
@@ -869,73 +903,83 @@ void Renderer::CaptureThumbnail(ImageFileBuffer *pngOut)
constants[2] = 1.0f; constants[2] = 1.0f;
constants[3] = 1.0f; constants[3] = 1.0f;
} }
c.m_pDeviceContext->Unmap(c.m_thumbnailBoundsBuffer, 0); c.m_pDeviceContext->Unmap(c.m_thumbnailBoundsBuffer, 0);
c.m_pDeviceContext->Draw(4, 0); c.m_pDeviceContext->Draw(4, 0);
} }
D3D11_TEXTURE2D_DESC desc = {}; D3D11_TEXTURE2D_DESC texDesc = {};
renderTargetTextures[MAX_MIP_LEVELS - 2]->GetDesc(&desc); renderTargetTextures[MAX_MIP_LEVELS - 2]->GetDesc(&texDesc);
desc.Usage = D3D11_USAGE_STAGING; texDesc.Usage = D3D11_USAGE_STAGING;
desc.BindFlags = 0; texDesc.BindFlags = 0;
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; texDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
desc.MiscFlags = 0; texDesc.MiscFlags = 0;
m_pDevice->CreateTexture2D(&desc, nullptr, &stagingTexture); m_pDevice->CreateTexture2D(&texDesc, NULL, &stagingTexture);
const unsigned int stride = kThumbnailSize * 4;
unsigned char *linearData = new unsigned char[kThumbnailSize * stride];
unsigned char *linearData = new unsigned char[kThumbnailSize * kThumbnailSize * 4u];
if (stagingTexture) if (stagingTexture)
{ {
c.m_pDeviceContext->CopyResource(stagingTexture, renderTargetTextures[MAX_MIP_LEVELS - 2]); c.m_pDeviceContext->CopyResource(stagingTexture, renderTargetTextures[MAX_MIP_LEVELS - 2]);
D3D11_MAPPED_SUBRESOURCE mapped = {}; D3D11_MAPPED_SUBRESOURCE mapped = {};
if (SUCCEEDED(c.m_pDeviceContext->Map(stagingTexture, 0, D3D11_MAP_READ, 0, &mapped))) if (SUCCEEDED(c.m_pDeviceContext->Map(stagingTexture, 0, D3D11_MAP_READ, 0, &mapped)))
{ {
const unsigned char *src = static_cast<const unsigned char *>(mapped.pData); const unsigned char *src = static_cast<const unsigned char *>(mapped.pData);
unsigned char *dst = linearData; unsigned char *dst = linearData;
for (unsigned int y = 0; y < kThumbnailSize; ++y)
for (UINT y = 0; y < kThumbnailSize; ++y)
{ {
std::memcpy(dst, src, kThumbnailSize * 4u); memcpy(dst, src, stride);
for (unsigned int x = 0; x < kThumbnailSize; ++x)
unsigned char *alpha = dst + 3;
for (UINT x = 0; x < kThumbnailSize; ++x)
{ {
dst[x * 4u + 3u] = 0xFF; *alpha = 0xFF;
alpha += 4;
} }
src += mapped.RowPitch; src += mapped.RowPitch;
dst += kThumbnailSize * 4u; dst += stride;
} }
c.m_pDeviceContext->Unmap(stagingTexture, 0); c.m_pDeviceContext->Unmap(stagingTexture, 0);
} }
} }
this->ConvertLinearToPng(pngOut, linearData, kThumbnailSize, kThumbnailSize); ConvertLinearToPng(pngOut, linearData, kThumbnailSize, kThumbnailSize);
delete[] linearData; delete[] linearData;
if (stagingTexture) if (stagingTexture)
{ {
stagingTexture->Release(); stagingTexture->Release();
stagingTexture = nullptr; stagingTexture = NULL;
} }
if (samplerState) if (samplerState)
{ {
samplerState->Release(); samplerState->Release();
samplerState = nullptr; samplerState = NULL;
} }
if (rasterizerState) if (rasterizerState)
{ {
rasterizerState->Release(); rasterizerState->Release();
rasterizerState = nullptr; rasterizerState = NULL;
} }
if (depthState) if (depthState)
{ {
depthState->Release(); depthState->Release();
depthState = nullptr; depthState = NULL;
} }
if (blendState) if (blendState)
{ {
blendState->Release(); blendState->Release();
blendState = nullptr; blendState = NULL;
} }
c.m_pDeviceContext->OMSetBlendState(this->GetManagedBlendState(), c.blendFactor, 0xFFFFFFFFu); c.m_pDeviceContext->OMSetBlendState(GetManagedBlendState(), c.blendFactor, -1);
c.m_pDeviceContext->OMSetDepthStencilState(this->GetManagedDepthStencilState(), 0); c.m_pDeviceContext->OMSetDepthStencilState(GetManagedDepthStencilState(), 0);
c.m_pDeviceContext->RSSetState(this->GetManagedRasterizerState()); c.m_pDeviceContext->RSSetState(GetManagedRasterizerState());
D3D11_VIEWPORT viewport = {}; D3D11_VIEWPORT viewport = {};
viewport.TopLeftX = 0.0f; viewport.TopLeftX = 0.0f;
@@ -944,8 +988,10 @@ void Renderer::CaptureThumbnail(ImageFileBuffer *pngOut)
viewport.Height = (float)backBufferHeight; viewport.Height = (float)backBufferHeight;
viewport.MinDepth = 0.0f; viewport.MinDepth = 0.0f;
viewport.MaxDepth = 1.0f; viewport.MaxDepth = 1.0f;
c.m_pDeviceContext->RSSetViewports(1, &viewport); c.m_pDeviceContext->RSSetViewports(1, &viewport);
c.m_pDeviceContext->OMSetRenderTargets(1, &renderTargetView, depthStencilView); c.m_pDeviceContext->OMSetRenderTargets(1, &renderTargetView, depthStencilView);
activeVertexType = 0xFFFFFFFFu;
activePixelType = 0xFFFFFFFFu; activeVertexType = -1;
activePixelType = -1;
} }
+1 -1
View File
@@ -1,4 +1,4 @@
#pragma once #include "stdafx.h"
#include "Renderer.h" #include "Renderer.h"
#include <cstring> #include <cstring>
+84 -106
View File
@@ -1,13 +1,12 @@
#pragma once #include "stdafx.h"
#include "Renderer.h" #include "Renderer.h"
#include <cstddef>
#include <cstring> #include <cstring>
#include <limits> #include <limits>
ID3D11BlendState *Renderer::GetManagedBlendState() ID3D11BlendState *Renderer::GetManagedBlendState()
{ {
Context &c = this->getContext(); Context &c = getContext();
const D3D11_RENDER_TARGET_BLEND_DESC &rtBlend = c.blendDesc.RenderTarget[0]; 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) | 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); auto it = managedBlendStates.find(key);
if (it != managedBlendStates.end()) if (it != managedBlendStates.end())
{
return it->second; return it->second;
}
ID3D11BlendState *state = nullptr; ID3D11BlendState *state = NULL;
m_pDevice->CreateBlendState(&c.blendDesc, &state); m_pDevice->CreateBlendState(&c.blendDesc, &state);
managedBlendStates.emplace(key, state); managedBlendStates.emplace(key, state);
return state; return state;
@@ -27,18 +24,16 @@ ID3D11BlendState *Renderer::GetManagedBlendState()
ID3D11DepthStencilState *Renderer::GetManagedDepthStencilState() 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) | 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); (c.depthStencilDesc.DepthWriteMask == D3D11_DEPTH_WRITE_MASK_ALL ? 1 : 0);
auto it = managedDepthStencilStates.find(key); auto it = managedDepthStencilStates.find(key);
if (it != managedDepthStencilStates.end()) if (it != managedDepthStencilStates.end())
{
return it->second; return it->second;
}
ID3D11DepthStencilState *state = nullptr; ID3D11DepthStencilState *state = NULL;
m_pDevice->CreateDepthStencilState(&c.depthStencilDesc, &state); m_pDevice->CreateDepthStencilState(&c.depthStencilDesc, &state);
managedDepthStencilStates.emplace(key, state); managedDepthStencilStates.emplace(key, state);
return state; return state;
@@ -46,7 +41,7 @@ ID3D11DepthStencilState *Renderer::GetManagedDepthStencilState()
ID3D11RasterizerState *Renderer::GetManagedRasterizerState() ID3D11RasterizerState *Renderer::GetManagedRasterizerState()
{ {
Context &c = this->getContext(); Context &c = getContext();
const int key = (static_cast<std::uint8_t>(c.rasterizerDesc.DepthBias)) | const int key = (static_cast<std::uint8_t>(c.rasterizerDesc.DepthBias)) |
(static_cast<std::uint8_t>(static_cast<int>(c.rasterizerDesc.SlopeScaledDepthBias)) << 8) | (static_cast<std::uint8_t>(static_cast<int>(c.rasterizerDesc.SlopeScaledDepthBias)) << 8) |
@@ -54,11 +49,9 @@ ID3D11RasterizerState *Renderer::GetManagedRasterizerState()
auto it = managedRasterizerStates.find(key); auto it = managedRasterizerStates.find(key);
if (it != managedRasterizerStates.end()) if (it != managedRasterizerStates.end())
{
return it->second; return it->second;
}
ID3D11RasterizerState *state = nullptr; ID3D11RasterizerState *state = NULL;
m_pDevice->CreateRasterizerState(&c.rasterizerDesc, &state); m_pDevice->CreateRasterizerState(&c.rasterizerDesc, &state);
managedRasterizerStates.emplace(key, state); managedRasterizerStates.emplace(key, state);
return state; return state;
@@ -66,14 +59,12 @@ ID3D11RasterizerState *Renderer::GetManagedRasterizerState()
ID3D11SamplerState *Renderer::GetManagedSamplerState() ID3D11SamplerState *Renderer::GetManagedSamplerState()
{ {
Context &c = this->getContext(); Context &c = getContext();
const int key = m_textures[c.textureIdx].samplerParams; const int key = m_textures[c.textureIdx].samplerParams;
auto it = managedSamplerStates.find(key); auto it = managedSamplerStates.find(key);
if (it != managedSamplerStates.end()) if (it != managedSamplerStates.end())
{
return it->second; return it->second;
}
const bool clampU = (key & 0x01) != 0; const bool clampU = (key & 0x01) != 0;
const bool clampV = (key & 0x02) != 0; const bool clampV = (key & 0x02) != 0;
@@ -96,7 +87,7 @@ ID3D11SamplerState *Renderer::GetManagedSamplerState()
desc.MinLOD = -(std::numeric_limits<float>::max)(); desc.MinLOD = -(std::numeric_limits<float>::max)();
desc.MaxLOD = (std::numeric_limits<float>::max)(); desc.MaxLOD = (std::numeric_limits<float>::max)();
ID3D11SamplerState *state = nullptr; ID3D11SamplerState *state = NULL;
m_pDevice->CreateSamplerState(&desc, &state); m_pDevice->CreateSamplerState(&desc, &state);
managedSamplerStates.emplace(key, state); managedSamplerStates.emplace(key, state);
return state; return state;
@@ -104,37 +95,37 @@ ID3D11SamplerState *Renderer::GetManagedSamplerState()
void Renderer::StateSetFogEnable(bool enable) void Renderer::StateSetFogEnable(bool enable)
{ {
Context &c = this->getContext(); Context &c = getContext();
c.fogEnabled = enable ? 1 : 0; c.fogEnabled = enable ? TRUE : FALSE;
} }
void Renderer::StateSetFogMode(int mode) void Renderer::StateSetFogMode(int mode)
{ {
Context &c = this->getContext(); Context &c = getContext();
c.fogMode = mode; c.fogMode = mode;
} }
void Renderer::StateSetFogNearDistance(float dist) void Renderer::StateSetFogNearDistance(float dist)
{ {
Context &c = this->getContext(); Context &c = getContext();
c.fogNearDistance = dist; c.fogNearDistance = dist;
} }
void Renderer::StateSetFogFarDistance(float dist) void Renderer::StateSetFogFarDistance(float dist)
{ {
Context &c = this->getContext(); Context &c = getContext();
c.fogFarDistance = dist; c.fogFarDistance = dist;
} }
void Renderer::StateSetFogDensity(float density) void Renderer::StateSetFogDensity(float density)
{ {
Context &c = this->getContext(); Context &c = getContext();
c.fogDensity = density; c.fogDensity = density;
} }
void Renderer::StateSetFogColour(float red, float green, float blue) void Renderer::StateSetFogColour(float red, float green, float blue)
{ {
Context &c = this->getContext(); Context &c = getContext();
c.fogColourRed = red; c.fogColourRed = red;
c.fogColourBlue = blue; c.fogColourBlue = blue;
c.fogColourGreen = green; c.fogColourGreen = green;
@@ -144,25 +135,23 @@ void Renderer::UpdateViewportState() {}
void Renderer::StateSetLightingEnable(bool enable) void Renderer::StateSetLightingEnable(bool enable)
{ {
Context &c = this->getContext(); Context &c = getContext();
if (c.commandBuffer != nullptr && c.commandBuffer->isActive != 0) if (c.commandBuffer != NULL && c.commandBuffer->isActive != 0)
{ {
c.commandBuffer->SetLightingEnable(enable); c.commandBuffer->SetLightingEnable(enable);
return; return;
} }
c.lightingEnabled = enable ? 1 : 0; c.lightingEnabled = enable ? TRUE : FALSE;
} }
void Renderer::StateSetLightColour(int light, float red, float green, float blue) void Renderer::StateSetLightColour(int light, float red, float green, float blue)
{ {
if (light >= 2) if (light >= 2)
{
return; return;
}
Context &c = this->getContext(); Context &c = getContext();
if (c.commandBuffer != nullptr && c.commandBuffer->isActive != 0) if (c.commandBuffer != NULL && c.commandBuffer->isActive != 0)
{ {
c.commandBuffer->SetLightColour(light, red, green, blue); c.commandBuffer->SetLightColour(light, red, green, blue);
return; return;
@@ -172,13 +161,13 @@ void Renderer::StateSetLightColour(int light, float red, float green, float blue
c.lightColour[light].y = green; c.lightColour[light].y = green;
c.lightColour[light].z = blue; c.lightColour[light].z = blue;
c.lightColour[light].w = 1.0f; c.lightColour[light].w = 1.0f;
c.lightingDirty = 1; c.lightingDirty = TRUE;
} }
void Renderer::StateSetLightAmbientColour(float red, float green, float blue) void Renderer::StateSetLightAmbientColour(float red, float green, float blue)
{ {
Context &c = this->getContext(); Context &c = getContext();
if (c.commandBuffer != nullptr && c.commandBuffer->isActive != 0) if (c.commandBuffer != NULL && c.commandBuffer->isActive != 0)
{ {
c.commandBuffer->SetLightAmbientColour(red, green, blue); c.commandBuffer->SetLightAmbientColour(red, green, blue);
return; return;
@@ -188,31 +177,29 @@ void Renderer::StateSetLightAmbientColour(float red, float green, float blue)
c.lightAmbientColour.y = green; c.lightAmbientColour.y = green;
c.lightAmbientColour.z = blue; c.lightAmbientColour.z = blue;
c.lightAmbientColour.w = 1.0f; c.lightAmbientColour.w = 1.0f;
c.lightingDirty = 1; c.lightingDirty = TRUE;
} }
void Renderer::StateSetLightEnable(int light, bool enable) void Renderer::StateSetLightEnable(int light, bool enable)
{ {
if (light >= 2) if (light >= 2)
{
return; return;
}
Context &c = this->getContext(); Context &c = getContext();
if (c.commandBuffer != nullptr && c.commandBuffer->isActive != 0) if (c.commandBuffer != NULL && c.commandBuffer->isActive != 0)
{ {
c.commandBuffer->SetLightEnable(light, enable); c.commandBuffer->SetLightEnable(light, enable);
return; return;
} }
c.lightEnabled[light] = enable ? 1 : 0; c.lightEnabled[light] = enable ? TRUE : FALSE;
c.lightingDirty = 1; c.lightingDirty = TRUE;
} }
void Renderer::StateSetColour(float r, float g, float b, float a) void Renderer::StateSetColour(float r, float g, float b, float a)
{ {
Context &c = this->getContext(); Context &c = getContext();
if (c.commandBuffer != nullptr && c.commandBuffer->isActive != 0) if (c.commandBuffer != NULL && c.commandBuffer->isActive != 0)
{ {
c.commandBuffer->SetColor(r, g, b, a); c.commandBuffer->SetColor(r, g, b, a);
return; return;
@@ -229,35 +216,35 @@ void Renderer::StateSetColour(float r, float g, float b, float a)
void Renderer::StateSetDepthMask(bool enable) void Renderer::StateSetDepthMask(bool enable)
{ {
Context &c = this->getContext(); Context &c = getContext();
if (c.commandBuffer != nullptr && c.commandBuffer->isActive != 0) if (c.commandBuffer != NULL && c.commandBuffer->isActive != 0)
{ {
c.commandBuffer->SetDepthMask(enable); c.commandBuffer->SetDepthMask(enable);
return; return;
} }
c.depthStencilDesc.DepthWriteMask = enable ? D3D11_DEPTH_WRITE_MASK_ALL : D3D11_DEPTH_WRITE_MASK_ZERO; c.depthStencilDesc.DepthWriteMask = enable ? D3D11_DEPTH_WRITE_MASK_ALL : D3D11_DEPTH_WRITE_MASK_ZERO;
c.m_pDeviceContext->OMSetDepthStencilState(this->GetManagedDepthStencilState(), 0); c.m_pDeviceContext->OMSetDepthStencilState(GetManagedDepthStencilState(), 0);
c.depthWriteEnabled = enable ? 1 : 0; c.depthWriteEnabled = enable ? TRUE : FALSE;
} }
void Renderer::StateSetBlendEnable(bool enable) void Renderer::StateSetBlendEnable(bool enable)
{ {
Context &c = this->getContext(); Context &c = getContext();
if (c.commandBuffer != nullptr && c.commandBuffer->isActive != 0) if (c.commandBuffer != NULL && c.commandBuffer->isActive != 0)
{ {
c.commandBuffer->SetBlendEnable(enable); c.commandBuffer->SetBlendEnable(enable);
return; return;
} }
c.blendDesc.RenderTarget[0].BlendEnable = enable ? TRUE : FALSE; 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) void Renderer::StateSetBlendFunc(int src, int dst)
{ {
Context &c = this->getContext(); Context &c = getContext();
if (c.commandBuffer != nullptr && c.commandBuffer->isActive != 0) if (c.commandBuffer != NULL && c.commandBuffer->isActive != 0)
{ {
c.commandBuffer->SetBlendFunc(src, dst); c.commandBuffer->SetBlendFunc(src, dst);
return; 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].SrcBlend = static_cast<D3D11_BLEND>(src);
c.blendDesc.RenderTarget[0].DestBlend = static_cast<D3D11_BLEND>(dst); 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) void Renderer::StateSetBlendFactor(unsigned int colour)
{ {
Context &c = this->getContext(); Context &c = getContext();
if (c.commandBuffer != nullptr && c.commandBuffer->isActive != 0) if (c.commandBuffer != NULL && c.commandBuffer->isActive != 0)
{ {
c.commandBuffer->SetBlendFactor(colour); c.commandBuffer->SetBlendFactor(colour);
return; return;
@@ -282,12 +269,12 @@ void Renderer::StateSetBlendFactor(unsigned int colour)
c.blendFactor[1] = static_cast<float>((colour >> 8) & 0xFFu) / scale; c.blendFactor[1] = static_cast<float>((colour >> 8) & 0xFFu) / scale;
c.blendFactor[2] = static_cast<float>((colour >> 16) & 0xFFu) / scale; c.blendFactor[2] = static_cast<float>((colour >> 16) & 0xFFu) / scale;
c.blendFactor[3] = static_cast<float>((colour >> 24) & 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) void Renderer::StateSetAlphaFunc(int, float param)
{ {
Context &c = this->getContext(); Context &c = getContext();
c.alphaReference = param; c.alphaReference = param;
const float alpha[4] = {0.0f, 0.0f, 0.0f, c.alphaTestEnabled ? c.alphaReference : 0.0f}; 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) void Renderer::StateSetDepthFunc(int func)
{ {
Context &c = this->getContext(); Context &c = getContext();
if (c.commandBuffer != nullptr && c.commandBuffer->isActive != 0) if (c.commandBuffer != NULL && c.commandBuffer->isActive != 0)
{ {
c.commandBuffer->SetDepthFunc(func); c.commandBuffer->SetDepthFunc(func);
return; return;
} }
c.depthStencilDesc.DepthFunc = static_cast<D3D11_COMPARISON_FUNC>(func); 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) void Renderer::StateSetFaceCull(bool enable)
{ {
Context &c = this->getContext(); Context &c = getContext();
if (c.commandBuffer != nullptr && c.commandBuffer->isActive != 0) if (c.commandBuffer != NULL && c.commandBuffer->isActive != 0)
{ {
c.commandBuffer->SetFaceCull(enable); c.commandBuffer->SetFaceCull(enable);
return; return;
} }
c.rasterizerDesc.CullMode = enable ? D3D11_CULL_BACK : D3D11_CULL_NONE; c.rasterizerDesc.CullMode = enable ? D3D11_CULL_BACK : D3D11_CULL_NONE;
c.m_pDeviceContext->RSSetState(this->GetManagedRasterizerState()); c.m_pDeviceContext->RSSetState(GetManagedRasterizerState());
c.faceCullEnabled = enable ? 1 : 0; c.faceCullEnabled = enable ? TRUE : FALSE;
} }
void Renderer::StateSetFaceCullCW(bool enable) void Renderer::StateSetFaceCullCW(bool enable)
{ {
Context &c = this->getContext(); Context &c = getContext();
if (c.faceCullEnabled) if (c.faceCullEnabled)
{
c.rasterizerDesc.CullMode = enable ? D3D11_CULL_BACK : D3D11_CULL_FRONT; c.rasterizerDesc.CullMode = enable ? D3D11_CULL_BACK : D3D11_CULL_FRONT;
}
else else
{
c.rasterizerDesc.CullMode = D3D11_CULL_NONE; c.rasterizerDesc.CullMode = D3D11_CULL_NONE;
}
c.m_pDeviceContext->RSSetState(this->GetManagedRasterizerState()); c.m_pDeviceContext->RSSetState(GetManagedRasterizerState());
} }
void Renderer::StateSetLineWidth(float) {} void Renderer::StateSetLineWidth(float) {}
void Renderer::StateSetWriteEnable(bool red, bool green, bool blue, bool alpha) void Renderer::StateSetWriteEnable(bool red, bool green, bool blue, bool alpha)
{ {
Context &c = this->getContext(); Context &c = getContext();
std::uint8_t mask = 0; std::uint8_t mask = 0;
mask |= red ? 0x1 : 0; mask |= red ? 0x1 : 0;
@@ -352,27 +335,27 @@ void Renderer::StateSetWriteEnable(bool red, bool green, bool blue, bool alpha)
mask |= alpha ? 0x8 : 0; mask |= alpha ? 0x8 : 0;
c.blendDesc.RenderTarget[0].RenderTargetWriteMask = mask; 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) void Renderer::StateSetDepthTestEnable(bool enable)
{ {
Context &c = this->getContext(); Context &c = getContext();
if (c.commandBuffer != nullptr && c.commandBuffer->isActive != 0) if (c.commandBuffer != NULL && c.commandBuffer->isActive != 0)
{ {
c.commandBuffer->SetDepthTestEnable(enable); c.commandBuffer->SetDepthTestEnable(enable);
return; return;
} }
c.depthStencilDesc.DepthEnable = enable ? TRUE : FALSE; c.depthStencilDesc.DepthEnable = enable ? TRUE : FALSE;
c.m_pDeviceContext->OMSetDepthStencilState(this->GetManagedDepthStencilState(), 0); c.m_pDeviceContext->OMSetDepthStencilState(GetManagedDepthStencilState(), 0);
c.depthTestEnabled = enable ? 1 : 0; c.depthTestEnabled = enable ? TRUE : FALSE;
} }
void Renderer::StateSetAlphaTestEnable(bool enable) void Renderer::StateSetAlphaTestEnable(bool enable)
{ {
Context &c = this->getContext(); Context &c = getContext();
c.alphaTestEnabled = enable ? 1 : 0; c.alphaTestEnabled = enable ? TRUE : FALSE;
const float alpha[4] = {0.0f, 0.0f, 0.0f, enable ? c.alphaReference : 0.0f}; const float alpha[4] = {0.0f, 0.0f, 0.0f, enable ? c.alphaReference : 0.0f};
D3D11_MAPPED_SUBRESOURCE mapped = {}; D3D11_MAPPED_SUBRESOURCE mapped = {};
@@ -383,17 +366,17 @@ void Renderer::StateSetAlphaTestEnable(bool enable)
void Renderer::StateSetDepthSlopeAndBias(float slope, float bias) void Renderer::StateSetDepthSlopeAndBias(float slope, float bias)
{ {
Context &c = this->getContext(); Context &c = getContext();
const float scale = 65536.0f; const float scale = 65536.0f;
c.rasterizerDesc.DepthBias = static_cast<int>(bias * scale); c.rasterizerDesc.DepthBias = static_cast<int>(bias * scale);
c.rasterizerDesc.SlopeScaledDepthBias = slope * scale; c.rasterizerDesc.SlopeScaledDepthBias = slope * scale;
c.m_pDeviceContext->RSSetState(this->GetManagedRasterizerState()); c.m_pDeviceContext->RSSetState(GetManagedRasterizerState());
} }
void Renderer::UpdateFogState() void Renderer::UpdateFogState()
{ {
Context &c = this->getContext(); Context &c = getContext();
ID3D11DeviceContext *d3d11 = c.m_pDeviceContext; ID3D11DeviceContext *d3d11 = c.m_pDeviceContext;
float fogParams[4] = {}; float fogParams[4] = {};
@@ -426,7 +409,7 @@ void Renderer::UpdateFogState()
void Renderer::StateSetVertexTextureUV(float u, float v) 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}; const float texgen[4] = {u - 1.0f, v - 1.0f, 0.0f, 0.0f};
D3D11_MAPPED_SUBRESOURCE mapped = {}; D3D11_MAPPED_SUBRESOURCE mapped = {};
@@ -437,7 +420,7 @@ void Renderer::StateSetVertexTextureUV(float u, float v)
void Renderer::UpdateTexGenState() void Renderer::UpdateTexGenState()
{ {
Context &c = this->getContext(); Context &c = getContext();
D3D11_MAPPED_SUBRESOURCE mapped = {}; D3D11_MAPPED_SUBRESOURCE mapped = {};
c.m_pDeviceContext->Map(c.m_texGenMatricesBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &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() void Renderer::UpdateLightingState()
{ {
Context &c = this->getContext(); Context &c = getContext();
if (!c.lightingDirty || !c.lightingEnabled) if (!c.lightingDirty || !c.lightingEnabled)
{ {
return; return;
@@ -471,18 +454,16 @@ void Renderer::UpdateLightingState()
std::memcpy(mapped.pData, c.lightDirection, lightingBytes); std::memcpy(mapped.pData, c.lightDirection, lightingBytes);
c.m_pDeviceContext->Unmap(c.m_lightingStateBuffer, 0); 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) void Renderer::StateSetLightDirection(int light, float x, float y, float z)
{ {
if (light >= 2) if (light >= 2)
{
return; return;
}
Context &c = this->getContext(); Context &c = getContext();
if (c.commandBuffer != nullptr && c.commandBuffer->isActive != 0) if (c.commandBuffer != NULL && c.commandBuffer->isActive != 0)
{ {
c.commandBuffer->SetLightDirection(light, x, y, z); c.commandBuffer->SetLightDirection(light, x, y, z);
return; return;
@@ -495,12 +476,12 @@ void Renderer::StateSetLightDirection(int light, float x, float y, float z)
const DirectX::XMVECTOR normalized = DirectX::XMVector3Normalize(transformed); const DirectX::XMVECTOR normalized = DirectX::XMVector3Normalize(transformed);
DirectX::XMStoreFloat4(&c.lightDirection[light], normalized); DirectX::XMStoreFloat4(&c.lightDirection[light], normalized);
c.lightingDirty = 1; c.lightingDirty = TRUE;
} }
void Renderer::StateSetViewport(C4JRender::eViewportType viewportType) void Renderer::StateSetViewport(C4JRender::eViewportType viewportType)
{ {
this->getContext(); getContext();
m_ViewportType = viewportType; m_ViewportType = viewportType;
const float fullWidth = static_cast<float>(backBufferWidth); 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) 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); DirectX::XMVECTOR plane = DirectX::XMVectorSet(x, y, z, w);
if (eyeSpace) if (eyeSpace)
{ {
DirectX::XMFLOAT4X4 modelView; DirectX::XMFLOAT4X4 modelView;
std::memset(&modelView, 0, sizeof(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(); DirectX::XMVECTOR determinant = DirectX::XMVectorZero();
const DirectX::XMMATRIX inverse = DirectX::XMMatrixInverse(&determinant, DirectX::XMLoadFloat4x4(&modelView)); 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) 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; D3D11_DEPTH_STENCIL_DESC desc = c.depthStencilDesc;
desc.StencilEnable = TRUE; desc.StencilEnable = TRUE;
@@ -613,26 +594,23 @@ void Renderer::StateSetStencil(D3D11_COMPARISON_FUNC function, uint8_t stencil_r
desc.FrontFace.StencilFunc = function; desc.FrontFace.StencilFunc = function;
desc.BackFace.StencilFunc = function; desc.BackFace.StencilFunc = function;
ID3D11DepthStencilState *state = nullptr; ID3D11DepthStencilState *state = NULL;
m_pDevice->CreateDepthStencilState(&desc, &state); m_pDevice->CreateDepthStencilState(&desc, &state);
m_pDeviceContext->OMSetDepthStencilState(state, stencil_ref); m_pDeviceContext->OMSetDepthStencilState(state, stencil_ref);
if (state != nullptr) if (state != NULL) state->Release();
{
state->Release();
}
} }
void Renderer::StateSetForceLOD(int LOD) void Renderer::StateSetForceLOD(int LOD)
{ {
Context &c = this->getContext(); Context &c = getContext();
c.forcedLOD = LOD; c.forcedLOD = LOD;
} }
void Renderer::StateUpdate() void Renderer::StateUpdate()
{ {
Context &c = this->getContext(); Context &c = getContext();
this->StateSetFaceCull(c.faceCullEnabled != 0); StateSetFaceCull(c.faceCullEnabled);
this->StateSetDepthMask(c.depthWriteEnabled != 0); StateSetDepthMask(c.depthWriteEnabled);
this->StateSetDepthTestEnable(c.depthTestEnabled != 0); StateSetDepthTestEnable(c.depthTestEnabled);
this->StateSetAlphaTestEnable(c.alphaTestEnabled != 0); StateSetAlphaTestEnable(c.alphaTestEnabled);
} }
+44 -28
View File
@@ -1,9 +1,10 @@
#include "stdafx.h"
#include "Renderer.h" #include "Renderer.h"
#include "libpng/png.h" #include "libpng/png.h"
unsigned char* dataStart; unsigned char* dataStart;
unsigned char* dataCurr; unsigned char *dataCurr;
unsigned char* dataEnd; unsigned char *dataEnd;
DXGI_FORMAT Renderer::textureFormats[] = { DXGI_FORMAT_B8G8R8A8_UNORM }; DXGI_FORMAT Renderer::textureFormats[] = { DXGI_FORMAT_B8G8R8A8_UNORM };
@@ -16,16 +17,17 @@ void user_write_data_init(unsigned char* pBuffer, int size)
int user_write_data_bytes_written() int user_write_data_bytes_written()
{ {
return (int)(dataCurr - dataStart); return static_cast<int>(dataCurr - dataStart);
} }
void user_write_data(png_struct_def* png_ptr, unsigned char* data, size_t length) void user_write_data(png_struct_def* png_ptr, unsigned char* src, size_t length)
{ {
int canWrite = (int)(dataEnd - dataCurr); int bytesToWrite = static_cast<int>(dataEnd - dataCurr);
if ((int)length < canWrite) if (static_cast<int>(length) < bytesToWrite)
canWrite = (int)length; bytesToWrite = static_cast<int>(length);
memcpy(dataCurr, data, canWrite);
dataCurr = (unsigned char*)dataCurr + canWrite; memcpy(dataCurr, src, bytesToWrite);
dataCurr += bytesToWrite;
} }
void user_flush_data(png_struct_def* png_ptr) void user_flush_data(png_struct_def* png_ptr)
@@ -51,11 +53,17 @@ int Renderer::TextureCreate()
void Renderer::TextureFree(int idx) void Renderer::TextureFree(int idx)
{ {
m_textures[idx].texture->Release(); if (m_textures[idx].texture)
m_textures[idx].view->Release(); {
m_textures[idx].view = NULL; m_textures[idx].texture->Release();
m_textures[idx].texture = NULL;
}
if (m_textures[idx].view)
{
m_textures[idx].view->Release();
m_textures[idx].view = NULL;
}
m_textures[idx].allocated = false; m_textures[idx].allocated = false;
m_textures[idx].texture = NULL;
} }
void Renderer::TextureBind(int idx) void Renderer::TextureBind(int idx)
@@ -63,7 +71,7 @@ void Renderer::TextureBind(int idx)
if (idx == -1) if (idx == -1)
idx = defaultTextureIndex; idx = defaultTextureIndex;
Context& c = this->getContext(); Context& c = getContext();
if (c.commandBuffer && c.commandBuffer->isActive) if (c.commandBuffer && c.commandBuffer->isActive)
c.commandBuffer->BindTexture(idx); c.commandBuffer->BindTexture(idx);
@@ -79,7 +87,7 @@ void Renderer::TextureBindVertex(int idx)
if (idx == -1) if (idx == -1)
idx = defaultTextureIndex; idx = defaultTextureIndex;
Context& c = this->getContext(); Context& c = getContext();
c.textureIdx = idx; c.textureIdx = idx;
c.m_pDeviceContext->VSSetShaderResources(0, 1, &m_textures[idx].view); c.m_pDeviceContext->VSSetShaderResources(0, 1, &m_textures[idx].view);
@@ -89,19 +97,19 @@ void Renderer::TextureBindVertex(int idx)
void Renderer::TextureSetTextureLevels(int levels) void Renderer::TextureSetTextureLevels(int levels)
{ {
Context& c = this->getContext(); Context& c = getContext();
m_textures[c.textureIdx].mipLevels = levels; m_textures[c.textureIdx].mipLevels = levels;
} }
int Renderer::TextureGetTextureLevels() int Renderer::TextureGetTextureLevels()
{ {
Context& c = this->getContext(); Context& c = getContext();
return m_textures[c.textureIdx].mipLevels; return m_textures[c.textureIdx].mipLevels;
} }
void Renderer::TextureData(int width, int height, void* data, int level, C4JRender::eTextureFormat format) void Renderer::TextureData(int width, int height, void* data, int level, C4JRender::eTextureFormat format)
{ {
Context& c = this->getContext(); Context& c = getContext();
int idx = c.textureIdx; int idx = c.textureIdx;
m_textures[idx].textureFormat = format; m_textures[idx].textureFormat = format;
@@ -125,13 +133,19 @@ void Renderer::TextureData(int width, int height, void* data, int level, C4JRend
m_pDevice->CreateShaderResourceView(m_textures[idx].texture, NULL, &m_textures[idx].view); m_pDevice->CreateShaderResourceView(m_textures[idx].texture, NULL, &m_textures[idx].view);
} }
c.m_pDeviceContext->UpdateSubresource(m_textures[idx].texture, level, NULL, c.m_pDeviceContext->UpdateSubresource(
data, width * 4, width * height * 4); m_textures[idx].texture,
level,
NULL,
data,
static_cast<UINT>(width * 4),
static_cast<UINT>(width * height * 4)
);
} }
void Renderer::TextureDataUpdate(int xoffset, int yoffset, int width, int height, void* data, int level) void Renderer::TextureDataUpdate(int xoffset, int yoffset, int width, int height, void* data, int level)
{ {
Context& c = this->getContext(); Context& c = getContext();
int idx = c.textureIdx; int idx = c.textureIdx;
D3D11_TEXTURE2D_DESC desc; D3D11_TEXTURE2D_DESC desc;
@@ -150,14 +164,14 @@ void Renderer::TextureDataUpdate(int xoffset, int yoffset, int width, int height
level, level,
&box, &box,
data, data,
width * 4, static_cast<UINT>(width * 4),
width * height * 4 static_cast<UINT>(width * height * 4)
); );
} }
void Renderer::TextureSetParam(int param, int value) void Renderer::TextureSetParam(int param, int value)
{ {
Context& c = this->getContext(); Context& c = getContext();
int idx = c.textureIdx; int idx = c.textureIdx;
switch (param) switch (param)
@@ -197,7 +211,7 @@ void Renderer::TextureDynamicUpdateEnd()
void Renderer::UpdateTextureState(bool bVertex) void Renderer::UpdateTextureState(bool bVertex)
{ {
Context& c = this->getContext(); Context& c = getContext();
ID3D11SamplerState* pSampler = GetManagedSamplerState(); ID3D11SamplerState* pSampler = GetManagedSamplerState();
if (bVertex) if (bVertex)
@@ -212,7 +226,8 @@ HRESULT Renderer::LoadTextureData(const char* szFilename, D3DXIMAGE_INFO* pSrcIn
memset(&image, 0, sizeof(image)); memset(&image, 0, sizeof(image));
image.version = PNG_IMAGE_VERSION; image.version = PNG_IMAGE_VERSION;
png_image_begin_read_from_file(&image, szFilename); if (!png_image_begin_read_from_file(&image, szFilename))
return -1;
// TODO(3UR): why crash? // TODO(3UR): why crash?
//if ((image.format & 3u) > 1) //if ((image.format & 3u) > 1)
@@ -235,7 +250,8 @@ HRESULT Renderer::LoadTextureData(BYTE* pbData, DWORD dwBytes, D3DXIMAGE_INFO* p
memset(&image, 0, sizeof(image)); memset(&image, 0, sizeof(image));
image.version = PNG_IMAGE_VERSION; image.version = PNG_IMAGE_VERSION;
png_image_begin_read_from_memory(&image, pbData, dwBytes); if (!png_image_begin_read_from_memory(&image, pbData, dwBytes))
return -1;
// TODO(3UR): why crash? // TODO(3UR): why crash?
//if ((image.format & 3u) > 1) //if ((image.format & 3u) > 1)
@@ -280,7 +296,7 @@ HRESULT Renderer::SaveTextureDataToMemory(void* pOutput, int outputCapacity, int
png_image_write_to_stdio(&image, NULL, 0, ppDataIn, 0, NULL, user_write_data, user_flush_data); png_image_write_to_stdio(&image, NULL, 0, ppDataIn, 0, NULL, user_write_data, user_flush_data);
*outputLength = (int)(dataCurr - dataStart); *outputLength = static_cast<int>(dataCurr - dataStart);
return S_OK; return S_OK;
} }
+23 -45
View File
@@ -1,59 +1,47 @@
#pragma once #include "stdafx.h"
#include "Renderer.h" #include "Renderer.h"
#include <cstdint>
#include <cstdio>
#include <cstring>
D3D11_PRIMITIVE_TOPOLOGY *Renderer::m_Topologies = nullptr;
void Renderer::DrawVertexBuffer(C4JRender::ePrimitiveType PrimitiveType, int count, ID3D11Buffer *buffer, C4JRender::eVertexType vType, void Renderer::DrawVertexBuffer(C4JRender::ePrimitiveType PrimitiveType, int count, ID3D11Buffer *buffer, C4JRender::eVertexType vType,
C4JRender::ePixelShaderType psType) C4JRender::ePixelShaderType psType)
{ {
Renderer::Context &c = this->getContext(); Renderer::Context &c = getContext();
ID3D11DeviceContext *d3d11 = c.m_pDeviceContext; ID3D11DeviceContext *d3d11 = c.m_pDeviceContext;
int drawCount = count; int drawCount = count;
bool indexed = false; bool indexed = false;
this->DrawVertexSetup(vType, psType, PrimitiveType, &drawCount, &indexed); DrawVertexSetup(vType, psType, PrimitiveType, &drawCount, &indexed);
this->StateUpdate(); StateUpdate();
const UINT stride = vertexStrideTable[vType]; const UINT stride = vertexStrideTable[vType];
const UINT offset = 0; const UINT offset = 0;
d3d11->IASetVertexBuffers(0, 1, &buffer, &stride, &offset); d3d11->IASetVertexBuffers(0, 1, &buffer, &stride, &offset);
if (indexed) if (indexed)
{
d3d11->DrawIndexed(drawCount, 0, 0); d3d11->DrawIndexed(drawCount, 0, 0);
}
else else
{
d3d11->Draw(count, 0); d3d11->Draw(count, 0);
}
} }
void Renderer::DrawVertexSetup(C4JRender::eVertexType vType, C4JRender::ePixelShaderType psType, C4JRender::ePrimitiveType PrimitiveType, int *count, void Renderer::DrawVertexSetup(C4JRender::eVertexType vType, C4JRender::ePixelShaderType psType, C4JRender::ePrimitiveType PrimitiveType, int *count,
bool *indexed) bool *indexed)
{ {
Renderer::Context &c = this->getContext(); Renderer::Context &c = getContext();
ID3D11DeviceContext *d3d11 = c.m_pDeviceContext; ID3D11DeviceContext *d3d11 = c.m_pDeviceContext;
C4JRender::eVertexType effectiveVertexType = vType; C4JRender::eVertexType effectiveVertexType = vType;
if (effectiveVertexType == C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1 && c.lightingEnabled) if (effectiveVertexType == C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1 && c.lightingEnabled)
{
effectiveVertexType = C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1_LIT; effectiveVertexType = C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1_LIT;
}
if (effectiveVertexType != activeVertexType) if (effectiveVertexType != activeVertexType)
{ {
d3d11->VSSetShader(vertexShaderTable[effectiveVertexType], nullptr, 0); d3d11->VSSetShader(vertexShaderTable[effectiveVertexType], NULL, 0);
d3d11->IASetInputLayout(inputLayoutTable[effectiveVertexType]); d3d11->IASetInputLayout(inputLayoutTable[effectiveVertexType]);
activeVertexType = effectiveVertexType; activeVertexType = effectiveVertexType;
} }
if (psType != activePixelType) if (psType != activePixelType)
{ {
d3d11->PSSetShader(pixelShaderTable[psType], nullptr, 0); d3d11->PSSetShader(pixelShaderTable[psType], NULL, 0);
activePixelType = psType; activePixelType = psType;
} }
@@ -62,7 +50,7 @@ void Renderer::DrawVertexSetup(C4JRender::eVertexType vType, C4JRender::ePixelSh
if (c.matrixDirty[MATRIX_MODE_MODELVIEW]) if (c.matrixDirty[MATRIX_MODE_MODELVIEW])
{ {
d3d11->Map(c.m_modelViewMatrix, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped); d3d11->Map(c.m_modelViewMatrix, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped);
std::memcpy(mapped.pData, this->MatrixGet(MATRIX_MODE_MODELVIEW), sizeof(DirectX::XMMATRIX)); memcpy(mapped.pData, MatrixGet(MATRIX_MODE_MODELVIEW), sizeof(DirectX::XMMATRIX));
d3d11->Unmap(c.m_modelViewMatrix, 0); d3d11->Unmap(c.m_modelViewMatrix, 0);
c.matrixDirty[MATRIX_MODE_MODELVIEW] = false; c.matrixDirty[MATRIX_MODE_MODELVIEW] = false;
} }
@@ -70,7 +58,7 @@ void Renderer::DrawVertexSetup(C4JRender::eVertexType vType, C4JRender::ePixelSh
if (c.matrixDirty[MATRIX_MODE_MODELVIEW_PROJECTION]) if (c.matrixDirty[MATRIX_MODE_MODELVIEW_PROJECTION])
{ {
d3d11->Map(c.m_projectionMatrix, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped); d3d11->Map(c.m_projectionMatrix, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped);
std::memcpy(mapped.pData, this->MatrixGet(MATRIX_MODE_MODELVIEW_PROJECTION), sizeof(DirectX::XMMATRIX)); memcpy(mapped.pData, MatrixGet(MATRIX_MODE_MODELVIEW_PROJECTION), sizeof(DirectX::XMMATRIX));
d3d11->Unmap(c.m_projectionMatrix, 0); d3d11->Unmap(c.m_projectionMatrix, 0);
c.matrixDirty[MATRIX_MODE_MODELVIEW_PROJECTION] = false; c.matrixDirty[MATRIX_MODE_MODELVIEW_PROJECTION] = false;
} }
@@ -78,17 +66,17 @@ void Renderer::DrawVertexSetup(C4JRender::eVertexType vType, C4JRender::ePixelSh
if (c.matrixDirty[MATRIX_MODE_MODELVIEW_TEXTURE]) if (c.matrixDirty[MATRIX_MODE_MODELVIEW_TEXTURE])
{ {
d3d11->Map(c.m_textureMatrix, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped); d3d11->Map(c.m_textureMatrix, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped);
std::memcpy(mapped.pData, this->MatrixGet(MATRIX_MODE_MODELVIEW_TEXTURE), sizeof(DirectX::XMMATRIX)); memcpy(mapped.pData, MatrixGet(MATRIX_MODE_MODELVIEW_TEXTURE), sizeof(DirectX::XMMATRIX));
d3d11->Unmap(c.m_textureMatrix, 0); d3d11->Unmap(c.m_textureMatrix, 0);
c.matrixDirty[MATRIX_MODE_MODELVIEW_TEXTURE] = false; c.matrixDirty[MATRIX_MODE_MODELVIEW_TEXTURE] = false;
} }
this->UpdateFogState(); UpdateFogState();
this->UpdateViewportState(); UpdateViewportState();
this->UpdateLightingState(); UpdateLightingState();
this->UpdateTexGenState(); UpdateTexGenState();
d3d11->IASetPrimitiveTopology(m_Topologies[PrimitiveType]); d3d11->IASetPrimitiveTopology(g_topologies[PrimitiveType]);
if (PrimitiveType == C4JRender::PRIMITIVE_TYPE_QUAD_LIST) if (PrimitiveType == C4JRender::PRIMITIVE_TYPE_QUAD_LIST)
{ {
@@ -106,24 +94,22 @@ void Renderer::DrawVertexSetup(C4JRender::eVertexType vType, C4JRender::ePixelSh
return; return;
} }
d3d11->IASetIndexBuffer(nullptr, DXGI_FORMAT_R16_UINT, 0); d3d11->IASetIndexBuffer(NULL, DXGI_FORMAT_R16_UINT, 0);
*indexed = false; *indexed = false;
} }
void Renderer::DrawVertices(C4JRender::ePrimitiveType PrimitiveType, int count, void *vertices, C4JRender::eVertexType vType, void Renderer::DrawVertices(C4JRender::ePrimitiveType PrimitiveType, int count, void *vertices, C4JRender::eVertexType vType,
C4JRender::ePixelShaderType psType) C4JRender::ePixelShaderType psType)
{ {
Renderer::Context &c = this->getContext(); Renderer::Context &c = getContext();
ID3D11DeviceContext *d3d11 = c.m_pDeviceContext; ID3D11DeviceContext *d3d11 = c.m_pDeviceContext;
Renderer::CommandBuffer *commandBuffer = c.commandBuffer; Renderer::CommandBuffer *commandBuffer = c.commandBuffer;
if (commandBuffer != nullptr) if (commandBuffer != NULL)
{ {
C4JRender::eVertexType effectiveVertexType = vType; C4JRender::eVertexType effectiveVertexType = vType;
if (effectiveVertexType == C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1 && c.lightingEnabled) if (effectiveVertexType == C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1 && c.lightingEnabled)
{
effectiveVertexType = C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1_LIT; effectiveVertexType = C4JRender::VERTEX_TYPE_PF3_TF2_CB4_NB4_XW1_LIT;
}
c.recordingPrimitiveType = PrimitiveType; c.recordingPrimitiveType = PrimitiveType;
c.recordingVertexType = effectiveVertexType; c.recordingVertexType = effectiveVertexType;
@@ -134,7 +120,7 @@ void Renderer::DrawVertices(C4JRender::ePrimitiveType PrimitiveType, int count,
int drawCount = count; int drawCount = count;
bool indexed = false; bool indexed = false;
this->DrawVertexSetup(vType, psType, PrimitiveType, &drawCount, &indexed); DrawVertexSetup(vType, psType, PrimitiveType, &drawCount, &indexed);
const UINT stride = vertexStrideTable[vType]; const UINT stride = vertexStrideTable[vType];
const UINT vertexBytes = stride * static_cast<UINT>(count); const UINT vertexBytes = stride * static_cast<UINT>(count);
@@ -142,35 +128,27 @@ void Renderer::DrawVertices(C4JRender::ePrimitiveType PrimitiveType, int count,
assert(vertexBytes <= Context::VERTEX_BUFFER_SIZE); assert(vertexBytes <= Context::VERTEX_BUFFER_SIZE);
if (c.dynamicVertexOffset + vertexBytes > Context::VERTEX_BUFFER_SIZE) if (c.dynamicVertexOffset + vertexBytes > Context::VERTEX_BUFFER_SIZE)
{
c.dynamicVertexOffset = 0; c.dynamicVertexOffset = 0;
}
D3D11_MAPPED_SUBRESOURCE mapped = {}; D3D11_MAPPED_SUBRESOURCE mapped = {};
const D3D11_MAP mapType = c.dynamicVertexOffset == 0 ? D3D11_MAP_WRITE_DISCARD : D3D11_MAP_WRITE_NO_OVERWRITE; const D3D11_MAP mapType = c.dynamicVertexOffset == 0 ? D3D11_MAP_WRITE_DISCARD : D3D11_MAP_WRITE_NO_OVERWRITE;
const HRESULT hr = d3d11->Map(c.dynamicVertexBuffer, 0, mapType, 0, &mapped); const HRESULT hr = d3d11->Map(c.dynamicVertexBuffer, 0, mapType, 0, &mapped);
if (hr != 0) if (FAILED(hr))
{ printf("ERROR: 0x%x\n", static_cast<unsigned int>(hr));
std::printf("ERROR: 0x%x\n", static_cast<unsigned int>(hr));
}
std::memcpy(static_cast<std::uint8_t *>(mapped.pData) + c.dynamicVertexOffset, vertices, vertexBytes); memcpy(reinterpret_cast<std::uint8_t *>(mapped.pData) + c.dynamicVertexOffset, vertices, vertexBytes);
d3d11->Unmap(c.dynamicVertexBuffer, 0); d3d11->Unmap(c.dynamicVertexBuffer, 0);
this->StateUpdate(); StateUpdate();
ID3D11Buffer *dynamicBuffer = c.dynamicVertexBuffer; ID3D11Buffer *dynamicBuffer = c.dynamicVertexBuffer;
const UINT vertexOffset = c.dynamicVertexOffset; const UINT vertexOffset = c.dynamicVertexOffset;
d3d11->IASetVertexBuffers(0, 1, &dynamicBuffer, &stride, &vertexOffset); d3d11->IASetVertexBuffers(0, 1, &dynamicBuffer, &stride, &vertexOffset);
if (indexed) if (indexed)
{
d3d11->DrawIndexed(drawCount, 0, 0); d3d11->DrawIndexed(drawCount, 0, 0);
}
else else
{
d3d11->Draw(count, 0); d3d11->Draw(count, 0);
}
c.dynamicVertexOffset += vertexBytes; c.dynamicVertexOffset += vertexBytes;
} }