Cogs.Core
ClipmapDebug.cpp
1#include "ClipmapDebug.h"
2
3#include "ClipmapTerrainTypes.h"
4#include "Effects.h"
5
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"
11
12#include <cassert>
13
15 glm::mat4 projectionMatrix;
16 glm::vec4 diffuseColor;
17};
18
19void Cogs::ClipmapDebug::displayClipmapTexture(IGraphicsDevice * device, TextureHandle texture, int x, int y, int width, int height, float multiplier)
20{
23 static BufferHandle constantBuffer = BufferHandle::NoHandle;
24 static VertexBufferHandle quadBuffer = VertexBufferHandle::NoHandle;
25 static SamplerStateHandle samplerStateHandle = SamplerStateHandle::NoHandle;
26 static DepthStencilStateHandle depthStateHandle = DepthStencilStateHandle::NoHandle;
27
28 if(!HandleIsValid(constantBuffer)){
29 constantBuffer = device->getBuffers()->loadBuffer(nullptr, sizeof(DebugGlobals), Usage::Dynamic, AccessMode::Write, BindFlags::ConstantBuffer);
30 }
31
32 if (!HandleIsValid(quadBuffer)) {
33 auto effects = device->getEffects();
34 auto buffers = device->getBuffers();
35 effectHandle = Terrain::EffectLoader::loadEffect(effects, "ClipmapDebugVS", "ClipmapDebugPS");
36 assert(effects->checkEffect(effectHandle) == Cogs::ResourceStatus::Ready && "Expects synchronous effect loading");
37
38 VertexElement elements[] = {
39 { 0, DataFormat::X32Y32Z32_FLOAT, ElementSemantic::Position, 0, InputType::VertexData, 0 },
40 { 3 * sizeof(float), DataFormat::X32Y32_FLOAT, ElementSemantic::TextureCoordinate, 0, InputType::VertexData, 0 }
41 };
42 VertexFormatHandle vertexFormat = buffers->createVertexFormat(elements, 2);
43 inputLayoutHandle = buffers->loadInputLayout(&vertexFormat, 1, effectHandle);
44
45 const float z = 0.0f;
46
47 const float positions[] = {
48 -1, -1, z, 0, 1,
49 1, -1, z, 1, 1,
50 1, 1, z, 1, 0,
51
52 -1, -1, z, 0, 1,
53 1, 1, z, 1, 0,
54 -1, 1, z, 0, 0,
55 };
56 quadBuffer = device->getBuffers()->loadVertexBuffer(positions, 6, vertexFormat);
57
58 SamplerState sampler = {
59 SamplerState::AddressMode::Wrap,
60 SamplerState::AddressMode::Wrap,
61 SamplerState::AddressMode::Wrap,
62 SamplerState::FilterMode::MinMagMipPoint,
63 SamplerState::ComparisonFunction::Never,
64 0,
65 { 0.0f, 0.0f, 0.0f, 0.0f },
66 };
67 samplerStateHandle = device->getTextures()->loadSamplerState(sampler);
68
69 DepthStencilState depthState = { true, true, DepthStencilState::Always };
70 depthStateHandle = device->getRenderTargets()->loadDepthStencilState(depthState);
71 }
72
73 if (HandleIsValid(texture)) {
74 auto context = device->getImmediateContext();
75
76 context->setRasterizerState(RasterizerStateHandle::NoHandle);
77 context->setBlendState(BlendStateHandle::NoHandle);
78 context->setDepthStencilState(depthStateHandle);
79
80 context->setViewport(static_cast<float>(x), static_cast<float>(y), static_cast<float>(width), static_cast<float>(height));
81
82 const uint32_t strides[] = { 5 * sizeof(float) };
83 context->setVertexBuffers(&quadBuffer, 1, strides, nullptr);
84
85 context->setEffect(effectHandle);
86 context->setInputLayout(inputLayoutHandle);
87
88 const auto ortho = glm::ortho<float>(-1, 1, -1, 1, -1, 1);
89 const Vector4 diffuse{ multiplier, multiplier, multiplier, 1 };
90
91 {
92 MappedBuffer<DebugGlobals> constants(context, constantBuffer, MapMode::WriteDiscard);
93 if (constants) {
94 constants->projectionMatrix = ortho;
95 constants->diffuseColor = diffuse;
96 }
97 }
98 context->setConstantBuffer("Globals", constantBuffer);
99
100 context->setTexture("diffuseTexture", 0, texture);
101 context->setSamplerState("diffuseSampler", 0, samplerStateHandle);
102
103 context->draw(PrimitiveType::TriangleList, 0, 6);
104
105 context->setDepthStencilState(DepthStencilStateHandle::NoHandle);
106 }
107}
bool HandleIsValid(const ResourceHandle_t< T > &handle)
Check if the given resource is valid, that is not equal to NoHandle or InvalidHandle.
@ Ready
The resource has loaded successfully and is ready for use.
static const Handle_t InvalidHandle
Represents an invalid handle.
Definition: Common.h:81