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

This commit is contained in:
3UR
2026-03-03 00:23:38 +10:00
parent 26f8477e4b
commit 3b406a063d
7 changed files with 556 additions and 575 deletions

View File

@@ -1,39 +1,25 @@
#pragma once
#include "CompiledShaders.h"
#include "stdafx.h"
#include "Renderer.h"
#include <cstdio>
#include <cstring>
#include <limits>
#include <new>
#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<size_t>(width) * static_cast<size_t>(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<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_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<unsigned short>(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<unsigned short>(i + 1);
fanIndices[offset + 2] = static_cast<unsigned short>(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<unsigned char *>(linearData);
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;
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<int *>(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<Renderer::Context *>(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<float>::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<float *>(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<const unsigned char *>(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;
}