bk

package
v0.0.0-...-e77de60 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 23, 2020 License: Zlib Imports: 11 Imported by: 0

Documentation

Overview

bk-api provide low-level graphics api.

Index

Constants

View Source
const (
	IdMask      uint16 = 0x0FFF
	IdTypeShift        = 12
)
View Source
const (
	IdTypeIndex uint16 = iota
	IdTypeVertex
	IdTypeTexture
	IdTypeLayout
	IdTypeUniform
	IdTypeShader
)

ID FORMAT 0x F FFF

^    ^
|    +---- id value
+--------- id type
View Source
const (
	MaxIndex   = 2 << 10
	MaxVertex  = 2 << 10
	MaxTexture = 1 << 10
	MaxUniform = 32 * 8
	MaxShader  = 32
)
View Source
const (
	DebugResMan uint32 = 0x000000001
	DebugQueue  uint32 = 0x000000002
)
View Source
const (
	SK_LayerMask   uint32 = 0xF0000000
	SK_ShaderMask  uint32 = 0x0F000000
	SK_BlendMask   uint32 = 0x00F00000
	SK_TextureMask uint32 = 0x000FF000
)
View Source
const InvalidId uint16 = 0x0000
View Source
const MAX_QUEUE_SIZE = 8 << 10

~ 8000 draw call

View Source
const UInt16Max uint16 = 0xFFFF
View Source
const (
	UNIFORM_BUFFER_SIZE = 16 << 10
)

Variables

View Source
var MAX = struct {
}{}

//// MAX SIZE

View Source
var P2C4 = []VertexComp{
	{2, AttrFloat, 0, 0},
	{4, AttrUInt8, 8, 1},
}

<x,y, color>

View Source
var P2T2 = []VertexComp{
	{2, AttrFloat, 0, 0},
	{2, AttrFloat, 8, 0},
}

<x,y, u,v>

View Source
var P2T2C4 = []VertexComp{
	{2, AttrFloat, 0, 0},
	{2, AttrFloat, 8, 0},
	{4, AttrUInt8, 16, 1},
}

useful defines <x,y, u,v, color>

View Source
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,
}
View Source
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

View Source
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

View Source
var ST_PT = struct {
	TRIANGLES      uint64
	TRIANGLE_STRIP uint64
	LINES          uint64
	LINE_STRIP     uint64
	POINTS         uint64
}{
	TRIANGLES:      0x0000000000000000,
	TRIANGLE_STRIP: 0x0000000000001000,
	LINES:          0x0000000000002000,
	LINE_STRIP:     0x0000000000003000,
	POINTS:         0x0000000000004000,
}

Functions

func Compile

func Compile(vertex, fragment string) (uint32, error)

func Destroy

func Destroy()

Destroy release any resource used by the bk-api.

func Dump

func Dump()

func Flush

func Flush() int

Execute final draw

func GetErrors

func GetErrors() string

func Init

func Init()

Init init the bk-api.

func Reset

func Reset(width, height uint32, pixelRatio float32)

Reset resets RenderContext's internal state, such as frame-buffer size.

func ResetDrawCall

func ResetDrawCall()

Reset DrawCall state

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

func SetIndexBuffer(id uint16, firstIndex, num uint32)

SetIndexBuffer sets index buffer for drawCall primitive.

func SetScissor

func SetScissor(x, y, width, height uint16) uint16

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

func SetState(state uint64, rgba uint32)

SetState set render's states for drawCall primitive. State flags is defined by ST_BLEND.

func SetStencil

func SetStencil(stencil uint32)

SetStencil sets stencil test state.

func SetTexture

func SetTexture(stage uint8, sampler uint16, handle uint16, flags uint32)

SetSprite sets texture stages for drawCall primitive.

func SetTransform

func SetTransform(mtx *f32.Mat4)

SetTransform sets Model matrix.

func SetUniform

func SetUniform(id uint16, ptr unsafe.Pointer)

SetUniform sets shader uniform parameter for drawCall primitive.

func SetVertexBuffer

func SetVertexBuffer(stream uint8, id uint16, firstVertex, numVertex uint32)

SetVertexBuffer sets vertex buffer for drawCall primitive.

func SetViewClear

func SetViewClear(id uint8, flags uint16, rgba uint32, depth float32, stencil uint8)

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 SetViewPort(id uint8, x, y, width, height uint16)

func SetViewScissor

func SetViewScissor(id uint8, x, y, width, height uint16)

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

func SetViewTransform(id uint8, view, proj *f32.Mat4, flags uint8)

SetViewTransform sets view and projection matrices, all drawCall primitives in this view will use these matrices.

func Submit

func Submit(id uint8, program uint16, depth int32) uint32

Submit primitive for rendering. Default depth is zero. Returns Number of drawCall calls.

func Touch

func Touch(id uint8) uint32

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_decode(code uint32, uType, loc, size, num *uint8)

func Uniform_encode

func Uniform_encode(uType UniformType, loc, size, num uint8) uint32

Types

type AttrType

type AttrType uint8

/ Vertex attribute type enum

