-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgraphics_vk_depth.cpp
More file actions
85 lines (74 loc) · 2.27 KB
/
Copy pathgraphics_vk_depth.cpp
File metadata and controls
85 lines (74 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "subsystems/graphics/graphics.h"
#include "subsystems/graphics/graphics_vk_internal.h"
#include "drivers/video/display_info.h"
#include "log/klog.h"
#include "mm/kheap.h"
/*
* DuetOS — Vulkan ICD software depth surface.
*
* Single shared 16-bit depth buffer sized to the live framebuffer
* extent. The v0 ICD has one scanout target, so one depth buffer
* is enough; a follow-on slice that exposes multiple swapchains
* or off-screen render targets will need to key this by image
* handle and let `VkDestroyImage` drop its entry.
*
* Lazy allocation: the buffer is only allocated when the first
* Z-test draw asks for it. Boot ICD init costs nothing. Boot
* self-test teardown frees the surface so the leak walk stays
* clean.
*
* Cleared to `0xFFFF` (far) on alloc so the first draw passes
* the Less compare unconditionally.
*/
namespace duetos::subsystems::graphics::internal
{
namespace
{
DepthSurface g_depth{};
bool g_alloc_failed_once = false;
void FillU16(u16* data, u64 count, u16 value)
{
for (u64 i = 0; i < count; ++i)
data[i] = value;
}
} // namespace
DepthSurface* DepthSurfaceGetOrAlloc()
{
if (g_depth.data != nullptr)
return &g_depth;
if (g_alloc_failed_once)
return nullptr;
const auto di = drivers::video::Query();
if (!di.available || di.width == 0 || di.height == 0)
return nullptr;
const u64 pixel_count = static_cast<u64>(di.width) * di.height;
const u64 bytes = pixel_count * sizeof(u16);
void* mem = mm::KMalloc(bytes);
if (mem == nullptr)
{
g_alloc_failed_once = true;
KLOG_WARN_V("subsystems/graphics", "depth surface alloc failed; depth test will be skipped", bytes);
return nullptr;
}
g_depth.data = static_cast<u16*>(mem);
g_depth.w = di.width;
g_depth.h = di.height;
FillU16(g_depth.data, pixel_count, 0xFFFFu);
return &g_depth;
}
void DepthSurfaceClear(u16 value)
{
if (g_depth.data == nullptr)
return;
FillU16(g_depth.data, static_cast<u64>(g_depth.w) * g_depth.h, value);
}
void DepthSurfaceFree()
{
if (g_depth.data != nullptr)
mm::KFree(g_depth.data);
g_depth.data = nullptr;
g_depth.w = 0;
g_depth.h = 0;
g_alloc_failed_once = false;
}
} // namespace duetos::subsystems::graphics::internal