Files
Ink-Shader-Language/README.md
2025-09-14 20:21:43 +02:00

4.3 KiB

Shader Compiler

A compiler for a custom, platform-agnostic shader language that compiles to platform specific shader languages (currently only HLSL).

Features

A simple passthrough shader (passthrough.shd) can look as follows

vertex main :: (position : float4 @position) -> float4 {
	return position;
}

pixel main :: () -> float4 @target {
	return float4(1, 1, 1, 1);
}

The shader contains both vertex and pixel shader code, where you specify the entry points as above. Entry points can have any and overlapping names, which will be exposed in the meta data of a compiled shader. There is basic support for most HLSL built-in math functions for the following types:

  • Scalar types: int, float
  • Vector types: float2, float3, float4, int2, int3, int4
  • Matrices: float4x4 All of the above can be constructed with their namesake constructors i.e. float4(x, y, z, w);. We also support Samplers and Texture2D

If you want to declare and use variables you can do it as follows

x : float  = 2.0; // no 'f' suffix required or even supported (it gives an error)
y : float  = 4.0;
v : float2 = float2(x, y);
v2 := float2(x, y);

You can also do arithmetic as you would expect

x : float = 2.0 * 4.0 + (3 * 2); // int literals automatically convert to floats.

There is basic struct support

Camera_Data :: struct {
	projection : float4x4;
	view : float4x4;
}

And there is a special struct called properties, which is used for custom data you want to pass in.

** Note: Properties will likely be deprecated, since the language now supports @ hints to easily mark buffers and values with metadata.**

properties {
	projection : float4x4;
	view : float4x4;
}

which will be exposed in the compiled result. properties can be renamed to a custom/shorter name like

p :: properties {
	...


You can also define constant buffers

camera :: constant_buffer {
	projection : float4x4;
	view       : float4x4;
}

Jai Usage Example

To compile a shader and use the result, you can do the following in jai


// In the future, you can pass environment defines to the compiler.
ctx : Compiler_Context;
compile_file(*compiler, "shader.shd", allocator);

if ctx.had_error {
	log_error("%\n", report_messages(ctx.messages),, temp);
	return;
}

// The ctx now contains all the needed information like the source text, entry points, constant buffers etc.

When parsing a shader you get the following struct as a result

Compiler_Context :: struct {
	file : Input_File;

	environment : Environment;
	
	tokens : [..]Token;;
	root  : *AST_Node;
	nodes : [..]AST_Node;

	codegen_result_text : string;

	constant_buffers : Static_Array(Type_Variable_Handle, 16);

	scope_stack : Scope_Stack;
	type_variables : [..]Type_Variable;

	property_name : string;

	vertex_entry_point : struct {
		node : *AST_Node;
		name : string;
		input : [..]Field;
	}

	pixel_entry_point : struct {
		node : *AST_Node;
		name : string;
		return_value : Field;
	}

	properties : Properties;

	max_constant_buffers :: 16;

	cbuffers : Static_Array(Constant_Buffer, max_constant_buffers);

	had_error : bool;
	messages : [..]Compiler_Message;
}

Constant_Buffer :: struct {
	name : string;

	fields : Static_Array(Property_Field, 16);

	// hints : Field_Hint; // optional hint...
	hints : [..]Field_Hint;

	buffer_index : u32;
}

Properties :: struct {
	fields : [..]Property_Field;

	buffer_index : u32;
}

A field is just a simple struct with a name and type (and hints such as semantics or custom hints in the future)

Field_Hint :: struct {
	kind : Hint_Kind;

	target_index : int;
	custom_hint_name : string;
}

Field :: struct {
	name : string;

	type : Field_Type;
	hints : [..]Field_Hint;
}

Field_Kind :: enum {
	Int       :: 0;
	Half      :: 1;
	Float     :: 2;
	Double    :: 3;
	Texture2D :: 8;
	Sampler   :: 9;

	Function;
	Struct;
	Array; // Not yet supported
}

Field_Type :: struct {
	kind : Field_Kind;

	name : string; //@Note(niels): for structs

	children : [..]Field;
}

Hint_Kind :: enum {
	None;
	
	Position;
	Target;

	Custom;
}

Notable missing features

  • While
  • Arrays
  • Multiple render targets
  • Custom buffers/structured buffers
  • Interpolation specifiers
  • Proper variant handling with environment defines
  • Importing files such as shared utils etc. with something other than textual #load