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
2 changes: 1 addition & 1 deletion examples/sample-scenes/include/MouseCollisionScene.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class MouseCollisionScene : public Scene
m_debugInput = true;
m_debugFly = true;

for (size_t i = 0; i < 600; i++)
for (size_t i = 0; i < 9900; i++)
{

float y = (int)(i / 20);
Expand Down
47 changes: 45 additions & 2 deletions include/weird-engine/Profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,18 @@ namespace WeirdEngine
<< std::setw(10) << "% Frame" << "\n";
std::cout << "-----------------------------------------------------------------\n";

for (const auto& stat : m_stats)
printStatsRange(0, m_stats.size(), totalFrameTimeMs);

std::cout << "=================================================================\n\n";
}

void printStatsRange(size_t start, size_t end, double totalFrameTimeMs)
{
size_t i = start;
while (i < end)
{
const auto& stat = m_stats[i];

std::string indent(stat.depth * 4, ' ');
std::string name = indent + stat.name;
double avgTimeMs = stat.totalTimeMs / stat.count;
Expand All @@ -150,8 +160,41 @@ namespace WeirdEngine
std::cout << std::left << std::setw(40) << name << std::right << std::setw(15) << std::fixed
<< std::setprecision(3) << avgTimeMs << std::setw(9) << std::fixed << std::setprecision(1)
<< percentage << "%\n";

// Find end of this stat's subtree
size_t subEnd = i + 1;
while (subEnd < end && m_stats[subEnd].depth > stat.depth)
subEnd++;

if (subEnd > i + 1)
{
// Sum direct children total times
double childrenTotalMs = 0.0;
for (size_t j = i + 1; j < subEnd; j++)
{
if (m_stats[j].depth == stat.depth + 1)
childrenTotalMs += m_stats[j].totalTimeMs;
}

// Print children recursively
printStatsRange(i + 1, subEnd, totalFrameTimeMs);

// If unaccounted time exceeds 1% of frame, print an "Other" row
double unaccountedMs = stat.totalTimeMs - childrenTotalMs;
double unaccountedPct = totalFrameTimeMs > 0.0 ? (unaccountedMs / totalFrameTimeMs) * 100.0 : 0.0;
if (unaccountedPct > 1.0)
{
std::string otherIndent((stat.depth + 1) * 4, ' ');
std::string otherName = otherIndent + "Other";
double otherAvgMs = unaccountedMs / stat.count;
std::cout << std::left << std::setw(40) << otherName << std::right << std::setw(15)
<< std::fixed << std::setprecision(3) << otherAvgMs << std::setw(9) << std::fixed
<< std::setprecision(1) << unaccountedPct << "%\n";
}
}

i = subEnd;
}
std::cout << "=================================================================\n\n";
}

struct StackItem
Expand Down
4 changes: 2 additions & 2 deletions include/weird-engine/systems/2DSDFShaderGenerationSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ namespace WeirdEngine::SDFShaderGenerationSystem2D
oss << "int idx = dataOffset + " << 2 * i << ";\n";

// Fetch parameters
oss << "vec4 parameters0 = texelFetch(t_shapeBuffer, ivec2(idx, 0), 0);\n";
oss << "vec4 parameters1 = texelFetch(t_shapeBuffer, ivec2(idx + 1, 0), 0);\n";
oss << "vec4 parameters0 = texelFetch(t_shapeBuffer, ivec2(idx % 16384, idx / 16384), 0);\n";
oss << "vec4 parameters1 = texelFetch(t_shapeBuffer, ivec2((idx + 1) % 16384, (idx + 1) / 16384), 0);\n";

// Get distance function
auto fragmentCode = sdfs[shape.distanceFieldId]->print();
Expand Down
19 changes: 16 additions & 3 deletions include/weird-renderer/core/SDF2DRenderPipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "weird-renderer/resources/Shader.h"
#include "weird-renderer/resources/Texture.h"
#include "weird-renderer/scene/Camera.h"
#include <vector>

