Skip to content

gg.render #

gg/render

V implementation of sokol-gl for gg.

Call Context.destroy() before graphics shutdown to release its vertex buffer, default pipelines, image, sampler, shader, and commit listener. Repeated calls are safe. Pipelines returned by Context.make_pipeline() remain owned by the caller; release them with Context.destroy_pipeline() before destroying their shaders.

Context.load_pipeline() selects the pipeline for subsequent batches until another pipeline is loaded or a saved selection is restored with Context.pop_pipeline(). Context.current_pipeline() returns that selection so callers can save and restore it without consuming pipeline stack space. Ending a primitive batch preserves the selected pipeline.

Context.num_vertices() and Context.num_commands() report the current recording queue. Vertex counts include the extra vertices used to turn quads into triangles. Command counts include viewport and scissor commands; compatible consecutive draws can share one command. Drawing or rendering on the CPU leaves the queue intact. A graphics commit clears the queue.

Use Context.rewind() to discard an abandoned recording before calling draw(), without committing a graphics frame. It clears that context's vertices, commands, uniforms and error, ends any unfinished primitive, and resets the layer to zero. It preserves matrices and the selected color, texture and pipeline. Other contexts and the frame index are unchanged. Record the replacement geometry before starting its render pass.

Constants #

const max_stack_depth = 32
const matrixmode_modelview = MatrixMode{ value: 0 }
const matrixmode_projection = MatrixMode{ value: 1 }
const matrixmode_texture = MatrixMode{ value: 2 }
const num_primitive_types = 6

fn make_font_shader #

fn make_font_shader() gfx.Shader

make_font_shader builds the shader the text atlas is drawn with: identical vertex stage, fragment stage rewritten so a single-channel atlas's red channel acts as alpha coverage.

fn make_shader #

fn make_shader() gfx.Shader

fn mat4_from_array #

@[direct_array_access]
fn mat4_from_array(arr []f32) Mat4

fn mat4_identity #

fn mat4_identity() Mat4

fn mat4_mul #

@[direct_array_access]
fn mat4_mul(a Mat4, b Mat4) Mat4

fn new_context #

fn new_context(desc Desc) &Context

fn BlendMode.from #

fn BlendMode.from[W](input W) !BlendMode

from returns the BlendMode value of an integer or of a field name, or an error if there is none.

fn CommandKind.from #

fn CommandKind.from[W](input W) !CommandKind

from returns the CommandKind value of an integer or of a field name, or an error if there is none.

fn Error.from #

fn Error.from[W](input W) !Error

from returns the Error value of an integer or of a field name, or an error if there is none.

fn Primitive.from #

fn Primitive.from[W](input W) !Primitive

from returns the Primitive value of an integer or of a field name, or an error if there is none.

enum BlendMode #

enum BlendMode {
	none
	alpha
	add
	multiply
	screen
}

enum CommandKind #

enum CommandKind {
	draw
	viewport
	scissor
}

enum Error #

enum Error {
	no_error
	vertices_full
	commands_full
	uniforms_full
	stack_overflow
	stack_underflow
	no_context
}

enum Primitive #

enum Primitive {
	points
	lines
	line_strip
	triangles
	triangle_strip
	quads
}

struct Command #

struct Command {
pub mut:
	kind            CommandKind
	primitive       Primitive
	base_vertex     int
	num_vertices    int
	pipeline        gfx.Pipeline
	image           gfx.Image
	sampler         gfx.Sampler
	uniform_index   int
	blend_mode      BlendMode
	x               int
	y               int
	w               int
	h               int
	origin_top_left bool
	layer_id        int
}

struct Context #

@[heap]
struct Context {
	st &State
}

fn (Context) register_image #

fn (ctx &Context) register_image(id int, data voidptr, width int, height int, channels int, filter int)

fn (Context) is_image_registered #

fn (ctx &Context) is_image_registered(id int) bool

is_image_registered reports whether a CPU-side copy of id already exists.

fn (Context) update_image_region #

fn (ctx &Context) update_image_region(id int, data voidptr, x int, y int, w int, h int, channels int)

update_image_region refreshes a sub-rectangle of an already registered image. Only the CPU rasterizer uses the registry, so callers should skip this on the GPU path. It avoids copying a whole atlas for every small dirty rectangle.

fn (Context) destroy #

fn (ctx &Context) destroy()

destroy releases the context's graphics resources and CPU-side image copies. It must be called before gfx.shutdown(). Pipelines created with make_pipeline are owned by the caller and stay alive until destroy_pipeline is called for them.

