1#include "ClipmapDebug.h"
3#include "ClipmapTerrainTypes.h"
6#include "Rendering/IGraphicsDevice.h"
7#include "Rendering/IBuffers.h"
8#include "Rendering/ITextures.h"
9#include "Rendering/IContext.h"
10#include "Rendering/IRenderTargets.h"
13 glm::mat4 projectionMatrix;
14 glm::vec4 diffuseColor;
17void Cogs::ClipmapDebug::displayClipmapTexture(IGraphicsDevice * device, TextureHandle texture,
int x,
int y,
int width,
int height,
float multiplier)
21 static BufferHandle constantBuffer = BufferHandle::NoHandle;
22 static VertexBufferHandle quadBuffer = VertexBufferHandle::NoHandle;
23 static SamplerStateHandle samplerStateHandle = SamplerStateHandle::NoHandle;
24 static DepthStencilStateHandle depthStateHandle = DepthStencilStateHandle::NoHandle;
26 if(!HandleIsValid(constantBuffer)){
27 constantBuffer = device->getBuffers()->loadBuffer(
nullptr,
sizeof(
DebugGlobals), Usage::Dynamic, AccessMode::Write, BindFlags::ConstantBuffer);
31 auto effects = device->getEffects();
32 auto buffers = device->getBuffers();
33 effectHandle = Terrain::EffectLoader::loadEffect(effects,
"ClipmapDebugVS",
"ClipmapDebugPS");
35 VertexElement elements[] = {
36 { 0, DataFormat::X32Y32Z32_FLOAT, ElementSemantic::Position, 0, InputType::VertexData, 0 },
37 { 3 *
sizeof(float), DataFormat::X32Y32_FLOAT, ElementSemantic::TextureCoordinate, 0, InputType::VertexData, 0 }
39 VertexFormatHandle vertexFormat = buffers->createVertexFormat(elements, 2);
40 inputLayoutHandle = buffers->loadInputLayout(&vertexFormat, 1, effectHandle);
44 const float positions[] = {
53 quadBuffer = device->getBuffers()->loadVertexBuffer(positions, 6, vertexFormat);
55 SamplerState sampler = {
56 SamplerState::AddressMode::Wrap,
57 SamplerState::AddressMode::Wrap,
58 SamplerState::AddressMode::Wrap,
59 SamplerState::FilterMode::MinMagMipPoint,
60 SamplerState::ComparisonFunction::Never,
62 { 0.0f, 0.0f, 0.0f, 0.0f },
64 samplerStateHandle = device->getTextures()->loadSamplerState(sampler);
66 DepthStencilState depthState = {
true,
true, DepthStencilState::Always };
67 depthStateHandle = device->getRenderTargets()->loadDepthStencilState(depthState);
71 auto context = device->getImmediateContext();
73 context->setRasterizerState(RasterizerStateHandle::NoHandle);
74 context->setBlendState(BlendStateHandle::NoHandle);
75 context->setDepthStencilState(depthStateHandle);
77 context->setViewport(
static_cast<float>(x),
static_cast<float>(y),
static_cast<float>(width),
static_cast<float>(height));
79 const uint32_t strides[] = { 5 *
sizeof(float) };
80 context->setVertexBuffers(&quadBuffer, 1, strides,
nullptr);
82 context->setEffect(effectHandle);
83 context->setInputLayout(inputLayoutHandle);
85 const auto ortho = glm::ortho<float>(-1, 1, -1, 1, -1, 1);
86 const Vector4 diffuse{ multiplier, multiplier, multiplier, 1 };
89 MappedBuffer<DebugGlobals> constants(context, constantBuffer, MapMode::WriteDiscard);
91 constants->projectionMatrix = ortho;
92 constants->diffuseColor = diffuse;
95 context->setConstantBuffer(
"Globals", constantBuffer);
97 context->setTexture(
"diffuseTexture", 0, texture);
98 context->setSamplerState(
"diffuseSampler", 0, samplerStateHandle);
100 context->draw(PrimitiveType::TriangleList, 0, 6);
102 context->setDepthStencilState(DepthStencilStateHandle::NoHandle);
bool HandleIsValid(const ResourceHandle_t< T > &handle)
Check if the given resource is valid, that is not equal to NoHandle or InvalidHandle.
static const Handle_t InvalidHandle
Represents an invalid handle.