Kraken Logo

Kraken Engine

DocumentationGuidesShowcaseCommunity
Ctrl
K
Installation
Creating a Window
What is a Shader?
Using Shaders
Uniforms
Texture Samplers

Built bydurkisneer1.Kraken Engine is open source and available onGitHub.

  1. Guides
  2. Implementing Shaders
  3. Using Shaders

Using Shaders

Guide to loading and applying fragment shaders in Kraken.

Kraken shaders are loaded with kn.shaders.Shader. A shader object owns the compiled fragment shader plus the render state needed to use it for later draw calls.

import pykraken as kn

invert_shader = kn.shaders.Shader(
    "assets/shaders/invert.frag",
    uniform_buffer_count=0,
    sampler_count=1,
)

Pass the shader base path to Shader, not a source file path. For example, if your baked outputs include assets/shaders/invert.frag.spv, pass "assets/shaders/invert.frag". Kraken picks the platform file it needs at runtime.

Bake HLSL Shaders

The recommended workflow is:

  1. Write the fragment shader in HLSL.
  2. Bake it with pykraken bake.
  3. Load the baked shader by base path with Shader.
  4. Bind any texture samplers and uniforms before drawing with it.
pykraken bake shaders/invert.frag.hlsl -o assets/shaders

The CLI accepts one input file and an optional output directory:

pykraken bake [-h] [-o OUT] [-v] input

Note:

pykraken bake currently works for HLSL only. GLSL shaders are still allowed, but you must compile them yourself and place the compiled output where Shader can load it by base path.

A One-Texture Fragment Shader

This shader inverts the color of the texture being drawn. It uses one texture sampler and no uniforms.

invert.frag.hlsl
Texture2D spriteTex : register(t0, space2);
SamplerState spriteSamp : register(s0, space2);

struct PSInput {
    float4 v_color : COLOR0;
    float2 v_uv    : TEXCOORD0;
};

struct PSOutput {
    float4 o_color : SV_Target;
};

PSOutput main(PSInput input) {
    float4 color = spriteTex.Sample(spriteSamp, input.v_uv) * input.v_color;
    color.rgb = 1.0 - color.rgb;

    PSOutput output;
    output.o_color = color;
    return output;
}

Load it with one sampler slot:

invert_shader = kn.shaders.Shader(
    "assets/shaders/invert.frag",
    uniform_buffer_count=0,
    sampler_count=1,
)

Since the only texture and sampler we have are in binding 0, we do not need to explicitly set it in code or make the texture shader-sampled. The texture passed to kn.renderer.draw is already available to the shader as t0/s0.

sprite_texture = kn.Texture("assets/player.png")

For extra textures beyond t0/s0, use set_texture_sampler. That more advanced workflow is covered in Texture Samplers.

Drawing With a Shader

bind() makes the shader active for later draw calls. unbind() returns rendering to the default shader state.

invert_shader.bind()
kn.renderer.draw(sprite_texture)
invert_shader.unbind()

kn.renderer.draw(sprite_texture)  # Draws normally again.

Bind the shader as tightly as possible around the draw calls that need it. That keeps later rendering from accidentally inheriting the effect.

PreviousWhat is a Shader?
NextUniforms

On this page

Bake HLSL ShadersA One-Texture Fragment ShaderDrawing With a Shader