fn (Context) setup #

fn (ctx &Context) setup(desc Desc)

fn (Context) defaults #

fn (ctx &Context) defaults()

fn (Context) begin #

fn (ctx &Context) begin(mode Primitive)

fn (Context) end #

fn (ctx &Context) end()

fn (Context) begin_points #

fn (ctx &Context) begin_points()

fn (Context) begin_lines #

fn (ctx &Context) begin_lines()

fn (Context) begin_line_strip #

fn (ctx &Context) begin_line_strip()

fn (Context) begin_triangles #

fn (ctx &Context) begin_triangles()

fn (Context) begin_triangle_strip #

fn (ctx &Context) begin_triangle_strip()

fn (Context) begin_quads #

fn (ctx &Context) begin_quads()

fn (Context) c1i #

fn (ctx &Context) c1i(rgba u32)

fn (Context) c3b #

fn (ctx &Context) c3b(r u8, g u8, b u8)

fn (Context) c3f #

fn (ctx &Context) c3f(r f32, g f32, b f32)

fn (Context) c4b #

fn (ctx &Context) c4b(r u8, g u8, b u8, a u8)

fn (Context) c4f #

fn (ctx &Context) c4f(r f32, g f32, b f32, a f32)

fn (Context) v2f #

fn (ctx &Context) v2f(x f32, y f32)

fn (Context) v3f #

fn (ctx &Context) v3f(x f32, y f32, z f32)

fn (Context) t2f #

fn (ctx &Context) t2f(u f32, v f32)

fn (Context) v2f_t2f #

fn (ctx &Context) v2f_t2f(x f32, y f32, u f32, v f32)

fn (Context) v2f_c4b #

fn (ctx &Context) v2f_c4b(x f32, y f32, r u8, g u8, b u8, a u8)

fn (Context) v2f_c4f #

fn (ctx &Context) v2f_c4f(x f32, y f32, r f32, g f32, b f32, a f32)

fn (Context) v2f_c3b #

fn (ctx &Context) v2f_c3b(x f32, y f32, r u8, g u8, b u8)

fn (Context) v2f_c3f #

fn (ctx &Context) v2f_c3f(x f32, y f32, r f32, g f32, b f32)

fn (Context) v2f_c1i #

fn (ctx &Context) v2f_c1i(x f32, y f32, rgba u32)

fn (Context) v3f_t2f #

fn (ctx &Context) v3f_t2f(x f32, y f32, z f32, u f32, v f32)

fn (Context) v3f_c4b #

fn (ctx &Context) v3f_c4b(x f32, y f32, z f32, r u8, g u8, b u8, a u8)

fn (Context) v3f_c4f #

fn (ctx &Context) v3f_c4f(x f32, y f32, z f32, r f32, g f32, b f32, a f32)

fn (Context) v3f_c3b #

fn (ctx &Context) v3f_c3b(x f32, y f32, z f32, r u8, g u8, b u8)

fn (Context) v3f_c3f #

fn (ctx &Context) v3f_c3f(x f32, y f32, z f32, r f32, g f32, b f32)

fn (Context) v3f_c1i #

fn (ctx &Context) v3f_c1i(x f32, y f32, z f32, rgba u32)

fn (Context) v2f_t2f_c4b #

fn (ctx &Context) v2f_t2f_c4b(x f32, y f32, u f32, v f32, r u8, g u8, b u8, a u8)

fn (Context) v2f_t2f_c4f #

fn (ctx &Context) v2f_t2f_c4f(x f32, y f32, u f32, v f32, r f32, g f32, b f32, a f32)

fn (Context) v2f_t2f_c3b #

fn (ctx &Context) v2f_t2f_c3b(x f32, y f32, u f32, v f32, r u8, g u8, b u8)

fn (Context) v2f_t2f_c3f #

fn (ctx &Context) v2f_t2f_c3f(x f32, y f32, u f32, v f32, r f32, g f32, b f32)

fn (Context) v2f_t2f_c1i #

fn (ctx &Context) v2f_t2f_c1i(x f32, y f32, u f32, v f32, rgba u32)

fn (Context) v3f_t2f_c4b #

fn (ctx &Context) v3f_t2f_c4b(x f32, y f32, z f32, u f32, v f32, r u8, g u8, b u8, a u8)

fn (Context) v3f_t2f_c4f #

fn (ctx &Context) v3f_t2f_c4f(x f32, y f32, z f32, u f32, v f32, r f32, g f32, b f32, a f32)

fn (Context) v3f_t2f_c3b #