const (
	AttrInt8   AttrType = iota // byte
	AttrUInt8                  // uint8, unsigned byte
	AttrInt16                  // int16
	AttrUInt16                 // uint16
	AttrFixed                  // fixed
	AttrFloat                  // float32

	AttrCount
)

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 FreeList

type FreeList struct {
	// contains filtered or unexported fields
}

func (*FreeList) Pop

func (fl *FreeList) Pop() (slot uint16, ok bool)

func (*FreeList) Push

func (fl *FreeList) Push(slot uint16)

type GLShader

type GLShader struct {
	Program uint32
}

func (*GLShader) Create

func (s *GLShader) Create(vsh, fsh string) error

func (*GLShader) Destroy

func (s *GLShader) Destroy()

func (*GLShader) GetAttrLocation

func (s *GLShader) GetAttrLocation(attr string) uint32

func (*GLShader) GetUniformLocation

func (s *GLShader) GetUniformLocation(uniform string) uint32

func (*GLShader) SetFloat

func (s *GLShader) SetFloat(name string, value float32)

func (*GLShader) SetInteger

func (s *GLShader) SetInteger(name string, value int32)

func (*GLShader) SetMatrix4

func (s *GLShader) SetMatrix4(name string, mat4 f32.Mat4)

func (*GLShader) SetVec4fArray

func (s *GLShader) SetVec4fArray(name string, array []float32, count int32)

func (*GLShader) SetVector2f

func (s *GLShader) SetVector2f(name string, x, y float32)

func (*GLShader) SetVector3f

func (s *GLShader) SetVector3f(name string, x, y, z float32)

func (*GLShader) SetVector4f

func (s *GLShader) SetVector4f(name string, x, y, z, w float32)

func (*GLShader) Use

func (s *GLShader) Use()

type IndexBuffer

type IndexBuffer struct {
	Id uint32
	// contains filtered or unexported fields
}

func (*IndexBuffer) Create

func (ib *IndexBuffer) Create(size uint32, data unsafe.Pointer, flags uint16) error

func (*IndexBuffer) Destroy

func (ib *IndexBuffer) Destroy()

func (*IndexBuffer) Update

func (ib *IndexBuffer) Update(offset uint32, size uint32, data unsafe.Pointer, discard bool)

/ discard=false

type Memory

type Memory struct {
	Data unsafe.Pointer
	Size uint32
}

type Rect

type Rect struct {
	// contains filtered or unexported fields
}

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) Flush

func (rq *RenderQueue) Flush() int

/ 执行最终的绘制

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 (rq *RenderQueue) SetViewClear(id uint8, flags uint16, rgba uint32, depth float32, stencil uint8)

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)

func (*RenderQueue) Submit

func (rq *RenderQueue) Submit(id uint8, program uint16, depth int32) uint32

conversion: depth range [-int16, int16]

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) Init

func (rm *ResManager) Init()

skip first index - 0

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 (sh *Shader) AddUniformBinding(uniform string)

func (*Shader) BindAttributes

func (sh *Shader) BindAttributes(R *ResManager, streams []Stream)

如果 AttrBinds 指定了一个 Stream,但是 Stream 并没有提供相应的数据(stride < Offset) 此时应该 disable 当前 Attribute,

type SortKey

type SortKey struct {
	Layer   uint16
	Order   uint16
	Shader  uint16
	Blend   uint16
	Texture uint16
}

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

func SkDecode

func SkDecode(key uint64) (sk SortKey)

func (*SortKey) Decode

func (sk *SortKey) Decode(key uint64)

func (*SortKey) Encode

func (sk *SortKey) Encode() (key uint64)

type SortMode

type SortMode int
const (
	Sequential SortMode = iota
	Ascending
	Descending
)

type Stream

type Stream struct {
	// contains filtered or unexported fields
}

type SubTex

type SubTex struct {
	*Texture2D

	// location -
	Min, Max f32.Vec2
}

/// 还需要抽象 SubTexture 的概念出来

type Texture2D

type Texture2D struct {
	Width, Height float32
	Id            uint32
}

func (*Texture2D) Bind

func (t *Texture2D) Bind(stage int32)

func (*Texture2D) Create

func (t *Texture2D) Create(image image.Image) error

func (*Texture2D) Destroy

func (t *Texture2D) Destroy()

func (*Texture2D) Sub

func (t *Texture2D) Sub(x, y float32, w, h float32) *SubTex

func (*Texture2D) Update

func (t *Texture2D) Update(img image.Image, xoff, yoff int32, w, h int32) (err error)

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) Copy

func (ub *UniformBuffer) Copy(ptr unsafe.Pointer, size uint32)

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) Create

func (vb *VertexBuffer) Create(size uint32, data unsafe.Pointer, layout uint16, flags uint16) error

/ draw indirect >= es 3.0 or gl 4.0

func (*VertexBuffer) Destroy

func (vb *VertexBuffer) Destroy()

func (*VertexBuffer) Update

func (vb *VertexBuffer) Update(offset uint32, size uint32, data unsafe.Pointer, discard bool)

/ discard = false

type VertexComp

type VertexComp struct {
	Num        uint8
	Type       AttrType
	Offset     uint8
	Normalized uint8
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL