|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define NOMINMAX |
|
|
#include <d3d11.h> |
|
|
|
|
|
#include <math.h> |
|
|
|
|
|
#include "../d3d/shaders/debugLineVS.hlsl.h" |
|
|
#include "../d3d/shaders/debugLinePS.hlsl.h" |
|
|
|
|
|
|
|
|
#include "debugLineRenderD3D11.h" |
|
|
|
|
|
void DebugLineRenderD3D11::init(ID3D11Device* d, ID3D11DeviceContext* c) |
|
|
{ |
|
|
m_device = d; |
|
|
m_context = c; |
|
|
|
|
|
|
|
|
{ |
|
|
D3D11_RASTERIZER_DESC desc = {}; |
|
|
desc.FillMode = D3D11_FILL_SOLID; |
|
|
desc.CullMode = D3D11_CULL_BACK; |
|
|
desc.FrontCounterClockwise = TRUE; |
|
|
desc.DepthBias = 0; |
|
|
desc.DepthBiasClamp = 0.f; |
|
|
desc.SlopeScaledDepthBias = 0.f; |
|
|
desc.DepthClipEnable = TRUE; |
|
|
desc.ScissorEnable = FALSE; |
|
|
desc.MultisampleEnable = TRUE; |
|
|
desc.AntialiasedLineEnable = FALSE; |
|
|
|
|
|
m_device->CreateRasterizerState(&desc, m_rasterizerState.GetAddressOf()); |
|
|
} |
|
|
|
|
|
{ |
|
|
D3D11_DEPTH_STENCIL_DESC depthStateDesc = {}; |
|
|
depthStateDesc.DepthEnable = FALSE; |
|
|
depthStateDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL; |
|
|
depthStateDesc.DepthFunc = D3D11_COMPARISON_LESS_EQUAL; |
|
|
|
|
|
m_device->CreateDepthStencilState(&depthStateDesc, m_depthStencilState.GetAddressOf()); |
|
|
} |
|
|
|
|
|
|
|
|
{ |
|
|
D3D11_INPUT_ELEMENT_DESC inputElementDescs[] = |
|
|
{ |
|
|
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, |
|
|
{ "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }, |
|
|
}; |
|
|
|
|
|
m_device->CreateInputLayout(inputElementDescs, 2, g_debugLineVS, sizeof(g_debugLineVS), m_inputLayout.GetAddressOf()); |
|
|
} |
|
|
|
|
|
|
|
|
{ |
|
|
D3D11_BLEND_DESC blendDesc = {}; |
|
|
|
|
|
blendDesc.AlphaToCoverageEnable = false; |
|
|
blendDesc.IndependentBlendEnable = false; |
|
|
blendDesc.RenderTarget[0].BlendEnable = false; |
|
|
blendDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA; |
|
|
blendDesc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA; |
|
|
blendDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD; |
|
|
blendDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA; |
|
|
blendDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA; |
|
|
blendDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD; |
|
|
blendDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL; |
|
|
|
|
|
m_device->CreateBlendState(&blendDesc, m_blendState.GetAddressOf()); |
|
|
} |
|
|
|
|
|
|
|
|
m_device->CreateVertexShader(g_debugLineVS, sizeof(g_debugLineVS), nullptr, m_vertexShader.GetAddressOf()); |
|
|
m_device->CreatePixelShader(g_debugLinePS, sizeof(g_debugLinePS), nullptr, m_pixelShader.GetAddressOf()); |
|
|
|
|
|
|
|
|
{ |
|
|
D3D11_BUFFER_DESC bufDesc; |
|
|
bufDesc.ByteWidth = sizeof(Matrix44); |
|
|
bufDesc.Usage = D3D11_USAGE_DYNAMIC; |
|
|
bufDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; |
|
|
bufDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; |
|
|
bufDesc.MiscFlags = 0; |
|
|
|
|
|
m_device->CreateBuffer(&bufDesc, NULL, m_constantBuffer.GetAddressOf()); |
|
|
} |
|
|
} |
|
|
|
|
|
void DebugLineRenderD3D11::addLine(const Vec3& p, const Vec3& q, const Vec4& color) |
|
|
{ |
|
|
Vertex v = { p, color }; |
|
|
Vertex w = { q, color }; |
|
|
|
|
|
m_queued.push_back(v); |
|
|
m_queued.push_back(w); |
|
|
} |
|
|
|
|
|
void DebugLineRenderD3D11::flush(const Matrix44& viewProj) |
|
|
{ |
|
|
if (m_queued.empty()) |
|
|
return; |
|
|
|
|
|
|
|
|
if (m_vertexBufferSize < int(m_queued.size())) |
|
|
{ |
|
|
m_vertexBuffer = nullptr; |
|
|
|
|
|
D3D11_BUFFER_DESC bufferDesc; |
|
|
bufferDesc.Usage = D3D11_USAGE_DYNAMIC; |
|
|
bufferDesc.ByteWidth = UINT(sizeof(Vertex)*m_queued.size()); |
|
|
bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; |
|
|
bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; |
|
|
bufferDesc.MiscFlags = 0; |
|
|
|
|
|
m_device->CreateBuffer(&bufferDesc, 0, &m_vertexBuffer); |
|
|
|
|
|
m_vertexBufferSize = int(m_queued.size()); |
|
|
} |
|
|
|
|
|
|
|
|
D3D11_MAPPED_SUBRESOURCE res; |
|
|
m_context->Map(m_vertexBuffer.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &res); |
|
|
memcpy(res.pData, &m_queued[0], sizeof(Vertex)*m_queued.size()); |
|
|
m_context->Unmap(m_vertexBuffer.Get(), 0); |
|
|
|
|
|
|
|
|
{ |
|
|
D3D11_MAPPED_SUBRESOURCE mappedResource = {}; |
|
|
if (SUCCEEDED(m_context->Map(m_constantBuffer.Get(), 0u, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource))) |
|
|
{ |
|
|
memcpy(mappedResource.pData, &viewProj, sizeof(viewProj)); |
|
|
m_context->Unmap(m_constantBuffer.Get(), 0u); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
m_context->VSSetShader(m_vertexShader.Get(), nullptr, 0u); |
|
|
m_context->GSSetShader(nullptr, nullptr, 0u); |
|
|
m_context->PSSetShader(m_pixelShader.Get(), nullptr, 0u); |
|
|
|
|
|
m_context->IASetInputLayout(m_inputLayout.Get()); |
|
|
m_context->OMSetBlendState(m_blendState.Get(), nullptr, 0xFFFFFFFF); |
|
|
|
|
|
m_context->VSSetConstantBuffers(0, 1, m_constantBuffer.GetAddressOf()); |
|
|
|
|
|
m_context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_LINELIST); |
|
|
|
|
|
m_context->RSSetState(m_rasterizerState.Get()); |
|
|
|
|
|
UINT vertexStride = sizeof(Vertex); |
|
|
UINT offset = 0u; |
|
|
m_context->IASetVertexBuffers(0, 1, m_vertexBuffer.GetAddressOf(), &vertexStride, &offset); |
|
|
|
|
|
|
|
|
m_context->Draw(UINT(m_queued.size()), 0); |
|
|
|
|
|
|
|
|
m_queued.resize(0); |
|
|
|
|
|
} |
|
|
|