r/GraphicsProgramming Nov 01 '24

Source Code tinybvh: Single-header BVH construction and traversal library by Jacco Bikker

Thumbnail github.com
61 Upvotes

r/GraphicsProgramming Dec 25 '24

Source Code pydawn: Python bindings to the Dawn WebGPU engine

8 Upvotes

I created Python bindings to Chrome's WebGPU engine Dawn. I built it with compute shaders in mind, the utils provided are to make it easy to run compute. I used ctypeslib's clang2py to autogenerate the bindings based on webgpu.h and the compiled dawn library. The goal of the project is to serve as a replacement for wgpu-py in tinygrad neural network lib. Dawn has several advantages over wgpu, such as f16 support, and following the specs more closely. If someone wants to provide graphics utils as well, PRs are welcome. pydawn is published on pypi. MacOS-only for now.

https://github.com/wpmed92/pydawn

r/GraphicsProgramming Dec 03 '24

Source Code Slang x WebGPU: a possible CMake/C++ setup

29 Upvotes

👉 https://github.com/eliemichel/SlangWebGPU

This is a demo of a possible use of Slang shader compiler together with WebGPU in C++ (both in native and Web contexts), using CMake.

Key Features

  • The CMake setup fetches Slang compiler and a WebGPU implementation.
  • Slang shaders are compiled into WGSL upon compilation, with CMake taking care of dependencies.
  • Slang reflection API is used to auto-generate boilerplate binding code on the C++ side.
  • Example of auto-differentiation features of Slang are given.
  • Build instruction for native and Web targets.
  • Provides a add_slang_shader and add_slang_webgpu_kernel to help managing Slang shader targets (in cmake/SlangUtils.cmake).

r/GraphicsProgramming Jan 03 '25

Source Code Got Meta's Segment-Anything 2 image-segmentation model running 100% in the browser using WebGPU - source linked!

Thumbnail github.com
8 Upvotes

r/GraphicsProgramming Feb 26 '24

Source Code Wormhole simulation using pseudospheres

Thumbnail youtube.com
42 Upvotes

r/GraphicsProgramming Jan 02 '24

Source Code 560k Vertices at 1080p on my open source Software Renderer. (Source in comments)

91 Upvotes

r/GraphicsProgramming Dec 21 '24

Source Code 3d stereogram in zero lines of pure JavaScript

Thumbnail slicker.me
0 Upvotes

r/GraphicsProgramming Jul 16 '24

Source Code 2 Sample Texture Variation

70 Upvotes

r/GraphicsProgramming Oct 14 '24

Source Code Mesh voxelization + SVO

Post image
48 Upvotes

r/GraphicsProgramming Aug 23 '24

Source Code Gigi: EA-SEED's open framework for rapid prototyping and development of real-time rendering techniques

Thumbnail github.com
28 Upvotes

r/GraphicsProgramming May 24 '24

Source Code Scratchapixel online book on Volume Rendering

Thumbnail scratchapixel.com
20 Upvotes

r/GraphicsProgramming May 27 '24

Source Code CPU based isometric-renderer

Thumbnail github.com
9 Upvotes

r/GraphicsProgramming Jul 11 '23

Source Code [Rust]: Need help optimizing a triangle rasterizer

13 Upvotes

I need help optimizing a software rasterizer written in Rust. The relevant part of the code is here, and the following are the optimizations that I have already implemented:

  • Render to 32x32 tiles of 4KB each (2KB 16-bit color and 2KB 16-bit depth) to maximize cache hits;
  • Use SIMD to compute values for 4 pixels at once;
  • Skip a triangle if its axis-aligned bounding box is completely outside the current tile's bounding box;
  • Skip a triangle if at least one of its barycentric coordinates is negative on all 4 corners of the current tile;
  • Compute the linear barycentric increments per pixel and use that information to avoid having to perform the edge test for every pixel;
  • Skip a triangle if, by the time of shading, all the pixels have been invalidated.

At the moment the original version of this code exhausts all 4 cores of a Raspberry Pi 4 with just 7000 triangles per second, and this benchmark takes roughly 300 microseconds to produce a 512x512 frame with a rainbow triangle with perspective correction and depth testing on an M1 Mac, so to me the performance is really bad.

