Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 53 additions & 33 deletions atlas/application/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,8 @@ Window::Window(const WindowConfiguration &config)
#ifdef METAL
this->externalMetalView = config.metalTargetView;
this->renderToExternalMetalView = this->externalMetalView != nullptr;
this->showHostWindow = config.showHostWindow && !this->renderToExternalMetalView;
this->showHostWindow =
config.showHostWindow && !this->renderToExternalMetalView;
#else
(void)config.metalTargetView;
this->externalMetalView = nullptr;
Expand Down Expand Up @@ -1638,6 +1639,15 @@ bool Window::stepFrame() {
pathTracer->copySrcFramebuffer, pathTracer->copyDstFramebuffer,
0);
commandBuffer->performResolve(copy);

pathTracer->copySrcFramebuffer->attachTexture(
pathTracer->pathTracingTextureBright->texture, 0);
pathTracer->copyDstFramebuffer->attachTexture(
target->brightTexture.texture, 0);
auto brightCopy = opal::ResolveAction::createForColorAttachment(
pathTracer->copySrcFramebuffer, pathTracer->copyDstFramebuffer,
0);
commandBuffer->performResolve(brightCopy);
#endif

continue;
Expand Down Expand Up @@ -1710,7 +1720,8 @@ bool Window::stepFrame() {
if (obj->canUseDeferredRendering()) {
return;
}
if (obj && obj->editorOnly && !this->areEditorControlsEnabled()) return;
if (obj && obj->editorOnly && !this->areEditorControlsEnabled())
return;
obj->setViewMatrix(this->camera->calculateViewMatrix());
obj->setProjectionMatrix(calculateProjectionMatrix());
obj->render(getDeltaTime(), commandBuffer,
Expand All @@ -1729,7 +1740,8 @@ bool Window::stepFrame() {
}

for (auto &obj : this->lateForwardRenderables) {
if (obj && obj->editorOnly && !this->areEditorControlsEnabled()) continue;
if (obj && obj->editorOnly && !this->areEditorControlsEnabled())
continue;
obj->setViewMatrix(this->camera->calculateViewMatrix());
obj->setProjectionMatrix(calculateProjectionMatrix());
obj->render(getDeltaTime(), commandBuffer,
Expand All @@ -1748,7 +1760,8 @@ bool Window::stepFrame() {
renderEditorGrid(commandBuffer);

for (auto &obj : this->firstRenderables) {
if (obj && obj->editorOnly && !this->areEditorControlsEnabled()) continue;
if (obj && obj->editorOnly && !this->areEditorControlsEnabled())
continue;
obj->setViewMatrix(this->camera->calculateViewMatrix());
obj->setProjectionMatrix(calculateProjectionMatrix());
obj->render(getDeltaTime(), commandBuffer,
Expand All @@ -1759,15 +1772,17 @@ bool Window::stepFrame() {
if (obj->renderLateForward) {
continue;
}
if (obj && obj->editorOnly && !this->areEditorControlsEnabled()) continue;
if (obj && obj->editorOnly && !this->areEditorControlsEnabled())
continue;
obj->setViewMatrix(this->camera->calculateViewMatrix());
obj->setProjectionMatrix(calculateProjectionMatrix());
obj->render(getDeltaTime(), commandBuffer,
shouldRefreshPipeline(obj));
}
updateFluidCaptures(commandBuffer);
for (auto &obj : this->lateForwardRenderables) {
if (obj && obj->editorOnly && !this->areEditorControlsEnabled()) continue;
if (obj && obj->editorOnly && !this->areEditorControlsEnabled())
continue;
obj->setViewMatrix(this->camera->calculateViewMatrix());
obj->setProjectionMatrix(calculateProjectionMatrix());
obj->render(getDeltaTime(), commandBuffer,
Expand Down Expand Up @@ -1812,7 +1827,8 @@ bool Window::stepFrame() {
this->currentRenderTarget = this->screenRenderTarget.get();
renderEditorGrid(commandBuffer);
for (auto &obj : this->firstRenderables) {
if (obj && obj->editorOnly && !this->areEditorControlsEnabled()) continue;
if (obj && obj->editorOnly && !this->areEditorControlsEnabled())
continue;
obj->setViewMatrix(this->camera->calculateViewMatrix());
obj->setProjectionMatrix(calculateProjectionMatrix());
obj->render(getDeltaTime(), commandBuffer,
Expand All @@ -1823,7 +1839,8 @@ bool Window::stepFrame() {
if (obj->renderLateForward) {
continue;
}
if (obj && obj->editorOnly && !this->areEditorControlsEnabled()) continue;
if (obj && obj->editorOnly && !this->areEditorControlsEnabled())
continue;
obj->setViewMatrix(this->camera->calculateViewMatrix());
obj->setProjectionMatrix(calculateProjectionMatrix());
obj->render(getDeltaTime(), commandBuffer,
Expand All @@ -1833,7 +1850,8 @@ bool Window::stepFrame() {
updateFluidCaptures(commandBuffer);

for (auto &obj : this->lateForwardRenderables) {
if (obj && obj->editorOnly && !this->areEditorControlsEnabled()) continue;
if (obj && obj->editorOnly && !this->areEditorControlsEnabled())
continue;
obj->setViewMatrix(this->camera->calculateViewMatrix());
obj->setProjectionMatrix(calculateProjectionMatrix());
obj->render(getDeltaTime(), commandBuffer,
Expand All @@ -1846,7 +1864,8 @@ bool Window::stepFrame() {
}

for (auto &obj : this->preferenceRenderables) {
if (obj && obj->editorOnly && !this->areEditorControlsEnabled()) continue;
if (obj && obj->editorOnly && !this->areEditorControlsEnabled())
continue;
obj->setViewMatrix(this->camera->calculateViewMatrix());
obj->setProjectionMatrix(calculateProjectionMatrix());
obj->render(getDeltaTime(), commandBuffer, shouldRefreshPipeline(obj));
Expand Down Expand Up @@ -2068,11 +2087,10 @@ void Window::setEditorKeyboardTransformAxes(int axes) {
if (!editorKeyboardTransform)
return;
editorKeyboardTransformAxes = std::clamp(axes, 1, 7);
editorActiveGizmoAxis =
editorKeyboardTransformAxes == 1 ? 1
: editorKeyboardTransformAxes == 2 ? 2
: editorKeyboardTransformAxes == 4 ? 3
: 0;
editorActiveGizmoAxis = editorKeyboardTransformAxes == 1 ? 1
: editorKeyboardTransformAxes == 2 ? 2
: editorKeyboardTransformAxes == 4 ? 3
: 0;
}

void Window::finishEditorKeyboardTransform(bool commit) {
Expand Down Expand Up @@ -2694,12 +2712,15 @@ void Window::updateEditorDrag(float x, float y, float scale) {
std::max(0.05f, editorDragStartObjectScale.z + scaleDelta);
}
if (editorTransformSnapping) {
nextScale.x = std::round(nextScale.x / editorTransformSnapIncrement) *
editorTransformSnapIncrement;
nextScale.y = std::round(nextScale.y / editorTransformSnapIncrement) *
editorTransformSnapIncrement;
nextScale.z = std::round(nextScale.z / editorTransformSnapIncrement) *
editorTransformSnapIncrement;
nextScale.x =
std::round(nextScale.x / editorTransformSnapIncrement) *
editorTransformSnapIncrement;
nextScale.y =
std::round(nextScale.y / editorTransformSnapIncrement) *
editorTransformSnapIncrement;
nextScale.z =
std::round(nextScale.z / editorTransformSnapIncrement) *
editorTransformSnapIncrement;
nextScale.x = std::max(0.001f, nextScale.x);
nextScale.y = std::max(0.001f, nextScale.y);
nextScale.z = std::max(0.001f, nextScale.z);
Expand Down Expand Up @@ -2735,8 +2756,8 @@ void Window::updateEditorKeyboardTransform(float x, float y, float scale) {
const float dx = editorKeyboardAccumulatedX / effectiveScale;
const float dy = editorKeyboardAccumulatedY / effectiveScale;
const int axes = editorKeyboardTransformAxes;
const float distance = glm::length(editorDragStartPosition.toGlm() -
camera->position.toGlm());
const float distance =
glm::length(editorDragStartPosition.toGlm() - camera->position.toGlm());

if (editorControlMode == EditorControlMode::Move) {
glm::vec3 delta(0.0f);
Expand All @@ -2752,10 +2773,8 @@ void Window::updateEditorKeyboardTransform(float x, float y, float scale) {
const float sensitivity = std::max(0.0025f, distance * 0.0025f);
delta = (right * dx + up * dy) * sensitivity;
} else {
const float viewWidth =
std::max(1.0f, static_cast<float>(width));
const float viewHeight =
std::max(1.0f, static_cast<float>(height));
const float viewWidth = std::max(1.0f, static_cast<float>(width));
const float viewHeight = std::max(1.0f, static_cast<float>(height));
const glm::mat4 viewProjection =
calculateProjectionMatrix() * camera->calculateViewMatrix();
const glm::vec3 center = editorDragStartPosition.toGlm();
Expand Down Expand Up @@ -3992,10 +4011,9 @@ void Window::addRenderTarget(RenderTarget *target) {
}

void Window::removeRenderTarget(RenderTarget *target) {
this->renderTargets.erase(
std::remove(this->renderTargets.begin(), this->renderTargets.end(),
target),
this->renderTargets.end());
this->renderTargets.erase(std::remove(this->renderTargets.begin(),
this->renderTargets.end(), target),
this->renderTargets.end());
if (this->currentRenderTarget == target) {
this->currentRenderTarget = nullptr;
}
Expand Down Expand Up @@ -4870,7 +4888,8 @@ void Window::captureFluidReflection(
if (dynamic_cast<Fluid *>(obj) == &fluid) {
continue;
}
if (obj && obj->editorOnly && !this->areEditorControlsEnabled()) continue;
if (obj && obj->editorOnly && !this->areEditorControlsEnabled())
continue;
obj->setViewMatrix(view);
obj->setProjectionMatrix(projection);
obj->render(getDeltaTime(), commandBuffer,
Expand Down Expand Up @@ -4997,7 +5016,8 @@ void Window::captureFluidRefraction(
if (dynamic_cast<Fluid *>(obj) == &fluid) {
continue;
}
if (obj && obj->editorOnly && !this->areEditorControlsEnabled()) continue;
if (obj && obj->editorOnly && !this->areEditorControlsEnabled())
continue;
obj->setViewMatrix(view);
obj->setProjectionMatrix(projection);
obj->render(getDeltaTime(), commandBuffer,
Expand Down
23 changes: 22 additions & 1 deletion include/atlas/core/default_shaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -7561,6 +7561,7 @@ R"( float3(vertices[bj2].tangent) * bb2,

kernel void main0(texture2d<float, access::write> outTex [[texture(0)]],
texture2d<float, access::read> prevTex [[texture(1)]],
texture2d<float, access::write> brightTex [[texture(2)]],
instance_acceleration_structure sceneAS [[buffer(0)]],
constant CameraUniforms &cam [[buffer(1)]],
constant Material *materials [[buffer(2)]],
Expand Down Expand Up @@ -7632,7 +7633,27 @@ kernel void main0(texture2d<float, access::write> outTex [[texture(0)]],

float3 accum = (prevColor.xyz * frameIndex + color) / (frameIndex + 1);
accum = clampLuminance(accum, 24.0);
outTex.write(float4(accum, 1.0), gid);

constexpr float bloomThreshold = 1.0;
constexpr float bloomKnee = 0.5;

float brightness = luminance(accum);
float soft = clamp(
brightness - bloomThreshold + bloomKnee,
0.0,
bloomKnee * 2.0
);

soft = soft * soft / max(bloomKnee * 4.0, 0.00001);

float contribution =
max(brightness - bloomThreshold, soft) /
max(brightness, 0.00001);

float3 brightColor = accum * contribution;

outTex.write(float4(accum, 1.0), gid);
brightTex.write(float4(brightColor, 1.0), gid);
}
)",
};
Expand Down
1 change: 1 addition & 0 deletions include/photon/illuminate.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class PathTracing {
std::shared_ptr<Texture> pathTracingTexture;
/** @brief Previous frame output texture used for accumulation. */
std::shared_ptr<Texture> pathTracingTexturePrev;
std::shared_ptr<Texture> pathTracingTextureBright;

/** @brief Rays traced per pixel each dispatch. */
int raysPerPixel = 4;
Expand Down
4 changes: 4 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,7 @@ release backend="AUTO":
run-docs:
just docs
python3 -m http.server 8000 --directory docs/html

editor backend="AUTO" bezel_native="OFF":
just target AtlasEditor {{ backend }} {{ bezel_native }}
"./build/bin/Atlas Engine.app/Contents/MacOS/Atlas Engine"
11 changes: 10 additions & 1 deletion photon/path_tracing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ void photon::PathTracing::init() {
pathTracingTexturePrev = std::make_shared<Texture>(
Texture::create(outputWidth, outputHeight, opal::TextureFormat::Rgba16F,
opal::TextureDataFormat::Rgba, TextureType::Color));
pathTracingTextureBright = std::make_shared<Texture>(
Texture::create(outputWidth, outputHeight, opal::TextureFormat::Rgba16F,
opal::TextureDataFormat::Rgba, TextureType::Color));

copySrcFramebuffer = std::make_shared<opal::Framebuffer>();
copyDstFramebuffer = std::make_shared<opal::Framebuffer>();
Expand All @@ -187,7 +190,8 @@ void photon::PathTracing::resizeOutput(int width, int height) {
const int newWidth = std::max(1, width);
const int newHeight = std::max(1, height);
if (newWidth == outputWidth && newHeight == outputHeight &&
pathTracingTexture != nullptr && pathTracingTexturePrev != nullptr) {
pathTracingTexture != nullptr && pathTracingTexturePrev != nullptr &&
pathTracingTextureBright != nullptr) {
return;
}

Expand All @@ -199,6 +203,9 @@ void photon::PathTracing::resizeOutput(int width, int height) {
pathTracingTexturePrev = std::make_shared<Texture>(
Texture::create(outputWidth, outputHeight, opal::TextureFormat::Rgba16F,
opal::TextureDataFormat::Rgba, TextureType::Color));
pathTracingTextureBright = std::make_shared<Texture>(
Texture::create(outputWidth, outputHeight, opal::TextureFormat::Rgba16F,
opal::TextureDataFormat::Rgba, TextureType::Color));
frameIndex = 0;
}

Expand Down Expand Up @@ -743,6 +750,8 @@ void photon::PathTracing::render(
pathTracingPipeline->bindTexture("outTex", pathTracingTexture->texture, 0);
pathTracingPipeline->bindTexture("prevTex", pathTracingTexturePrev->texture,
1);
pathTracingPipeline->bindTexture("brightTex",
pathTracingTextureBright->texture, 2);

static std::shared_ptr<opal::Texture> fallbackSkyboxTexture = nullptr;
if (fallbackSkyboxTexture == nullptr) {
Expand Down
23 changes: 22 additions & 1 deletion shaders/metal/path_tracing/path.metal
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,7 @@ float3 sampleRadiance(uint2 gid, uint sampleIndex, uint w,

kernel void main0(texture2d<float, access::write> outTex [[texture(0)]],
texture2d<float, access::read> prevTex [[texture(1)]],
texture2d<float, access::write> brightTex [[texture(2)]],
instance_acceleration_structure sceneAS [[buffer(0)]],
constant CameraUniforms &cam [[buffer(1)]],
constant Material *materials [[buffer(2)]],
Expand Down Expand Up @@ -1255,5 +1256,25 @@ kernel void main0(texture2d<float, access::write> outTex [[texture(0)]],

float3 accum = (prevColor.xyz * frameIndex + color) / (frameIndex + 1);
accum = clampLuminance(accum, 24.0);
outTex.write(float4(accum, 1.0), gid);

constexpr float bloomThreshold = 1.0;
constexpr float bloomKnee = 0.5;

float brightness = luminance(accum);
float soft = clamp(
brightness - bloomThreshold + bloomKnee,
0.0,
bloomKnee * 2.0
);

soft = soft * soft / max(bloomKnee * 4.0, 0.00001);

float contribution =
max(brightness - bloomThreshold, soft) /
max(brightness, 0.00001);

float3 brightColor = accum * contribution;

outTex.write(float4(accum, 1.0), gid);
brightTex.write(float4(brightColor, 1.0), gid);
}
1 change: 1 addition & 0 deletions tests/path-tracing/main.ascene
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
"targets": [
{
"display": true,
"effects": [],
"name": "Main Target",
"render": true,
"type": "scene"
Expand Down
Loading