Skip to content

Commit

Permalink
dx hdr
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxwellGengYF committed Dec 21, 2024
1 parent 9ff40a6 commit 30e1bd0
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 38 deletions.
12 changes: 10 additions & 2 deletions src/backends/dx/DXApi/LCSwapChain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ LCSwapChain::LCSwapChain(
ThrowIfFailed(device->dxgiFactory->CreateSwapChainForHwnd(
queue->Queue(),
windowHandle,

&swapChainDesc,
nullptr,
nullptr,
Expand All @@ -46,8 +47,15 @@ LCSwapChain::LCSwapChain(
for (uint32_t n = 0; n < frameCount; n++) {
ThrowIfFailed(swapChain->GetBuffer(n, IID_PPV_ARGS(&m_renderTargets[n].rt)));
}
// if (!vsync)
// swapChain->SetMaximumFrameLatency(backBufferCount * 2);
if (!vsync) {
ComPtr<IDXGISwapChain3> swapChain3;
auto hr = swapChain->QueryInterface(IID_PPV_ARGS(&swapChain3));
if (hr == S_OK) {
swapChain3->SetMaximumFrameLatency(backBufferCount * 2);
} else {
LUISA_WARNING("Can not get IDXGISwapChain3, please check your Direct-X runtime.");
}
}
}
LCSwapChain::LCSwapChain(
PixelStorage &storage,
Expand Down
32 changes: 17 additions & 15 deletions src/backends/dx/DXApi/dx_hdr_ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,20 +155,22 @@ auto DXHDRExtImpl::set_hdr_meta_data(
float max_cll,
float max_fall,
const DXHDRExt::DisplayChromaticities *custom_chroma) noexcept -> Meta {
// DXGI_COLOR_SPACE_TYPE color_space = DXGI_COLOR_SPACE_CUSTOM;
// auto chroma = dx_hdr_ext_detail::SetHDRMetaData(
// color_space,
// reinterpret_cast<LCSwapChain *>(swapchain_handle),
// true,
// max_output_nits,
// min_output_nits,
// max_cll,
// max_fall,
// custom_chroma);
// return {
// static_cast<ColorSpace>(color_space),
// chroma};
LUISA_ERROR("Not implemented.");
return {};
DXGI_COLOR_SPACE_TYPE color_space = DXGI_COLOR_SPACE_CUSTOM;
auto swapChain = reinterpret_cast<LCSwapChain *>(swapchain_handle);
ComPtr<IDXGISwapChain4> swapChain4;
ThrowIfFailed(swapChain->swapChain->QueryInterface(IID_PPV_ARGS(&swapChain4)));
auto chroma = dx_hdr_ext_detail::SetHDRMetaData(
color_space,
swapChain4.Get(),
swapChain->format,
true,
max_output_nits,
min_output_nits,
max_cll,
max_fall,
custom_chroma);
return {
static_cast<ColorSpace>(color_space),
chroma};
}
}// namespace lc::dx
13 changes: 13 additions & 0 deletions src/backends/validation/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "raster_ext_impl.h"
#include "dstorage_ext_impl.h"
#include "pinned_mem_impl.h"
#include "dx_hdr_ext_impl.h"
#include "native_res_ext_impl.h"
#include <luisa/core/logging.h>
#include <luisa/runtime/rhi/command.h>
Expand All @@ -33,6 +34,18 @@ Device::Device(Context &&ctx, luisa::shared_ptr<DeviceInterface> &&native) noexc
auto dstorage_ext = static_cast<DStorageExt *>(_native->extension(DStorageExt::name));
auto pinned_ext = static_cast<PinnedMemoryExt *>(_native->extension(PinnedMemoryExt::name));
auto native_res_ext = static_cast<NativeResourceExt *>(_native->extension(NativeResourceExt::name));
auto dx_hdr_ext = static_cast<DXHDRExt *>(_native->extension(DXHDRExt::name));
if (dx_hdr_ext) {
auto impl = new DXHDRExtImpl(dx_hdr_ext);
exts.try_emplace(
DXHDRExt::name,
ExtPtr{
impl,
detail::ext_deleter<DeviceExtension>{
[](DeviceExtension *ptr) {
delete static_cast<DXHDRExtImpl *>(ptr);
}}});
}
if (raster_ext) {
auto raster_impl = new RasterExtImpl(raster_ext);
exts.try_emplace(
Expand Down
27 changes: 27 additions & 0 deletions src/backends/validation/dx_hdr_ext_impl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "dx_hdr_ext_impl.h"
#include "swap_chain.h"
namespace lc::validation {
SwapchainCreationInfo DXHDRExtImpl::create_swapchain(
const DXSwapchainOption &option,
uint64_t stream_handle) noexcept {
auto sw = ptr->create_swapchain(option, stream_handle);
new SwapChain(sw.handle);
return sw;
}
auto DXHDRExtImpl::set_hdr_meta_data(
uint64_t swapchain_handle,
float max_output_nits,
float min_output_nits,
float max_cll,
float max_fall,
const DisplayChromaticities *custom_chroma) noexcept
-> Meta {
return ptr->set_hdr_meta_data(
swapchain_handle,
max_output_nits,
min_output_nits,
max_cll,
max_fall,
custom_chroma);
}
}// namespace lc::validation
21 changes: 21 additions & 0 deletions src/backends/validation/dx_hdr_ext_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once
#include <luisa/backends/ext/dx_hdr_ext_interface.h>

namespace lc::validation {
using namespace luisa::compute;
class DXHDRExtImpl : public DXHDRExt, public vstd::IOperatorNewBase {
public:
DXHDRExt *ptr;
DXHDRExtImpl(DXHDRExt *ptr) : ptr(ptr) {}
SwapchainCreationInfo create_swapchain(
const DXSwapchainOption &option,
uint64_t stream_handle) noexcept override;
Meta set_hdr_meta_data(
uint64_t swapchain_handle,
float max_output_nits = 1000.0f,
float min_output_nits = 0.001f,
float max_cll = 2000.0f,
float max_fall = 500.0f,
const DisplayChromaticities *custom_chroma = nullptr) noexcept override;
};
}// namespace lc::validation
2 changes: 1 addition & 1 deletion src/ext/marl
Submodule marl updated 1 files
+2 −2 include/marl/future.h
69 changes: 49 additions & 20 deletions src/tests/test_path_tracing.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <iostream>

#include <luisa/backends/ext/dx_hdr_ext.hpp>
#include <stb/stb_image_write.h>

#include <luisa/luisa-compute.h>
Expand Down Expand Up @@ -30,11 +30,7 @@ int main(int argc, char *argv[]) {
log_level_verbose();

Context context{argv[0]};
if (argc <= 1) {
LUISA_INFO("Usage: {} <backend>. <backend>: cuda, dx, cpu, metal", argv[0]);
exit(1);
}
Device device = context.create_device(argv[1]);
Device device = context.create_device("dx");

// load the Cornell Box scene
tinyobj::ObjReaderConfig obj_reader_config;
Expand Down Expand Up @@ -271,16 +267,37 @@ int main(int argc, char *argv[]) {
return clamp((x * (a * x + b)) / (x * (c * x + d) + e), 0.0f, 1.0f);
};

Callable linear_to_st2084 = [](Float3 color) noexcept {
Float m1 = 2610.0 / 4096.0 / 4;
Float m2 = 2523.0 / 4096.0 * 128;
Float c1 = 3424.0 / 4096.0;
Float c2 = 2413.0 / 4096.0 * 32;
Float c3 = 2392.0 / 4096.0 * 32;
Float3 cp = pow(abs(color), m1);
return pow((c1 + c2 * cp) / (1.0f + c3 * cp), m2);
};

Kernel2D clear_kernel = [](ImageFloat image) noexcept {
image.write(dispatch_id().xy(), make_float4(0.0f));
};

Kernel2D hdr2ldr_kernel = [&](ImageFloat hdr_image, ImageFloat ldr_image, Float scale, Bool is_hdr) noexcept {
Kernel2D hdr2ldr_kernel = [&](ImageFloat hdr_image, ImageFloat ldr_image, Float scale, Int mode) noexcept {
UInt2 coord = dispatch_id().xy();
Float4 hdr = hdr_image.read(coord);
Float3 ldr = hdr.xyz() / hdr.w * scale;
$if (!is_hdr) {
ldr = linear_to_srgb(ldr);
$switch (mode) {
// sRGB
$case (0) {
ldr = linear_to_srgb(ldr);
};
// 10-bit
$case (1) {
ldr = linear_to_st2084(ldr * 80.0f / 10000.0f);
};
// 16-bit
$case (2) {
// LINEAR
};
};
ldr_image.write(coord, make_float4(ldr, 1.0f));
};
Expand All @@ -302,40 +319,52 @@ int main(int argc, char *argv[]) {
<< make_sampler_shader(seed_image).dispatch(resolution);

Window window{"path tracing", resolution};
Swapchain swap_chain = device.create_swapchain(
auto dx_hdr_ext = device.extension<DXHDRExt>();
// Swapchain swap_chain = device.create_swapchain(
// stream,
// SwapchainOption{
// .display = window.native_display(),
// .window = window.native_handle(),
// .size = make_uint2(resolution),
// .wants_hdr = false,
// .wants_vsync = false,
// .back_buffer_count = 8,
// });
Swapchain swap_chain = dx_hdr_ext->create_swapchain(
stream,
SwapchainOption{
.display = window.native_display(),
DXHDRExt::DXSwapchainOption{
.window = window.native_handle(),
.size = make_uint2(resolution),
.wants_hdr = false,
.storage = PixelStorage::HALF4,
.wants_vsync = false,
.back_buffer_count = 8,
});
dx_hdr_ext->set_hdr_meta_data(
swap_chain.handle());
Image<float> ldr_image = device.create_image<float>(swap_chain.backend_storage(), resolution);
double last_time = 0.0;
uint frame_count = 0u;
Clock clock;

while (!window.should_close()) {
int mode = 0;
if (swap_chain.backend_storage() == PixelStorage::R10G10B10A2) {
mode = 1;
} else if (swap_chain.backend_storage() == PixelStorage::HALF4) {
mode = 2;
}
cmd_list << raytracing_shader(framebuffer, seed_image, accel, resolution)
.dispatch(resolution)
<< accumulate_shader(accum_image, framebuffer)
.dispatch(resolution);
cmd_list << hdr2ldr_shader(accum_image, ldr_image, 1.0f, swap_chain.backend_storage() != PixelStorage::BYTE4).dispatch(resolution);
cmd_list << hdr2ldr_shader(accum_image, ldr_image, 1.0f, mode).dispatch(resolution);
stream << cmd_list.commit()
<< swap_chain.present(ldr_image) << synchronize();
window.poll_events();
double dt = clock.toc() - last_time;
last_time = clock.toc();
frame_count += spp_per_dispatch;
LUISA_INFO("spp: {}, time: {} ms, spp/s: {}",
frame_count, dt, spp_per_dispatch / dt * 1000);
}
stream
<< ldr_image.copy_to(host_image.data())
<< synchronize();

LUISA_INFO("FPS: {}", frame_count / clock.toc() * 1000);
stbi_write_png("test_path_tracing.png", resolution.x, resolution.y, 4, host_image.data(), 0);
}

0 comments on commit 30e1bd0

Please sign in to comment.