fn (ctx &Context) v3f_t2f_c3b(x f32, y f32, z f32, u f32, v f32, r u8, g u8, b u8)

fn (Context) v3f_t2f_c3f #

fn (ctx &Context) v3f_t2f_c3f(x f32, y f32, z f32, u f32, v f32, r f32, g f32, b f32)

fn (Context) v3f_t2f_c1i #

fn (ctx &Context) v3f_t2f_c1i(x f32, y f32, z f32, u f32, v f32, rgba u32)

fn (Context) matrix_mode_modelview #

fn (ctx &Context) matrix_mode_modelview()

fn (Context) matrix_mode_projection #

fn (ctx &Context) matrix_mode_projection()

fn (Context) matrix_mode_texture #

fn (ctx &Context) matrix_mode_texture()

fn (Context) load_identity #

fn (ctx &Context) load_identity()

fn (Context) load_matrix #

fn (ctx &Context) load_matrix(m []f32)

fn (Context) load_transpose_matrix #

fn (ctx &Context) load_transpose_matrix(m []f32)

fn (Context) mult_matrix #

fn (ctx &Context) mult_matrix(m []f32)

fn (Context) mult_transpose_matrix #

fn (ctx &Context) mult_transpose_matrix(m []f32)

fn (Context) push_matrix #

fn (ctx &Context) push_matrix()

fn (Context) pop_matrix #

fn (ctx &Context) pop_matrix()

fn (Context) translate #

fn (ctx &Context) translate(x f32, y f32, z f32)

fn (Context) scale #

fn (ctx &Context) scale(x f32, y f32, z f32)

fn (Context) rotate #

fn (ctx &Context) rotate(angle f32, x f32, y f32, z f32)

fn (Context) ortho #

fn (ctx &Context) ortho(l f32, r f32, b f32, t f32, n f32, f f32)

fn (Context) frustum #

fn (ctx &Context) frustum(l f32, r f32, b f32, t f32, n f32, f f32)

fn (Context) perspective #

fn (ctx &Context) perspective(fov_y f32, aspect f32, z_near f32, z_far f32)

fn (Context) lookat #

fn (ctx &Context) lookat(eye_x f32, eye_y f32, eye_z f32, center_x f32, center_y f32, center_z f32, up_x f32, up_y f32, up_z f32)

fn (Context) point_size #

fn (ctx &Context) point_size(s f32)

fn (Context) texture #

fn (ctx &Context) texture(img gfx.Image, smp gfx.Sampler)

fn (Context) enable_texture #

fn (ctx &Context) enable_texture()

fn (Context) disable_texture #

fn (ctx &Context) disable_texture()

fn (Context) scissor_rect #

fn (ctx &Context) scissor_rect(x int, y int, w int, h int, origin_top_left bool)

fn (Context) scissor_rectf #

fn (ctx &Context) scissor_rectf(x f32, y f32, w f32, h f32, origin_top_left bool)

fn (Context) viewport #

fn (ctx &Context) viewport(x int, y int, w int, h int, origin_top_left bool)

fn (Context) viewportf #

fn (ctx &Context) viewportf(x f32, y f32, w f32, h f32, origin_top_left bool)

fn (Context) layer #

fn (ctx &Context) layer(id int)

fn (Context) draw #

fn (ctx &Context) draw()

fn (Context) rewind #

fn (ctx &Context) rewind()

rewind discards this context's recording before draw(), without committing a graphics frame. It preserves drawing state, resets the layer to zero, and leaves other contexts and the frame index unchanged.

fn (Context) num_vertices #

fn (ctx &Context) num_vertices() int

num_vertices returns the number of vertices recorded since the last commit or rewind, like sgl_num_vertices.

fn (Context) num_commands #

fn (ctx &Context) num_commands() int

num_commands returns the number of commands recorded since the last commit or rewind, like sgl_num_commands.

fn (Context) error #

fn (ctx &Context) error() Error

fn (Context) rad #

fn (ctx &Context) rad(deg f32) f32

fn (Context) deg #

fn (ctx &Context) deg(rad f32) f32

fn (Context) render_cpu #

fn (ctx &Context) render_cpu(width int, height int, clear u32) Frame

fn (Context) make_pipeline #

fn (ctx &Context) make_pipeline(desc gfx.PipelineDesc) Pipeline

fn (Context) make_pipeline_with_shader #

fn (ctx &Context) make_pipeline_with_shader(desc gfx.PipelineDesc, shader gfx.Shader) Pipeline

