Harbor

branch main
showing the latest snapshot on main
types.odin 5.6 KB · Plain text
gpu/types.odin 0644 Raw
package gpu

import glsl "core:math/linalg/glsl"
import bk "backend"

// Math type aliases
Vec2 :: glsl.vec2
Vec3 :: glsl.vec3
Vec4 :: glsl.vec4
Mat4 :: glsl.mat4x4
Quat :: quaternion128

// Color as 4 normalized floats (GPU-native, no conversion needed)
Color :: [4]f32

// Rectangle for 2D operations
Rectangle :: struct {
	x, y, width, height: f32,
}

// 2D camera with offset, target, rotation, and zoom
Camera2D :: struct {
	offset:   Vec2,
	target:   Vec2,
	rotation: f32,
	zoom:     f32,
}

// 3D camera with position, target, up vector, and projection params
Camera3D :: struct {
	position: Vec3,
	target:   Vec3,
	up:       Vec3,
	fovy:     f32,
	near:     f32,
	far:      f32,
}

Camera_Mode :: enum {
	FREE,
	ORBITAL,
	FIRST_PERSON,
	THIRD_PERSON,
}

// Easing functions for smooth camera transitions and animations
Ease_Type :: enum {
	LINEAR,
	SINE_IN,
	SINE_OUT,
	SINE_IN_OUT,
	QUAD_IN,
	QUAD_OUT,
	QUAD_IN_OUT,
	CUBIC_IN,
	CUBIC_OUT,
	CUBIC_IN_OUT,
	EXPO_IN,
	EXPO_OUT,
	EXPO_IN_OUT,
}

// Camera path keyframes for scripted camera movement (cutscenes)
Camera_Keyframe_2D :: struct {
	camera: Camera2D,
	time:   f32,       // absolute seconds from path start
	easing: Ease_Type, // easing TO this keyframe from previous
}

Camera_Keyframe_3D :: struct {
	camera: Camera3D,
	time:   f32,
	easing: Ease_Type,
}

Camera_Path_2D :: struct {
	keyframes: []Camera_Keyframe_2D, // user-owned slice
	duration:  f32,                   // last keyframe time
}

Camera_Path_3D :: struct {
	keyframes: []Camera_Keyframe_3D,
	duration:  f32,
}

// GPU vertex layout
Vertex :: struct {
	position:  Vec3,
	normal:    Vec3,
	tex_coord: Vec2,
	color:     Color,
}

// Opaque resource handles -- IDs are indices into internal pools
Texture :: struct {
	id:     u32,
	width:  i32,
	height: i32,
}

Mesh :: struct {
	id:           u32,
	vertex_count: i32,
	index_count:  i32,
}

Material :: struct {
	shader:       Shader,
	diffuse:      Texture,
	normal_map:   Texture,
	color:        Color,
	double_sided: bool,
	metallic:     f32,
	roughness:    f32,
	reflectance:  f32,
	emissive:     Color,
}

// Light types for the lighting system
Light_Type :: enum u32 {
	DIRECTIONAL = 0,
	POINT       = 1,
}

// Light source for 3D PBR and 2D screen-space lighting
Light :: struct {
	type:         Light_Type,
	enabled:      bool,
	position:     Vec3,   // world position (point) or direction (directional)
	color:        Color,  // RGB color
	intensity:    f32,    // brightness multiplier
	radius:       f32,    // point light range (attenuation cutoff)
	casts_shadow: bool,   // directional lights only
}

Model :: struct {
	id:        u32,
	meshes:    []Mesh,
	materials: []Material,
	transform: Mat4,
}

Shader :: struct {
	id: u32,
}

Compute_Shader :: struct {
	id: u32,
}

Storage_Buffer :: struct {
	id:   u32,
	size: int,
}

// Helper to create Color from u8 RGBA values
color_from_rgba :: proc(r, g, b, a: u8) -> Color {
	return Color{f32(r) / 255.0, f32(g) / 255.0, f32(b) / 255.0, f32(a) / 255.0}
}

