Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
131 changes: 120 additions & 11 deletions atlas/application/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,28 @@ computeShadowCasterSignature(const std::vector<Renderable *> &shadowCasters) {

return signature;
}

class RenderingContextScope {
public:
explicit RenderingContextScope(Window &window)
: previousWindow(Window::mainWindow),
previousDevice(opal::Device::globalInstance) {
window.activateRenderingContext();
}

~RenderingContextScope() {
if (previousWindow != nullptr) {
previousWindow->activateRenderingContext();
return;
}
Window::mainWindow = nullptr;
opal::Device::globalInstance = previousDevice;
}

private:
Window *previousWindow;
opal::Device *previousDevice;
};
} // namespace

Window::Window(const WindowConfiguration &config)
Expand Down Expand Up @@ -1034,7 +1056,7 @@ Window::Window(const WindowConfiguration &config)
this->setEditorControlsEnabled(config.editorControls);
this->metalUpscalingRatio = this->renderScale;

Window::mainWindow = this;
activateRenderingContext();

float initialMouseX = 0.0f;
float initialMouseY = 0.0f;
Expand Down Expand Up @@ -1404,6 +1426,7 @@ void Window::pollEvents() {
}

bool Window::stepFrame() {
RenderingContextScope renderingContext(*this);
this->initializeRunLoop();
if (this->shouldClose) {
return false;
Expand Down Expand Up @@ -1572,7 +1595,9 @@ bool Window::stepFrame() {

DebugTimer gpuTimer("Gpu Data");

renderLightsToShadowMaps(commandBuffer);
if (!this->usePathTracing) {
renderLightsToShadowMaps(commandBuffer);
}

std::vector<RenderTarget *> activeRenderTargets = this->renderTargets;
bool usesModeScreenTarget = false;
Expand Down Expand Up @@ -1632,8 +1657,13 @@ bool Window::stepFrame() {
}
#ifdef METAL
pathTracer->resizeOutput(target->getWidth(), target->getHeight());
pathTracer->render(commandBuffer, target->texture.texture,
target->brightTexture.texture);
if (!pathTracer->render(commandBuffer, target->texture.texture,
target->brightTexture.texture)) {
commandBuffer->beginPass(newRenderPass);
commandBuffer->clearColor(0.08f, 0.01f, 0.01f, 1.0f);
commandBuffer->clearDepth(1.0f);
commandBuffer->endPass();
}
#endif

continue;
Expand Down Expand Up @@ -1968,6 +1998,14 @@ bool Window::stepFrame() {
return !this->shouldClose;
}

void Window::activateRenderingContext() {
if (device != nullptr && device->context != nullptr) {
device->context->makeCurrent();
}
Window::mainWindow = this;
opal::Device::globalInstance = device.get();
}

void Window::resize(int width, int height, float scale) {
const int clampedWidth = std::max(1, width);
const int clampedHeight = std::max(1, height);
Expand Down Expand Up @@ -1995,6 +2033,31 @@ void Window::resize(int width, int height, float scale) {

device->getDefaultFramebuffer()->setViewport(0, 0, pixelWidth, pixelHeight);
setViewportState(0, 0, pixelWidth, pixelHeight);
const int targetWidth = std::max(
1, static_cast<int>(pixelWidth * this->getRenderScale()));
const int targetHeight = std::max(
1, static_cast<int>(pixelHeight * this->getRenderScale()));
for (RenderTarget *target : renderTargets) {
if (target != nullptr &&
(target->type == RenderTargetType::Scene ||
target->type == RenderTargetType::Multisampled) &&
(target->getWidth() != targetWidth ||
target->getHeight() != targetHeight)) {
target->resize(*this);
}
}
const std::array<std::shared_ptr<RenderTarget> *, 7> internalTargets = {
&gBuffer, &ssaoBuffer, &ssaoBlurBuffer,
&volumetricBuffer, &lightBuffer, &ssrFramebuffer,
&ssrHistoryFramebuffer};
for (auto *target : internalTargets) {
if (target != nullptr && *target != nullptr) {
(*target)->resize(*this);
}
}
if (bloomBuffer != nullptr) {
bloomBuffer->destroy();
}
this->editorGridInitialized = false;
this->shadowMapsDirty = true;
this->ssaoMapsDirty = true;
Expand Down Expand Up @@ -2301,8 +2364,13 @@ void Window::editorPointerEvent(int action, float x, float y, int button,
updateEditorCameraDrag(x, y, effectiveScale);
} else if (action == 2) {
editorCameraDragging = false;
editorOrbitVelocityX *= 0.65f;
editorOrbitVelocityY *= 0.65f;
if (usePathTracing) {
editorOrbitVelocityX = 0.0f;
editorOrbitVelocityY = 0.0f;
} else {
editorOrbitVelocityX *= 0.65f;
editorOrbitVelocityY *= 0.65f;
}
}
return;
}
Expand Down Expand Up @@ -2370,8 +2438,12 @@ void Window::editorScrollEvent(float delta, float scale) {
}

applyEditorZoomDelta(scrollAmount);
editorZoomVelocity += scrollAmount * 0.01f;
editorZoomVelocity = std::clamp(editorZoomVelocity, -80.0f, 80.0f);
if (usePathTracing) {
editorZoomVelocity = 0.0f;
} else {
editorZoomVelocity += scrollAmount * 0.01f;
editorZoomVelocity = std::clamp(editorZoomVelocity, -80.0f, 80.0f);
}
}

void Window::editorKeyEvent(int key, bool pressed) {
Expand Down Expand Up @@ -2888,8 +2960,13 @@ void Window::updateEditorCameraDrag(float x, float y, float scale) {
float yawDelta = dx * 0.22f;
float pitchDelta = -dy * 0.22f;
applyEditorOrbitDelta(yawDelta, pitchDelta);
editorOrbitVelocityX = yawDelta * 45.0f;
editorOrbitVelocityY = pitchDelta * 45.0f;
if (usePathTracing) {
editorOrbitVelocityX = 0.0f;
editorOrbitVelocityY = 0.0f;
} else {
editorOrbitVelocityX = yawDelta * 45.0f;
editorOrbitVelocityY = pitchDelta * 45.0f;
}
}

void Window::updateEditorCameraPan(float x, float y, float scale) {
Expand Down Expand Up @@ -3056,6 +3133,13 @@ void Window::updateEditorCameraInertia(float deltaTime) {
return;
}

if (usePathTracing) {
editorOrbitVelocityX = 0.0f;
editorOrbitVelocityY = 0.0f;
editorZoomVelocity = 0.0f;
return;
}

float dt = std::clamp(deltaTime, 1.0f / 240.0f, 1.0f / 30.0f);
if (!editorCameraDragging) {
if (std::abs(editorOrbitVelocityX) > 0.0001f ||
Expand Down Expand Up @@ -4729,7 +4813,9 @@ void Window::renderPingpong(RenderTarget *target) {
blurPipeline->setUniform1i("image", 0);

target->object->vao->bind();
target->object->ebo->bind();
if (target->object->ebo != nullptr) {
target->object->ebo->bind();
}

for (unsigned int i = 0; i < blurIterations; ++i) {
this->pingpongFramebuffers.at(horizontal)->bind();
Expand Down Expand Up @@ -5544,4 +5630,27 @@ void Window::enablePathTracing() {
this->pathTracer = std::make_shared<photon::PathTracing>();
pathTracer->init();
}

bool Window::setEditorPathTracingPreview(bool enabled) {
if (pathTracer == nullptr) {
return false;
}
if (enabled) {
if (gBuffer == nullptr) {
useDeferredRendering();
} else {
usePathTracing = false;
usesDeferred = true;
}
} else {
usesDeferred = false;
usePathTracing = true;
}
return true;
}

const std::string &Window::getPathTracingError() const {
static const std::string noError;
return pathTracer != nullptr ? pathTracer->getLastError() : noError;
}
#endif
72 changes: 43 additions & 29 deletions atlas/graphics/render_target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

RenderTarget::RenderTarget(Window &window, RenderTargetType type,
int resolution) {
creationResolution = resolution;
atlas_log("Creating render target (type: " +
std::to_string(static_cast<int>(type)) + ")");
Size2d drawableSize = window.getSize();
Expand Down Expand Up @@ -472,29 +473,54 @@ RenderTarget::RenderTarget(Window &window, RenderTargetType type,
packet.send();
}

void RenderTarget::resize(Window &window) {
if (type == RenderTargetType::Shadow ||
type == RenderTargetType::CubeShadow) {
return;
}
auto displayedObject = object;
auto savedEffects = std::move(effects);
const RenderTargetType savedType = type;
RenderTarget replacement(window, savedType, creationResolution);
replacement.object = std::move(displayedObject);
replacement.effects = std::move(savedEffects);
*this = std::move(replacement);
if (object != nullptr) {
object->textures.clear();
object->attachTexture(texture);
}
}

void RenderTarget::display(Window &window, float zindex) {
if (object == nullptr) {
CoreObject obj;
std::vector<CoreVertex> vertices = {
#ifdef METAL
{{1.0f, 1.0f, zindex}, Color::white(), {1.0f, 0.0f}}, // top right
{{1.0f, 1.0f, zindex}, Color::white(), {1.0f, 0.0f}},
{{-1.0f, 1.0f, zindex}, Color::white(), {0.0f, 0.0f}},
{{1.0f, -1.0f, zindex},
Color::white(),
{1.0f, 1.0f}},
{{1.0f, -1.0f, zindex},
Color::white(),
{1.0f, 1.0f}}, // bottom right
{1.0f, 1.0f}},
{{-1.0f, 1.0f, zindex}, Color::white(), {0.0f, 0.0f}},
{{-1.0f, -1.0f, zindex},
Color::white(),
{0.0f, 1.0f}}, // bottom left
{{-1.0f, 1.0f, zindex}, Color::white(), {0.0f, 0.0f}} // top left
{0.0f, 1.0f}}
#else
// positions // texture coords
{{1.0f, 1.0f, zindex}, Color::white(), {1.0f, 1.0f}}, // top right
{{1.0f, 1.0f, zindex}, Color::white(), {1.0f, 1.0f}},
{{1.0f, -1.0f, zindex},
Color::white(),
{1.0f, 0.0f}}, // bottom right
{1.0f, 0.0f}},
{{-1.0f, 1.0f, zindex}, Color::white(), {0.0f, 1.0f}},
{{1.0f, -1.0f, zindex},
Color::white(),
{1.0f, 0.0f}},
{{-1.0f, -1.0f, zindex},
Color::white(),
{0.0f, 0.0f}}, // bottom left
{{-1.0f, 1.0f, zindex}, Color::white(), {0.0f, 1.0f}} // top left
{0.0f, 0.0f}},
{{-1.0f, 1.0f, zindex}, Color::white(), {0.0f, 1.0f}}
#endif
};
VertexShader vertexShader =
Expand All @@ -506,15 +532,8 @@ void RenderTarget::display(Window &window, float zindex) {

obj.createAndAttachProgram(vertexShader, fragmentShader);

#ifdef METAL
std::vector<Index> indices = {0, 3, 1, 1, 3, 2};
#else
std::vector<Index> indices = {0, 1, 3, 1, 2, 3};
#endif

obj.attachTexture(this->texture);
obj.attachVertices(vertices);
obj.attachIndices(indices);
obj.renderOnlyTexture();
obj.show();
obj.initialize();
Expand Down Expand Up @@ -762,8 +781,10 @@ void RenderTarget::render(float dt,
renderTargetPipeline->setUniform1i("hasBrightTexture",
blurredTexture.id != 0 ? 1 : 0);

uint depthTextureId = depthTexture.id;
bool hasDepth = depthTexture.id != 0;
const bool hasDepth = depthTexture.id != 0 &&
(Window::mainWindow == nullptr ||
!Window::mainWindow->usePathTracing);
uint depthTextureId = hasDepth ? depthTexture.id : 0;
renderTargetPipeline->bindTexture2D("DepthTexture", depthTextureId, 2,
obj->id);
renderTargetPipeline->setUniform1i("hasDepthTexture", hasDepth ? 1 : 0);
Expand Down Expand Up @@ -917,14 +938,8 @@ void RenderTarget::render(float dt,

commandBuffer->bindDrawingState(obj->vao);
commandBuffer->bindPipeline(renderTargetPipeline);
if (!obj->indices.empty()) {
commandBuffer->drawIndexed(
static_cast<unsigned int>(obj->indices.size()), 1, 0, 0, 0,
obj->id);
} else {
commandBuffer->draw(static_cast<unsigned int>(obj->vertices.size()), 1,
0, 0, obj->id);
}
commandBuffer->draw(static_cast<unsigned int>(obj->vertices.size()), 1, 0,
0, obj->id);
commandBuffer->unbindDrawingState();

renderTargetPipeline->enableDepthTest(true);
Expand All @@ -940,9 +955,8 @@ void RenderTarget::render(float dt,
: 0;
debugPacket.triangleCount = 2;
debugPacket.vertexBufferSizeMb =
static_cast<float>(sizeof(CoreVertex) * 4) / (1024.0f * 1024.0f);
debugPacket.indexBufferSizeMb =
static_cast<float>(sizeof(Index) * 6) / (1024.0f * 1024.0f);
static_cast<float>(sizeof(CoreVertex) * 6) / (1024.0f * 1024.0f);
debugPacket.indexBufferSizeMb = 0.0f;
debugPacket.textureCount =
1 + (brightTexture.id != 0 ? 1 : 0) +
(depthTexture.id != 0 ? 1 : 0) + (gPosition.id != 0 ? 1 : 0) +
Expand Down
15 changes: 11 additions & 4 deletions atlas/graphics/texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,11 @@ Texture Texture::fromResource(const Resource& resource, TextureType type,
dataFormat = opal::TextureDataFormat::Rgba;
}

const uint mipLevels = 1u + static_cast<uint>(std::floor(std::log2(
std::max(width, height))));
opalTexture =
opal::Texture::create(opal::TextureType::Texture2D, internalFormat,
width, height, dataFormat, data, 1);
width, height, dataFormat, data, mipLevels);
stbi_image_free(data);
}
else {
Expand Down Expand Up @@ -286,9 +288,11 @@ Texture Texture::fromResource(const Resource& resource, TextureType type,
dataFormat = opal::TextureDataFormat::Red;
}

const uint mipLevels = 1u + static_cast<uint>(std::floor(std::log2(
std::max(width, height))));
opalTexture =
opal::Texture::create(opal::TextureType::Texture2D, internalFormat,
width, height, dataFormat, data, 1);
width, height, dataFormat, data, mipLevels);
stbi_image_free(data);
}

Expand All @@ -313,9 +317,12 @@ Texture Texture::fromResource(const Resource& resource, TextureType type,
? opal::TextureFilterMode::Nearest
: opal::TextureFilterMode::Linear;
};
const opal::TextureFilterMode minFilter =
params.minifyingFilter == TextureFilteringMode::Nearest
? opal::TextureFilterMode::NearestMipmapNearest
: opal::TextureFilterMode::LinearMipmapLinear;
opalTexture->setParameters(toOpalWrap(params.wrappingModeS),
toOpalWrap(params.wrappingModeT),
toOpalFilter(params.minifyingFilter),
toOpalWrap(params.wrappingModeT), minFilter,
toOpalFilter(params.magnifyingFilter));

if (params.wrappingModeS == TextureWrappingMode::ClampToBorder ||
Expand Down
Loading
Loading