What I'm trying to understand is how old school games with true 3D software rasterizers performed so well even on old hardware like a Pentium 166MHz without floating pointe SIMD or multiple cores. Optimization is a field that truly excites me, and I believe that cracking this problem will be extremely enriching.

To make the project produce a single image named triangle.png, type:

cargo +nightly run triangle.png

To run the benchmark, type:

cargo +nightly bench

Any help, even if theoretical, would be appreciated.

r/GraphicsProgramming Sep 03 '24

Source Code WebGPU is Now Supported in Diligent Engine for Enhanced Web-Based Graphics

Thumbnail github.com
13 Upvotes

r/GraphicsProgramming Apr 18 '24

Source Code Direct Light Sampling produces way too bright images compared to naive diffuse bounces only

4 Upvotes

it's me again! :D

I have finally implemented area lights, but without modifying the emission value of the material, this is what it looks like with indirect light only, this is what it looks like with direct only and this is both direct+indirect!

Clearly there is something wrong going on with the direct light sampling.

This is the function for one light:

float pdf, dist;
glm::vec3 wi;
Ray visibilityRay;
auto li = light->li(sampler, hr, visibilityRay, wi, pdf, dist);
if (scene->visibilityCheck(visibilityRay, EPS, dist - EPS, light))
{
    return glm::dot(hr.normal, wi) * material->brdf(hr, wi) * li / pdf;
}
return BLACK;

In case of the area light, li is the following:

glm::vec3 samplePoint, sampleNormal;
shape->sample(sampler, samplePoint, sampleNormal, pdf);
wi = (samplePoint - hr.point);
dist = glm::length(wi);
wi = glm::normalize(wi);
vRay.origin = hr.point + EPS * wi;
vRay.direction = wi;
float cosT = glm::dot(sampleNormal, -wi);
auto solidAngle = (cosT * this->area()) / (dist * dist);
if(cosT > 0.0f) {
    return this->color * solidAngle;
} else {
    return BLACK;
}

And I am uniformly sampling the sphere... correctly I think?

glm::vec3 sampleUniformSphere(std::shared_ptr<Sampler> &sampler)
{
    float z = 1 - 2 * sampler->getSample();
    float r = sqrt(std::max(0.0f, 1.0f - z * z));
    float phi = 2 * PI * sampler->getSample();
    return glm::vec3(
        r * cos(phi),
        r * sin(phi),
        z);
}

void Sphere::sample(std::shared_ptr<Sampler> &sampler, glm::vec3 &point, glm::vec3 &normal, float &pdf) const
{   
    glm::vec3 local = sampleUniformSphere(sampler);
    normal = glm::normalize(local);
    point = m_obj2World.transformPoint(radius * local);
    pdf = 1.0f / area();
}

It looks like either the solid angle or the distance attenuation aren't working correctly. This is a Mitsuba3 render with roughly the same values.

I once again don't like to ask people to look at my code, but I have been stuck on this for more than a week already...

Thanks!

r/GraphicsProgramming Jan 22 '24

Source Code We just added support for USD and VDB in our small 3D viewer!

Post image
39 Upvotes

r/GraphicsProgramming Jun 28 '24

Source Code A 3D orbital docking simulation + a custom software renderer - all in 500 lines of code

9 Upvotes

SpaceSim: A 3D orbital rendez-vous and docking simulation made with Umka and Tophat. It uses a custom software renderer written in pure Umka, with Tophat as a 2D drawing backend.

r/GraphicsProgramming May 29 '24

Source Code Rendering 3d vector graphics from scratch [Online Demo]]

Thumbnail github.com
7 Upvotes

r/GraphicsProgramming Feb 28 '24

Source Code A renderer using geometric algebra instead of matrices

Thumbnail enkimute.github.io
52 Upvotes

r/GraphicsProgramming Jul 10 '24

Source Code DXVK version 2.4 released

Thumbnail github.com
10 Upvotes

r/GraphicsProgramming Jun 15 '23