// Default camera constructors
default_camera_2d :: proc() -> Camera2D {
	return Camera2D{
		offset   = {0, 0},
		target   = {0, 0},
		rotation = 0,
		zoom     = 1,
	}
}

default_camera_3d :: proc() -> Camera3D {
	return Camera3D{
		position = {0, 2, 5},
		target   = {0, 0, 0},
		up       = {0, 1, 0},
		fovy     = 60,
		near     = 0.1,
		far      = 100,
	}
}

// Backend types re-exported for external pipeline creation
Backend            :: bk.Backend
Capability         :: bk.Capability
Capability_Flags   :: bk.Capability_Flags
Capabilities       :: bk.Capabilities
Frame_Context      :: bk.Frame_Context
Pipeline_Handle    :: bk.Pipeline_Handle
Buffer_Handle      :: bk.Buffer_Handle
Texture_Handle     :: bk.Texture_Handle
Shader_Handle      :: bk.Shader_Handle
Shader_Format      :: bk.Shader_Format
Shader_Module_Desc :: bk.Shader_Module_Desc
REQUIRED_SHADER_FORMAT :: bk.REQUIRED_SHADER_FORMAT
Descriptor_Handle  :: bk.Descriptor_Handle
Render_Pass_Handle :: bk.Render_Pass_Handle
Surface_Kind       :: bk.Surface_Kind
Surface_Desc       :: bk.Surface_Desc
X11_Surface_Handle :: bk.X11_Surface_Handle
Wayland_Surface_Handle :: bk.Wayland_Surface_Handle
Win32_Surface_Handle :: bk.Win32_Surface_Handle
Shader_Stage       :: bk.Shader_Stage
Shader_Stage_Flag  :: bk.Shader_Stage_Flag
Shader_Stage_Flags :: bk.Shader_Stage_Flags
Pipeline_Desc      :: bk.Pipeline_Desc
Blend_Mode         :: bk.Blend_Mode
Vertex_Binding     :: bk.Vertex_Binding
Vertex_Attribute   :: bk.Vertex_Attribute
Vertex_Format      :: bk.Vertex_Format
Vertex_Input_Rate  :: bk.Vertex_Input_Rate
Vertex_Attrib      :: bk.Vertex_Attrib
Vertex_Layout_Entry :: bk.Vertex_Layout_Entry
Vertex_Layout      :: bk.Vertex_Layout
MAX_VERTEX_ATTRIBS :: bk.MAX_VERTEX_ATTRIBS

// Layout variant enum and helpers
Layout_Variant     :: bk.Layout_Variant
MAX_LAYOUT_VARIANTS :: bk.MAX_LAYOUT_VARIANTS
layout_to_variant  :: bk.layout_to_variant
variant_to_layout  :: bk.variant_to_layout

// Predefined vertex layouts
LAYOUT_POS_NORM                :: bk.LAYOUT_POS_NORM
LAYOUT_POS_NORM_UV             :: bk.LAYOUT_POS_NORM_UV
LAYOUT_POS_NORM_UV_COLOR       :: bk.LAYOUT_POS_NORM_UV_COLOR
LAYOUT_POS_NORM_UV_TANGENT     :: bk.LAYOUT_POS_NORM_UV_TANGENT
LAYOUT_POS_NORM_UV_COLOR_TANGENT :: bk.LAYOUT_POS_NORM_UV_COLOR_TANGENT

// Vertex layout helpers
vertex_format_size             :: bk.vertex_format_size
vertex_attrib_default_format   :: bk.vertex_attrib_default_format
vertex_layout_build            :: bk.vertex_layout_build
vertex_layout_has              :: bk.vertex_layout_has
vertex_layout_to_pipeline_attrs :: bk.vertex_layout_to_pipeline_attrs

Topology           :: bk.Topology
Cull_Mode          :: bk.Cull_Mode
Front_Face         :: bk.Front_Face
NULL_PIPELINE      :: bk.NULL_PIPELINE