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