namespace WeirdEngine
{
Expand Down Expand Up @@ -36,6 +37,7 @@ namespace WeirdEngine
float motionBlurBlendSpeed;
bool debugDistanceField;
bool debugMaterialColors;
bool debugGrid = false;
float ballK;
float ambienOcclusionRadius;
float ambienOcclusionStrength;
Expand Down Expand Up @@ -108,6 +110,16 @@ namespace WeirdEngine
RenderTarget m_litSceneRender;

DataBuffer m_shapeDataBuffer;
DataBuffer m_gridHeaderBuffer;
DataBuffer m_gridIndicesBuffer;

// Reusable scratch buffers for grid building — allocated once, reused every frame
struct ObjBounds { int minX, minY, maxX, maxY; };
std::vector<ObjBounds> m_gridObjBounds;
std::vector<int> m_gridCellCounts;
std::vector<int> m_gridCellOffsets;
std::vector<glm::vec4> m_gridHeader;
std::vector<glm::vec4> m_gridIndices;

glm::mat4 m_oldCameraMatrix;
glm::mat4
Expand All @@ -116,9 +128,10 @@ namespace WeirdEngine

bool horizontal = true;
glm::vec3 cameraPositionChange;

void renderDistanceField(vec4* shapeData, uint32_t dataSize, uint32_t shapeCount, const Camera& camera,
double time, double delta);
struct GridInfo { float minX, minY, stepX, stepY; int gridCols, gridRows, indexTexWidth, indexTexHeight; };
GridInfo buildAccelerationGrid(vec4* shapeData, uint32_t dataSize, uint32_t shapeCount,
const Camera& camera); void renderDistanceField(vec4* shapeData, uint32_t dataSize, uint32_t shapeCount, const Camera& camera,
double time, double delta);
void applyJumpFloodCorrection(double time);
void upscaleDistance();
void renderMaterialColors(const Camera& camera, double time, double delta);
Expand Down
56 changes: 54 additions & 2 deletions include/weird-renderer/resources/DataBuffer.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once
#include <cassert>
#include <cstring>
#include <vector>
#include <glad/glad.h>

namespace WeirdEngine
Expand Down Expand Up @@ -40,14 +42,43 @@ namespace WeirdEngine
if (byteSize == 0 || (byteSize % texelBytes != 0))
return;

GLsizei width = static_cast<GLsizei>(byteSize / texelBytes);
GLint maxTexSize = 16384;
// glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTexSize);

size_t totalTexels = byteSize / texelBytes;
GLsizei texW, texH;

auto s = static_cast<GLsizei>(totalTexels);
if (s <= maxTexSize)
{
texW = static_cast<GLsizei>(totalTexels);
texH = 1;
}
else
{
// Wrap into 2D: use maxTexSize as row width, add enough rows
texW = static_cast<GLsizei>(maxTexSize);
texH = static_cast<GLsizei>((totalTexels + maxTexSize - 1) / maxTexSize);
}

// When wrapping to 2D, texW*texH may exceed totalTexels (due to ceiling
// division). Pad with zeros so OpenGL never reads past the source buffer.
const void* uploadPtr = data;
std::vector<float> paddedData;
size_t paddedTexels = static_cast<size_t>(texW) * static_cast<size_t>(texH);
if (paddedTexels > totalTexels)
{
paddedData.resize(paddedTexels * 4, 0.0f); // 4 floats per RGBA32F texel
std::memcpy(paddedData.data(), data, byteSize);
uploadPtr = paddedData.data();
}

GLint previousActiveTexture = 0;
GLint previousTextureBinding2D = 0;
glGetIntegerv(GL_ACTIVE_TEXTURE, &previousActiveTexture);
glGetIntegerv(GL_TEXTURE_BINDING_2D, &previousTextureBinding2D);
glBindTexture(GL_TEXTURE_2D, m_texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, width, 1, 0, GL_RGBA, GL_FLOAT, data);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, texW, texH, 0, GL_RGBA, GL_FLOAT, uploadPtr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
Expand All @@ -61,6 +92,27 @@ namespace WeirdEngine
uploadRawData(data, count * sizeof(T));
}

template <typename T> void uploadData2D(const T* data, size_t width, size_t height) const
{
size_t byteSize = width * height * sizeof(T);
const size_t texelBytes = 4 * sizeof(float); // one RGBA32F texel
if (byteSize == 0 || (byteSize % texelBytes != 0))
return;

GLint previousActiveTexture = 0;
GLint previousTextureBinding2D = 0;
glGetIntegerv(GL_ACTIVE_TEXTURE, &previousActiveTexture);
glGetIntegerv(GL_TEXTURE_BINDING_2D, &previousTextureBinding2D);
glBindTexture(GL_TEXTURE_2D, m_texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, static_cast<GLsizei>(width), static_cast<GLsizei>(height), 0, GL_RGBA, GL_FLOAT, data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_2D, static_cast<GLuint>(previousTextureBinding2D));
glActiveTexture(static_cast<GLenum>(previousActiveTexture));
}

GLuint getTexture() const
{
return m_texture;
Expand Down
5 changes: 5 additions & 0 deletions include/weird-renderer/resources/Shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ namespace WeirdEngine
glUniform1i(getUniformLocation(name), value);
}

void setUniform(const std::string& name, const glm::ivec2& value) const
{
glUniform2iv(getUniformLocation(name), 1, &value[0]);
}

void setUniform(const std::string& name, const glm::vec2& value) const
{
glUniform2fv(getUniformLocation(name), 1, &value[0]);
Expand Down
8 changes: 7 additions & 1 deletion src/weird-renderer/core/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ namespace WeirdEngine

auto& result = renderScene(scene, time, clampedDelta);
output(scene, result, clampedDelta);
glFinish();

static bool showDebugUI = false;

Expand Down Expand Up @@ -302,9 +303,11 @@ namespace WeirdEngine

bool enable3DSDFs = true;

// Render geometry
// 3D
if (enable3D)
{
PROFILE_SCOPE("3D Render");

auto& renderQueue = scene.getDrawQueue(); // TODO: sort and then draw it

// Set up framebuffer for 3D scene rendering
Expand Down Expand Up @@ -359,6 +362,7 @@ namespace WeirdEngine

// Draw the render plane with ray marching shader
m_renderPlane.draw(m_3DsdfShaderProgram);
glFinish();
}

// Render 3D geometry objects (with depth writing)
Expand All @@ -376,6 +380,8 @@ namespace WeirdEngine
// Draw objects in the scene (3D models)
scene.renderModels(m_3DSceneRender, m_geometryShaderProgram, m_instancedGeometryShaderProgram);
}

glFinish();

if (!enable2D)
{
Expand Down
Loading
Loading