feat(rendering): draw and pick immutable voxel height slices through D3D11

This commit is contained in:
faumaray committed 2026-12-03 00:05:11 +03:00
1 parent 13dc673088
commit 37a08d4d48
8 files changed
+1049 -11

No files matched your search

+78 -7
View File
@@ -11,6 +11,8 @@
#include <cstring>
#include <iterator>
#include <limits>
#include <new>
#include <stdexcept>
#include <utility>
namespace settlement::viewer {
@@ -468,6 +470,8 @@ cbuffer Camera : register(b0) {
float2 screen;
float scale;
float padding;
float2 depthParameters;
float2 depthPadding;
};
struct Input { float3 position : POSITION; float3 color : COLOR; };
struct Pixel { float4 position : SV_Position; float3 color : COLOR; };
@@ -477,7 +481,8 @@ Pixel project(Input input) {
(p.x + p.y) * halfTile.y - p.z * 16.0);
Pixel result;
result.position = float4(screenPoint.x * 2.0 / screen.x - 1.0,
1.0 - screenPoint.y * 2.0 / screen.y, 0.96 - (p.x + p.y + p.z * 1.5) * 0.009, 1.0);
1.0 - screenPoint.y * 2.0 / screen.y,
depthParameters.x - (p.x + p.y + p.z * 1.5) * depthParameters.y, 1.0);
result.color = input.color;
return result;
}
@@ -489,6 +494,8 @@ float4 paint(Pixel input) : SV_Target { return float4(input.color, 1.0); }
class PreparedGeometry {
public:
Mesh mesh;
std::shared_ptr<const VoxelScene> voxel_scene;
float depth_bias = .96f, depth_scale = .009f;
};
struct D3DScene::Backend {
@@ -539,21 +546,28 @@ struct D3DScene::Backend {
}
bool upload_geometry(const Mesh& mesh) {
if (mesh.vertices.empty()) return true;
const auto bytes = mesh.vertices.size() * sizeof(Vertex);
if (bytes > std::numeric_limits<UINT>::max()) return false;
ComPtr<ID3D11Buffer> replacement;
auto* destination = geometry.Get();
if (bytes > buffer_bytes) {
D3D11_BUFFER_DESC desc{};
desc.ByteWidth = static_cast<UINT>(bytes);
desc.Usage = D3D11_USAGE_DYNAMIC;
desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
if (FAILED(device->CreateBuffer(&desc, nullptr, geometry.ReleaseAndGetAddressOf()))) return false;
buffer_bytes = desc.ByteWidth;
if (FAILED(device->CreateBuffer(&desc, nullptr, &replacement))) return false;
destination = replacement.Get();
}
D3D11_MAPPED_SUBRESOURCE mapped{};
if (FAILED(context->Map(geometry.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped))) return false;
if (FAILED(context->Map(destination, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped))) return false;
std::memcpy(mapped.pData, mesh.vertices.data(), bytes);
context->Unmap(geometry.Get(), 0);
context->Unmap(destination, 0);
if (replacement) {
geometry = std::move(replacement);
buffer_bytes = static_cast<UINT>(bytes);
}
return true;
}
@@ -621,7 +635,7 @@ bool D3DScene::initialize(HWND target, bool software) {
raster.CullMode = D3D11_CULL_NONE;
raster.DepthClipEnable = TRUE;
D3D11_BUFFER_DESC constants{};
constants.ByteWidth = 32;
constants.ByteWidth = 48;
constants.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
if (FAILED(graphics->device->CreateInputLayout(elements, std::size(elements), vertex->GetBufferPointer(),
vertex->GetBufferSize(), &graphics->layout)) ||
@@ -656,6 +670,62 @@ bool D3DScene::render(const World& world, const SceneView& view, std::vector<std
return render(prepare(world, view), view, capture);
}
std::expected<std::shared_ptr<const PreparedGeometry>, VoxelSceneError>
D3DScene::prepare_voxels(std::shared_ptr<const VoxelScene> scene) {
if (!scene) return std::unexpected(VoxelSceneError::InvalidInput);
if (scene->faces().size() > std::numeric_limits<UINT>::max() / (6 * sizeof(Vertex)))
return std::unexpected(VoxelSceneError::LimitExceeded);
try {
auto prepared = std::make_shared<PreparedGeometry>();
prepared->voxel_scene = std::move(scene);
const auto bounds = prepared->voxel_scene->visible_bounds();
const double depth_extent = static_cast<double>(bounds.upper.x - bounds.lower.x) +
static_cast<double>(bounds.upper.y - bounds.lower.y) +
static_cast<double>(bounds.upper.z - bounds.lower.z) * 1.5;
prepared->depth_bias = .95f;
prepared->depth_scale = static_cast<float>(.9 / depth_extent);
auto& mesh = prepared->mesh;
mesh.vertices.reserve(prepared->voxel_scene->faces().size() * 6);
for (const auto& face : prepared->voxel_scene->faces()) {
const float x = static_cast<float>(std::int64_t{face.cell.x} - bounds.lower.x);
const float y = static_cast<float>(std::int64_t{face.cell.y} - bounds.lower.y);
const float z = static_cast<float>(std::int64_t{face.cell.z} - bounds.lower.z);
const auto packed = voxel_face_color(face);
const auto color = rgb(static_cast<int>((packed >> 16u) & 255u),
static_cast<int>((packed >> 8u) & 255u), static_cast<int>(packed & 255u));
if (face.normal.x != 0) {
const float a = x + (face.normal.x > 0 ? 1.f : 0.f);
mesh.quad({a, y, z}, {a, y + 1, z}, {a, y + 1, z + 1}, {a, y, z + 1}, color);
} else if (face.normal.y != 0) {
const float a = y + (face.normal.y > 0 ? 1.f : 0.f);
mesh.quad({x, a, z}, {x + 1, a, z}, {x + 1, a, z + 1}, {x, a, z + 1}, color);
} else {
const float a = z + (face.normal.z > 0 ? 1.f : 0.f);
mesh.quad({x, y, a}, {x + 1, y, a}, {x + 1, y + 1, a}, {x, y + 1, a}, color);
}
}
return prepared;
} catch (const std::bad_alloc&) {
return std::unexpected(VoxelSceneError::AllocationFailed);
} catch (const std::length_error&) {
return std::unexpected(VoxelSceneError::AllocationFailed);
}
}
bool D3DScene::render(std::shared_ptr<const PreparedGeometry> geometry, const VoxelCamera& camera,
std::vector<std::uint32_t>* capture) {
if (!geometry || !geometry->voxel_scene || !valid_voxel_camera(camera)) return false;
SceneView view{};
view.width = camera.width;
view.height = camera.height;
view.origin_x = static_cast<float>(camera.origin_x);
view.origin_y = static_cast<float>(camera.origin_y);
view.scale = static_cast<float>(camera.scale);
view.tile_half_width = static_cast<float>(camera.tile_half_width);
view.tile_half_height = static_cast<float>(camera.tile_half_height);
return render(std::move(geometry), view, capture);
}
bool D3DScene::render(std::shared_ptr<const PreparedGeometry> geometry, const SceneView& view,
std::vector<std::uint32_t>* capture) {
// Present may dispatch window messages, including destruction. Keep this
@@ -679,7 +749,8 @@ bool D3DScene::render(std::shared_ptr<const PreparedGeometry> geometry, const Sc
ctx->RSSetViewports(1, &viewport);
ctx->RSSetState(graphics.rasterizer.Get());
const float constants[] = {view.origin_x, view.origin_y, view.tile_half_width, view.tile_half_height,
viewport.Width, viewport.Height, view.scale, 0};
viewport.Width, viewport.Height, view.scale, 0,
geometry->depth_bias, geometry->depth_scale, 0, 0};
ctx->UpdateSubresource(graphics.camera.Get(), 0, nullptr, constants, 0, 0);
constexpr UINT stride = sizeof(Vertex);
constexpr UINT offset = 0;