Documentation ¶
Overview ¶
bk-api provide low-level graphics api.
Index ¶
- Constants
- Variables
- func Compile(vertex, fragment string) (uint32, error)
- func Destroy()
- func Dump()
- func Flush() int
- func GetErrors() string
- func Init()
- func Reset(width, height uint32, pixelRatio float32)
- func ResetDrawCall()
- func SetDebug(debug uint32)
- func SetIndexBuffer(id uint16, firstIndex, num uint32)
- func SetScissor(x, y, width, height uint16) uint16
- func SetScissorCached(id uint16)
- func SetState(state uint64, rgba uint32)
- func SetStencil(stencil uint32)
- func SetTexture(stage uint8, sampler uint16, handle uint16, flags uint32)
- func SetTransform(mtx *f32.Mat4)
- func SetUniform(id uint16, ptr unsafe.Pointer)
- func SetVertexBuffer(stream uint8, id uint16, firstVertex, numVertex uint32)
- func SetViewClear(id uint8, flags uint16, rgba uint32, depth float32, stencil uint8)
- func SetViewPort(id uint8, x, y, width, height uint16)
- func SetViewScissor(id uint8, x, y, width, height uint16)
- func SetViewTransform(id uint8, view, proj *f32.Mat4, flags uint8)
- func Submit(id uint8, program uint16, depth int32) uint32
- func Touch(id uint8) uint32
- func Uniform_decode(code uint32, uType, loc, size, num *uint8)
- func Uniform_encode(uType UniformType, loc, size, num uint8) uint32
- type AttrType
- type AttribBind
- type ByKeyAscending
- type ByKeyDescending
- type FreeList
- type GLShader
- func (s *GLShader) Create(vsh, fsh string) error
- func (s *GLShader) Destroy()
- func (s *GLShader) GetAttrLocation(attr string) uint32
- func (s *GLShader) GetUniformLocation(uniform string) uint32
- func (s *GLShader) SetFloat(name string, value float32)
- func (s *GLShader) SetInteger(name string, value int32)
- func (s *GLShader) SetMatrix4(name string, mat4 f32.Mat4)
- func (s *GLShader) SetVec4fArray(name string, array []float32, count int32)
- func (s *GLShader) SetVector2f(name string, x, y float32)
- func (s *GLShader) SetVector3f(name string, x, y, z float32)
- func (s *GLShader) SetVector4f(name string, x, y, z, w float32)
- func (s *GLShader) Use()
- type IndexBuffer
- type Memory
- type Rect
- type RenderContext
- type RenderDraw
- type RenderQueue
- func (rq *RenderQueue) Destroy()
- func (rq *RenderQueue) Flush() int
- func (rq *RenderQueue) Init()
- func (rq *RenderQueue) Reset(w, h uint16, pr float32)
- func (rq *RenderQueue) SetIndexBuffer(id uint16, firstIndex, num uint16)
- func (rq *RenderQueue) SetScissor(x, y, width, height uint16) (id uint16)
- func (rq *RenderQueue) SetScissorCached(id uint16)
- func (rq *RenderQueue) SetState(state uint64, rgba uint32)
- func (rq *RenderQueue) SetStencil(stencil uint32)
- func (rq *RenderQueue) SetTexture(stage uint8, samplerId uint16, texId uint16, flags uint32)
- func (rq *RenderQueue) SetTransform(mtx *f32.Mat4)
- func (rq *RenderQueue) SetUniform(id uint16, ptr unsafe.Pointer)
- func (rq *RenderQueue) SetVertexBuffer(stream uint8, id uint16, firstVertex, numVertex uint16)
- func (rq *RenderQueue) SetViewClear(id uint8, flags uint16, rgba uint32, depth float32, stencil uint8)
- func (rq *RenderQueue) SetViewPort(id uint8, x, y, width, height uint16)
- func (rq *RenderQueue) SetViewScissor(id uint8, x, y, with, height uint16)
- func (rq *RenderQueue) SetViewTransform(id uint8, view, proj *f32.Mat4, flags uint8)
- func (rq *RenderQueue) Submit(id uint8, program uint16, depth int32) uint32
- type ResManager
- func (rm *ResManager) AllocIndexBuffer(mem Memory) (id uint16, ib *IndexBuffer)
- func (rm *ResManager) AllocShader(vsh, fsh string) (id uint16, sh *Shader)
- func (rm *ResManager) AllocTexture(img image.Image) (id uint16, tex *Texture2D)
- func (rm *ResManager) AllocUniform(shId uint16, name string, xType UniformType, num uint32) (id uint16, um *Uniform)
- func (rm *ResManager) AllocVertexBuffer(mem Memory, stride uint16) (id uint16, vb *VertexBuffer)
- func (rm *ResManager) Destroy()
- func (rm *ResManager) Free(id uint16)
- func (rm *ResManager) IndexBuffer(id uint16) (ok bool, ib *IndexBuffer)
- func (rm *ResManager) Init()
- func (rm *ResManager) Shader(id uint16) (ok bool, sh *Shader)
- func (rm *ResManager) Texture(id uint16) (ok bool, tex *Texture2D)
- func (rm *ResManager) Uniform(id uint16) (ok bool, um *Uniform)
- func (rm *ResManager) VertexBuffer(id uint16) (ok bool, vb *VertexBuffer)
- type Shader
- type SortKey
- type SortMode
- type Stream
- type SubTex
- type Texture2D
- type Uniform
- type UniformBuffer
- func (ub *UniformBuffer) Copy(ptr unsafe.Pointer, size uint32)
- func (ub *UniformBuffer) GetPos() uint32
- func (ub *UniformBuffer) IsEmpty() bool
- func (ub *UniformBuffer) ReadPointer(size uint32) unsafe.Pointer
- func (ub *UniformBuffer) ReadUInt32() uint32
- func (ub *UniformBuffer) Reset()
- func (ub *UniformBuffer) Seek(pos uint32)
- func (ub *UniformBuffer) WriteUInt32(value uint32)
- type UniformType
- type VertexBuffer
- type VertexComp
Constants ¶
const ( IdMask uint16 = 0x0FFF IdTypeShift = 12 )
const ( IdTypeIndex uint16 = iota IdTypeVertex IdTypeTexture IdTypeLayout IdTypeUniform IdTypeShader )
ID FORMAT 0x F FFF
^ ^ | +---- id value +--------- id type
const ( MaxIndex = 2 << 10 MaxVertex = 2 << 10 MaxTexture = 1 << 10 MaxUniform = 32 * 8 MaxShader = 32 )
const ( DebugResMan uint32 = 0x000000001 DebugQueue uint32 = 0x000000002 )
const ( SK_LayerMask uint32 = 0xF0000000 SK_ShaderMask uint32 = 0x0F000000 SK_BlendMask uint32 = 0x00F00000 SK_TextureMask uint32 = 0x000FF000 )
const InvalidId uint16 = 0x0000
const MAX_QUEUE_SIZE = 8 << 10
~ 8000 draw call
const UInt16Max uint16 = 0xFFFF
const (
UNIFORM_BUFFER_SIZE = 16 << 10
)
Variables ¶
var MAX = struct {
}{}
//// MAX SIZE
var P2C4 = []VertexComp{ {2, AttrFloat, 0, 0}, {4, AttrUInt8, 8, 1}, }
<x,y, color>
var P2T2 = []VertexComp{ {2, AttrFloat, 0, 0}, {2, AttrFloat, 8, 0}, }
<x,y, u,v>
var P2T2C4 = []VertexComp{ {2, AttrFloat, 0, 0}, {2, AttrFloat, 8, 0}, {4, AttrUInt8, 16, 1}, }
useful defines <x,y, u,v, color>
var ST = struct { RGB_WRITE uint64 ALPHA_WRITE uint64 DEPTH_WRITE uint64 DEPTH_TEST_MASK uint64 DEPTH_TEST_SHIFT uint64 BLEND_MASK uint64 BLEND_SHIFT uint64 PT_MASK uint64 PT_SHIFT uint64 }{ RGB_WRITE: 0x0000000000000001, ALPHA_WRITE: 0x0000000000000002, DEPTH_WRITE: 0x0000000000000004, DEPTH_TEST_MASK: 0x00000000000000F0, DEPTH_TEST_SHIFT: 4, BLEND_MASK: 0x0000000000000F00, BLEND_SHIFT: 8, PT_MASK: 0x000000000000F000, PT_SHIFT: 12, }
var ST_BLEND = struct { DEFAULT uint64 ISABLE uint64 ALPHA_PREMULTIPLIED uint64 ALPHA_NON_PREMULTIPLIED uint64 ADDITIVE uint64 }{ ISABLE: 0x0000000000000100, ALPHA_PREMULTIPLIED: 0x0000000000000200, ALPHA_NON_PREMULTIPLIED: 0x0000000000000300, ADDITIVE: 0x0000000000000400, }
zero means no blend
var ST_DEPTH = struct { LESS uint64 LEQUAL uint64 EQUAL uint64 GEQUAL uint64 GREATER uint64 NOTEQUAL uint64 NEVER uint64 ALWAYS uint64 }{ LESS: 0x0000000000000010, LEQUAL: 0x0000000000000020, EQUAL: 0x0000000000000030, GEQUAL: 0x0000000000000040, GREATER: 0x0000000000000050, NOTEQUAL: 0x0000000000000060, NEVER: 0x0000000000000070, ALWAYS: 0x0000000000000080, }
zero means no depth-test
Functions ¶
func SetDebug ¶
func SetDebug(debug uint32)
SetDebug set debug Flags, DebugResMan enable ResourceManager's log output and DebugQueue enable RenderQueue's log output.
func SetIndexBuffer ¶
SetIndexBuffer sets index buffer for drawCall primitive.
func SetScissor ¶
SetScissor set scissor for drawCall primitive. Return a cached ScissorRect handler.
func SetScissorCached ¶
func SetScissorCached(id uint16)
SetScissorCached set scissor rect with a cached ScissorRect handler.
func SetState ¶
SetState set render's states for drawCall primitive. State flags is defined by ST_BLEND.
func SetTexture ¶
SetSprite sets texture stages for drawCall primitive.
func SetUniform ¶
SetUniform sets shader uniform parameter for drawCall primitive.
func SetVertexBuffer ¶
SetVertexBuffer sets vertex buffer for drawCall primitive.
func SetViewClear ¶
Set view clear Flags. rgba is Color clear value(default = 0x000000ff), depth is Depth clear value (default = 1.0), stencil is Stencil clear value(default = 0).
func SetViewPort ¶
func SetViewScissor ¶
SetViewScissor view scissor. Draw primitive outsize view will be clipped. When x, y, with, height are set to 0, scissor will be disabled.
func SetViewTransform ¶
SetViewTransform sets view and projection matrices, all drawCall primitives in this view will use these matrices.
func Submit ¶
Submit primitive for rendering. Default depth is zero. Returns Number of drawCall calls.
func Touch ¶
Submit an empty primitive for rendering. Uniforms and drawCall state will be applied but no geometry will be submitted. These empty drawCall calls will sort before ordinary drawCall calls.
func Uniform_decode ¶
func Uniform_encode ¶
func Uniform_encode(uType UniformType, loc, size, num uint8) uint32
Types ¶
type AttribBind ¶
type AttribBind struct {
// contains filtered or unexported fields
}
type ByKeyAscending ¶
type ByKeyAscending struct {
// contains filtered or unexported fields
}
sort draw-call based on SortKey
func (ByKeyAscending) Len ¶
func (a ByKeyAscending) Len() int
func (ByKeyAscending) Less ¶
func (a ByKeyAscending) Less(i, j int) bool
func (ByKeyAscending) Swap ¶
func (a ByKeyAscending) Swap(i, j int)
type ByKeyDescending ¶
type ByKeyDescending struct {
// contains filtered or unexported fields
}
func (ByKeyDescending) Len ¶
func (a ByKeyDescending) Len() int
func (ByKeyDescending) Less ¶
func (a ByKeyDescending) Less(i, j int) bool
func (ByKeyDescending) Swap ¶
func (a ByKeyDescending) Swap(i, j int)
type GLShader ¶
type GLShader struct {
Program uint32
}
func (*GLShader) GetAttrLocation ¶
func (*GLShader) GetUniformLocation ¶
func (*GLShader) SetInteger ¶
func (*GLShader) SetVec4fArray ¶
func (*GLShader) SetVector2f ¶
func (*GLShader) SetVector3f ¶
func (*GLShader) SetVector4f ¶
type IndexBuffer ¶
type IndexBuffer struct { Id uint32 // contains filtered or unexported fields }
func (*IndexBuffer) Destroy ¶
func (ib *IndexBuffer) Destroy()
type RenderContext ¶
type RenderContext struct { // data R *ResManager // contains filtered or unexported fields }
func NewRenderContext ¶
func NewRenderContext(r *ResManager, ub *UniformBuffer) *RenderContext
func (*RenderContext) AddClipRect ¶
func (ctx *RenderContext) AddClipRect(x, y, w, h uint16) uint16
func (*RenderContext) Draw ¶
func (ctx *RenderContext) Draw(sortKeys []uint64, sortValues []uint16, drawList []RenderDraw)
func (*RenderContext) Init ¶
func (ctx *RenderContext) Init()
func (*RenderContext) Reset ¶
func (ctx *RenderContext) Reset()
Reset OpenGL state, then each frame has same starting state.
func (*RenderContext) Shutdown ¶
func (ctx *RenderContext) Shutdown()
type RenderDraw ¶
type RenderDraw struct {
// contains filtered or unexported fields
}
type RenderQueue ¶
type RenderQueue struct { SortMode // contains filtered or unexported fields }
func NewRenderQueue ¶
func NewRenderQueue(m *ResManager) *RenderQueue
func (*RenderQueue) Destroy ¶
func (rq *RenderQueue) Destroy()
func (*RenderQueue) Init ¶
func (rq *RenderQueue) Init()
func (*RenderQueue) Reset ¶
func (rq *RenderQueue) Reset(w, h uint16, pr float32)
reset frame-buffer size
func (*RenderQueue) SetIndexBuffer ¶
func (rq *RenderQueue) SetIndexBuffer(id uint16, firstIndex, num uint16)
func (*RenderQueue) SetScissor ¶
func (rq *RenderQueue) SetScissor(x, y, width, height uint16) (id uint16)
func (*RenderQueue) SetScissorCached ¶
func (rq *RenderQueue) SetScissorCached(id uint16)
func (*RenderQueue) SetState ¶
func (rq *RenderQueue) SetState(state uint64, rgba uint32)
func (*RenderQueue) SetStencil ¶
func (rq *RenderQueue) SetStencil(stencil uint32)
func (*RenderQueue) SetTexture ¶
func (rq *RenderQueue) SetTexture(stage uint8, samplerId uint16, texId uint16, flags uint32)
func (*RenderQueue) SetTransform ¶
func (rq *RenderQueue) SetTransform(mtx *f32.Mat4)
Transform 是 uniform 之一,2D 世界可以省略
func (*RenderQueue) SetUniform ¶
func (rq *RenderQueue) SetUniform(id uint16, ptr unsafe.Pointer)
复制简单数据的时候(比如:Sampler),采用赋值的方式可能更快 TODO
func (*RenderQueue) SetVertexBuffer ¶
func (rq *RenderQueue) SetVertexBuffer(stream uint8, id uint16, firstVertex, numVertex uint16)
func (*RenderQueue) SetViewClear ¶
func (*RenderQueue) SetViewPort ¶
func (rq *RenderQueue) SetViewPort(id uint8, x, y, width, height uint16)
func (*RenderQueue) SetViewScissor ¶
func (rq *RenderQueue) SetViewScissor(id uint8, x, y, with, height uint16)
/ View Related Setting
func (*RenderQueue) SetViewTransform ¶
func (rq *RenderQueue) SetViewTransform(id uint8, view, proj *f32.Mat4, flags uint8)
type ResManager ¶
type ResManager struct {
// contains filtered or unexported fields
}
var R *ResManager
Global Resources Manager!
func NewResManager ¶
func NewResManager() *ResManager
func (*ResManager) AllocIndexBuffer ¶
func (rm *ResManager) AllocIndexBuffer(mem Memory) (id uint16, ib *IndexBuffer)
AllocIndexBuffer alloc a new Index-Buffer, Return the resource handler.
func (*ResManager) AllocShader ¶
func (rm *ResManager) AllocShader(vsh, fsh string) (id uint16, sh *Shader)
AllocShader compile and link the Shader source code, Return the resource handler.
func (*ResManager) AllocTexture ¶
func (rm *ResManager) AllocTexture(img image.Image) (id uint16, tex *Texture2D)
AllocTexture upload image to GPU, Return the resource handler.
func (*ResManager) AllocUniform ¶
func (rm *ResManager) AllocUniform(shId uint16, name string, xType UniformType, num uint32) (id uint16, um *Uniform)
AllocUniform get the uniform slot in a shader program, Return the resource handler.
func (*ResManager) AllocVertexBuffer ¶
func (rm *ResManager) AllocVertexBuffer(mem Memory, stride uint16) (id uint16, vb *VertexBuffer)
AllocVertexBuffer alloc a new Vertex-Buffer, Return the resource handler.
func (*ResManager) Destroy ¶
func (rm *ResManager) Destroy()
func (*ResManager) Free ¶
func (rm *ResManager) Free(id uint16)
Free free all resource managed by R. Including index-buffer, vertex-buffer, texture, uniform and shader program.
func (*ResManager) IndexBuffer ¶
func (rm *ResManager) IndexBuffer(id uint16) (ok bool, ib *IndexBuffer)
IndexBuffer returns the low-level IndexBuffer struct.
func (*ResManager) Shader ¶
func (rm *ResManager) Shader(id uint16) (ok bool, sh *Shader)
Shader returns the low-level Shader struct.
func (*ResManager) Texture ¶
func (rm *ResManager) Texture(id uint16) (ok bool, tex *Texture2D)
Texture returns the low-level Texture struct.
func (*ResManager) Uniform ¶
func (rm *ResManager) Uniform(id uint16) (ok bool, um *Uniform)
Uniform returns the low-level Uniform struct.
func (*ResManager) VertexBuffer ¶
func (rm *ResManager) VertexBuffer(id uint16) (ok bool, vb *VertexBuffer)
VertexBuffer returns the low-level VertexBuffer struct.
type Shader ¶
type Shader struct { GLShader // bind attribute AttrBinds [32]AttribBind // predefined uniform M, V, P Uniform // contains filtered or unexported fields }
编译/配置着色器相关数据 可以在 Shader 中实现预定义的 Uniform
func (*Shader) AddAttributeBinding ¶
func (sh *Shader) AddAttributeBinding(attr string, stream uint32, comp VertexComp)
func (*Shader) AddUniformBinding ¶
func (*Shader) BindAttributes ¶
func (sh *Shader) BindAttributes(R *ResManager, streams []Stream)
如果 AttrBinds 指定了一个 Stream,但是 Stream 并没有提供相应的数据(stride < Offset) 此时应该 disable 当前 Attribute,
type SortKey ¶
SortKey FORMAT 64bit: 0000 - 0000000000 - 00000 - 000 - 0000000000
^ ^ ^ ^ ^ | | | | | | z-order() shader(2^5) | texture(2^10)
Layer(2^4) blend(2^3) 4 + 10 + 5 + 3 + 10
type Uniform ¶
type Uniform struct { Name string // Uniform Name Slot uint8 // slot in shader Size uint8 Type UniformType Count uint8 }
type UniformBuffer ¶
type UniformBuffer struct {
// contains filtered or unexported fields
}
func NewUniformBuffer ¶
func NewUniformBuffer() *UniformBuffer
func (*UniformBuffer) GetPos ¶
func (ub *UniformBuffer) GetPos() uint32
func (*UniformBuffer) IsEmpty ¶
func (ub *UniformBuffer) IsEmpty() bool
func (*UniformBuffer) ReadPointer ¶
func (ub *UniformBuffer) ReadPointer(size uint32) unsafe.Pointer
func (*UniformBuffer) ReadUInt32 ¶
func (ub *UniformBuffer) ReadUInt32() uint32
func (*UniformBuffer) Reset ¶
func (ub *UniformBuffer) Reset()
func (*UniformBuffer) Seek ¶
func (ub *UniformBuffer) Seek(pos uint32)
func (*UniformBuffer) WriteUInt32 ¶
func (ub *UniformBuffer) WriteUInt32(value uint32)
type UniformType ¶
type UniformType uint8
const ( UniformStart UniformType = iota UniformMat4 // mat4 array UniformMat3 // mat3 array UniformVec4 // vec4 array UniformVec1 // float array UniformInt1 // int array UniformSampler // sampler UniformEnd )
/ 支持哪些类型呢? / https://github.com/bkaradzic/bgfx/issues/653 / float 在 GPU 中表现为 vec4
type VertexBuffer ¶
type VertexBuffer struct { Id uint32 // contains filtered or unexported fields }
func (*VertexBuffer) Destroy ¶
func (vb *VertexBuffer) Destroy()