make_pipeline_with_shader builds a pipeline around shader instead of the shared sgl shader. The text atlas needs this: its fragment stage treats the single-channel atlas's red channel as alpha, which the generic shader's plain texture * color multiply renders as red glyphs and black quads.

fn (Context) current_pipeline #

fn (ctx &Context) current_pipeline() Pipeline

current_pipeline returns the pipeline selected for subsequent drawing.

fn (Context) load_pipeline #

fn (ctx &Context) load_pipeline(p Pipeline)

fn (Context) load_default_pipeline #

fn (ctx &Context) load_default_pipeline()

fn (Context) push_pipeline #

fn (ctx &Context) push_pipeline()

fn (Context) pop_pipeline #

fn (ctx &Context) pop_pipeline()

fn (Context) destroy_pipeline #

fn (ctx &Context) destroy_pipeline(p Pipeline)

struct Desc #

struct Desc {
pub mut:
	max_vertices int
	max_commands int
	face_winding gfx.FaceWinding = .ccw
	// sample_count must match the render pass the context draws into: the window's
	// framebuffer sample count for the default pass (0 falls back to 1).
	sample_count int
}

struct Frame #

struct Frame {
pub mut:
	width  int
	height int
	pixels []u32
}

fn (Frame) save_ppm #

fn (f Frame) save_ppm(path string)

struct ImageData #

struct ImageData {
pub mut:
	data     voidptr
	pixels   []u8
	width    int
	height   int
	channels int
	filter   int
}

struct Mat4 #

struct Mat4 {
pub mut:
	v [4][4]f32
}

fn (Mat4) translate #

@[direct_array_access]
fn (mut m Mat4) translate(x f32, y f32, z f32)

fn (Mat4) scale #

@[direct_array_access]
fn (mut m Mat4) scale(x f32, y f32, z f32)

fn (Mat4) rotate #

@[direct_array_access]
fn (mut m Mat4) rotate(angle f32, x0 f32, y0 f32, z0 f32)

fn (Mat4) ortho #

@[direct_array_access]
fn (mut m Mat4) ortho(l f32, r f32, b f32, t f32, n f32, f f32)

fn (Mat4) frustum #

@[direct_array_access]
fn (mut m Mat4) frustum(l f32, r f32, b f32, t f32, n f32, f f32)

fn (Mat4) perspective #

@[direct_array_access]
fn (mut m Mat4) perspective(fovy f32, aspect f32, znear f32, zfar f32)

fn (Mat4) lookat #

@[direct_array_access]
fn (mut m Mat4) lookat(eye_x f32, eye_y f32, eye_z f32, center_x f32, center_y f32, center_z f32, up_x f32, up_y f32, up_z f32)

fn (Mat4) to_array #

@[direct_array_access]
fn (m &Mat4) to_array() [16]f32

struct MatrixMode #

struct MatrixMode {
pub:
	value int
}

struct Pipeline #

struct Pipeline {
pub mut:
	id         int
	blend_mode BlendMode
	pips       [num_primitive_types]gfx.Pipeline
}

struct State #

struct State {
pub mut:
	max_vertices      int
	max_commands      int
	face_winding      gfx.FaceWinding
	verts             []Vertex
	commands          []Command
	uniforms          []Uniform
	vbuf              gfx.Buffer
	vertex_count      int
	vtx_count         int
	base_vertex       int
	commands_count    int
	uniforms_count    int
	error             Error
	u                 f32
	vv                f32
	rgba              u32 = 0xffffffff
	point_size        f32 = 1.0
	texturing_enabled bool
	cur_img           gfx.Image
	cur_smp           gfx.Sampler
	pip_stack         [max_stack_depth]Pipeline
	pip_tos           int
	def_pip           Pipeline
	matrix_stack      [3][max_stack_depth]Mat4
	matrix_tos        [3]int
	cur_matrix_mode   MatrixMode
	matrix_dirty      bool = true
	frame_id          int
	update_frame_id   int = -1
	layer_id          int
	in_begin          bool
	cur_prim          Primitive
	def_img           gfx.Image
	def_smp           gfx.Sampler
	shader            gfx.Shader
	bindings          gfx.Bindings
	image_data        map[int]ImageData
	next_pipeline_id  int = 1
	sample_count      int = 1
}

struct Uniform #

struct Uniform {
pub mut:
	mvp Mat4
	tm  Mat4
}

struct Vertex #

struct Vertex {
pub mut:
	x     f32
	y     f32
	z     f32
	u     f32
	v     f32
	rgba  u32
	psize f32
}