DEV Community

Cover image for JVR Jaipal's Vulkan Renderer
Jaipal001
Jaipal001

Posted on

JVR Jaipal's Vulkan Renderer

Repo JVR

Compiling

make build/jvr.so  -j8
make build/jvrc.so -j8
Enter fullscreen mode Exit fullscreen mode

Running

make test  MAIN=src.cpp SHADER=prefix # prefix.frag and prefix.vert will be picked
make testc MAIN=src.c   SHADER=prefix # prefix.frag and prefix.vert will be picked
Enter fullscreen mode Exit fullscreen mode

Example

make test MAIN=donut.cpp SHADER=donut # Spinning donut or
make test MAIN=triangle.cpp SHADER=triangle # triangle
Enter fullscreen mode Exit fullscreen mode

[!WARNING]
Please do not use C Vulkan Render, but if you do, Just Why?

Donut shader

Instead of making CPU work for calculating vertex, pass the angles to vertex shader for computation

Vertex shader

#version 450

layout(push_constant) uniform UBO {
    mat4 mvp;
    vec2 rotate; //  x = A and y = B
} ubo;

layout(location = 0) in vec2 vertex; // x = theta, y = phi
layout(location = 0) out float zDist;

void main(){
    mat3 Rx = mat3(
        vec3(1, 0, 0),
        vec3(0,  cos(ubo.rotate.x), sin(ubo.rotate.x)),
        vec3(0, -sin(ubo.rotate.x), cos(ubo.rotate.x))
    );

    mat3 Rz = mat3(
        vec3( cos(ubo.rotate.y), sin(ubo.rotate.y), 0),
        vec3(-sin(ubo.rotate.y), cos(ubo.rotate.y), 0),
        vec3(0, 0, 1)
    );

    mat3 Ry = mat3(
        vec3( cos(vertex.y), 0, sin(vertex.y)),
        vec3(       0,       1,       0      ),
        vec3(-sin(vertex.y), 0, cos(vertex.y))
    );
    vec3 circle = vec3(2 + cos(vertex.x), sin(vertex.x), 0);
    vec3 donut = Rz * Rx * Ry * circle;
    gl_Position = ubo.mvp * vec4(donut, 1);
    zDist = donut.z;
}
Enter fullscreen mode Exit fullscreen mode

Fragment shader

#version 450
layout(location = 0) in float zDist;
layout(location = 0) out vec4 FragColor;

void main() {
    // Z ranges from 3 (max/ closest) to -3 (min/ farthest)
    float brightness = clamp(zDist / 6.0 + 0.5, 0.0, 1.0);
    FragColor = vec4(vec3(brightness), 1);
}
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
arpan_singh_121 profile image
Arpan Singh •

Great work sharing the Vulkan renderer and the straightforward build steps! Could you elaborate on how you manage synchronization between command buffers within your renderer architecture?