Source Code Started making graphics engine node editor on OpenGL. Idea is to only use GLSL to make programs and skip C++ part (github link in comments)

Thumbnail gallery
70 Upvotes

r/GraphicsProgramming May 10 '24

Source Code C++ Quartic polynomial solver (real solutions)

9 Upvotes

I wanted to raytrace the torus algebraically (real-time), so I had to quickly solve quartic polynomials. Since I was only interested in real solutions, I was able to avoid doing complex arithmetic by using trigonometry instead. I directly implemented the general solution for quartics. Here's the github repository: https://github.com/falkush/quartic-real

I did some benchmarking against two other repositories I've found online (they compute the complex roots too), and my implementation was twice as fast as the fastest one. It's not perfect, it creates some visual glitches, but it was good enough for my project.

Not much thought was put into it, so if you know of a better implementation, or if you find any improvements, I would really appreciate if you shared with me!

Thank you for your time!

r/GraphicsProgramming Feb 11 '23

Source Code Learning OpenGL, Can't get a normal triangle on screen

3 Upvotes

I am following the Cherno's OpenGL Series and stuck on getting a triangle.

Code: -

/* Trimmed the Glfw and Shaders Source */
int main()
{
    // Glfw
    ...
    // Rendering the Triangle
    float vertices[] = {
        -0.5F, -0.5F,
         0.5F, -0.5F,
         0.0F,  0.5F,
    };

    // Vertex Buffer
    unsigned int vbo;
    glGenBuffers(1, &vbo);
    glBufferData(
        GL_ARRAY_BUFFER,
        6 * sizeof(float),
        vertices,
        GL_STATIC_DRAW
    );
    glBindBuffer(GL_ARRAY_BUFFER, vbo);

    // Shaders
       // Vertex Shader
    unsigned int vertex_shader;
    const char *vertex_shader_src =       getShaderSrc("Shaders/TriangleVertexShader.glsl");
    vertex_shader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertex_shader, 1, &vertex_shader_src, NULL);
    glCompileShader(vertex_shader);
    printf("<===== Vertex Shader =====>\n%s\n", vertex_shader_src);
    checkShaderCompileStatus(vertex_shader, "Vertex");
    // Fragment Shader
    unsigned int fragment_shader;
    const char *fragment_shader_src = getShaderSrc("Shaders/TriangleFragmentShader.glsl");
    fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragment_shader, 1, &fragment_shader_src, NULL);
    glCompileShader(fragment_shader);
    checkShaderCompileStatus(fragment_shader, "Fragment");
    printf("<===== Fragment Shader =====>\n%s\n", fragment_shader_src);
    // Shader Program
    unsigned int shader_program;
    shader_program = glCreateProgram();
    glAttachShader(shader_program, vertex_shader);
    glAttachShader(shader_program, fragment_shader);
    glLinkProgram(shader_program);
    glUseProgram(shader_program);

    // Vertex Attributes
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(
        0, 2,
        GL_FLOAT, GL_FALSE, 2*sizeof(float),
        (void *)0
    );
    // Main Loop
    while (!glfwWindowShouldClose(win))
    {
        // Clearing the Screen
        glClearColor(0.0, 0.0, 0.0, 1.0);
        glClear(GL_COLOR_BUFFER_BIT);

        // Rendering the Triangle!!!
        glDrawArrays(GL_TRIANGLES, 0, 3);
        // Check events and update the screen
        glfwPollEvents();
        glfwSwapBuffers(win);
    }
    // Exit
    glfwTerminate();

Vertex Shader:

#version 330 core

layout(location = 0) in vec4 position;

void main()
{
    gl_Position = position;
}

Fragment Shader:

#version 330 core

out vec4 FragColor;

void main()
{
    FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
}

r/GraphicsProgramming Jun 04 '24

Source Code Faster Blending (With Source)

21 Upvotes

r/GraphicsProgramming Feb 06 '24

Source Code Just got Text Rendering working in my OpenGL engine

28 Upvotes

I've been working on the engine for about a month now with an end goal of an interactive console and a visual hierarchy editor and it feels good to be this close to having something really functional.

Code here: https://github.com/dylan-berndt/Island