mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-03 08:25:34 +00:00
gpu: rename DrawMode to Topology and move it to pipeline descriptors
Vulkan needs the topology stated in its pipeline descriptor, not at draw time. Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
@@ -42,11 +42,12 @@ type Backend struct {
|
||||
}
|
||||
|
||||
type Pipeline struct {
|
||||
vert *d3d11.VertexShader
|
||||
frag *d3d11.PixelShader
|
||||
layout *d3d11.InputLayout
|
||||
blend *d3d11.BlendState
|
||||
stride int
|
||||
vert *d3d11.VertexShader
|
||||
frag *d3d11.PixelShader
|
||||
layout *d3d11.InputLayout
|
||||
blend *d3d11.BlendState
|
||||
stride int
|
||||
topology driver.Topology
|
||||
}
|
||||
|
||||
type Texture struct {
|
||||
@@ -497,11 +498,12 @@ func (b *Backend) NewPipeline(desc driver.PipelineDesc) (driver.Pipeline, error)
|
||||
d3d11.IUnknownAddRef(unsafe.Pointer(fshRef), fshRef.Vtbl.AddRef)
|
||||
|
||||
return &Pipeline{
|
||||
vert: vshRef,
|
||||
frag: fshRef,
|
||||
layout: layout,
|
||||
stride: desc.VertexLayout.Stride,
|
||||
blend: blend,
|
||||
vert: vshRef,
|
||||
frag: fshRef,
|
||||
layout: layout,
|
||||
stride: desc.VertexLayout.Stride,
|
||||
blend: blend,
|
||||
topology: desc.Topology,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -551,31 +553,33 @@ func (b *Backend) Viewport(x, y, width, height int) {
|
||||
b.ctx.RSSetViewports(&b.viewport)
|
||||
}
|
||||
|
||||
func (b *Backend) DrawArrays(mode driver.DrawMode, off, count int) {
|
||||
b.prepareDraw(mode)
|
||||
func (b *Backend) DrawArrays(off, count int) {
|
||||
b.prepareDraw()
|
||||
b.ctx.Draw(uint32(count), uint32(off))
|
||||
}
|
||||
|
||||
func (b *Backend) DrawElements(mode driver.DrawMode, off, count int) {
|
||||
b.prepareDraw(mode)
|
||||
func (b *Backend) DrawElements(off, count int) {
|
||||
b.prepareDraw()
|
||||
b.ctx.DrawIndexed(uint32(count), uint32(off), 0)
|
||||
}
|
||||
|
||||
func (b *Backend) prepareDraw(mode driver.DrawMode) {
|
||||
if p := b.pipeline; p != nil {
|
||||
b.ctx.VSSetShader(p.vert)
|
||||
b.ctx.PSSetShader(p.frag)
|
||||
b.ctx.IASetInputLayout(p.layout)
|
||||
b.ctx.OMSetBlendState(p.blend, nil, 0xffffffff)
|
||||
if b.vert.buffer != nil {
|
||||
b.ctx.IASetVertexBuffers(b.vert.buffer.buf, uint32(p.stride), uint32(b.vert.offset))
|
||||
}
|
||||
func (b *Backend) prepareDraw() {
|
||||
p := b.pipeline
|
||||
if p == nil {
|
||||
return
|
||||
}
|
||||
b.ctx.VSSetShader(p.vert)
|
||||
b.ctx.PSSetShader(p.frag)
|
||||
b.ctx.IASetInputLayout(p.layout)
|
||||
b.ctx.OMSetBlendState(p.blend, nil, 0xffffffff)
|
||||
if b.vert.buffer != nil {
|
||||
b.ctx.IASetVertexBuffers(b.vert.buffer.buf, uint32(p.stride), uint32(b.vert.offset))
|
||||
}
|
||||
var topology uint32
|
||||
switch mode {
|
||||
case driver.DrawModeTriangles:
|
||||
switch p.topology {
|
||||
case driver.TopologyTriangles:
|
||||
topology = d3d11.PRIMITIVE_TOPOLOGY_TRIANGLELIST
|
||||
case driver.DrawModeTriangleStrip:
|
||||
case driver.TopologyTriangleStrip:
|
||||
topology = d3d11.PRIMITIVE_TOPOLOGY_TRIANGLESTRIP
|
||||
default:
|
||||
panic("unsupported draw mode")
|
||||
|
||||
@@ -32,8 +32,8 @@ type Device interface {
|
||||
NewPipeline(desc PipelineDesc) (Pipeline, error)
|
||||
|
||||
Viewport(x, y, width, height int)
|
||||
DrawArrays(mode DrawMode, off, count int)
|
||||
DrawElements(mode DrawMode, off, count int)
|
||||
DrawArrays(off, count int)
|
||||
DrawElements(off, count int)
|
||||
|
||||
BeginRenderPass(f Framebuffer, desc LoadDesc)
|
||||
EndRenderPass()
|
||||
@@ -71,6 +71,7 @@ type PipelineDesc struct {
|
||||
VertexLayout VertexLayout
|
||||
BlendDesc BlendDesc
|
||||
PixelFormat TextureFormat
|
||||
Topology Topology
|
||||
}
|
||||
|
||||
type VertexLayout struct {
|
||||
@@ -95,7 +96,7 @@ type AccessBits uint8
|
||||
|
||||
type BlendFactor uint8
|
||||
|
||||
type DrawMode uint8
|
||||
type Topology uint8
|
||||
|
||||
type TextureFilter uint8
|
||||
type TextureFormat uint8
|
||||
@@ -186,8 +187,8 @@ const (
|
||||
)
|
||||
|
||||
const (
|
||||
DrawModeTriangleStrip DrawMode = iota
|
||||
DrawModeTriangles
|
||||
TopologyTriangleStrip Topology = iota
|
||||
TopologyTriangles
|
||||
)
|
||||
|
||||
const (
|
||||
|
||||
@@ -387,7 +387,8 @@ type Backend struct {
|
||||
computeEnc C.CFTypeRef
|
||||
blitEnc C.CFTypeRef
|
||||
|
||||
prog *Program
|
||||
prog *Program
|
||||
topology C.MTLPrimitiveType
|
||||
|
||||
stagingBuf C.CFTypeRef
|
||||
stagingOff int
|
||||
@@ -420,6 +421,7 @@ type Program struct {
|
||||
|
||||
type Pipeline struct {
|
||||
pipeline C.CFTypeRef
|
||||
topology C.MTLPrimitiveType
|
||||
}
|
||||
|
||||
type Framebuffer struct {
|
||||
@@ -659,7 +661,7 @@ func (b *Backend) NewPipeline(desc driver.PipelineDesc) (driver.Pipeline, error)
|
||||
if pipe == 0 {
|
||||
return nil, errors.New("metal: pipeline construction failed")
|
||||
}
|
||||
return &Pipeline{pipeline: pipe}, nil
|
||||
return &Pipeline{pipeline: pipe, topology: primitiveFor(desc.Topology)}, nil
|
||||
}
|
||||
|
||||
func dataTypeSize(d shader.DataType) int {
|
||||
@@ -799,29 +801,27 @@ func (b *Backend) Viewport(x, y, width, height int) {
|
||||
})
|
||||
}
|
||||
|
||||
func (b *Backend) DrawArrays(mode driver.DrawMode, off, count int) {
|
||||
func (b *Backend) DrawArrays(off, count int) {
|
||||
enc := b.renderEnc
|
||||
if enc == 0 {
|
||||
panic("no active render pass")
|
||||
}
|
||||
t := primitiveFor(mode)
|
||||
C.renderEncDrawPrimitives(enc, t, C.NSUInteger(off), C.NSUInteger(count))
|
||||
C.renderEncDrawPrimitives(enc, b.topology, C.NSUInteger(off), C.NSUInteger(count))
|
||||
}
|
||||
|
||||
func (b *Backend) DrawElements(mode driver.DrawMode, off, count int) {
|
||||
func (b *Backend) DrawElements(off, count int) {
|
||||
enc := b.renderEnc
|
||||
if enc == 0 {
|
||||
panic("no active render pass")
|
||||
}
|
||||
t := primitiveFor(mode)
|
||||
C.renderEncDrawIndexedPrimitives(enc, t, b.indexBuf.buffer, C.NSUInteger(off), C.NSUInteger(count))
|
||||
C.renderEncDrawIndexedPrimitives(enc, b.topology, b.indexBuf.buffer, C.NSUInteger(off), C.NSUInteger(count))
|
||||
}
|
||||
|
||||
func primitiveFor(mode driver.DrawMode) C.MTLPrimitiveType {
|
||||
func primitiveFor(mode driver.Topology) C.MTLPrimitiveType {
|
||||
switch mode {
|
||||
case driver.DrawModeTriangles:
|
||||
case driver.TopologyTriangles:
|
||||
return C.MTLPrimitiveTypeTriangle
|
||||
case driver.DrawModeTriangleStrip:
|
||||
case driver.TopologyTriangleStrip:
|
||||
return C.MTLPrimitiveTypeTriangleStrip
|
||||
default:
|
||||
panic("metal: unknown draw mode")
|
||||
@@ -960,6 +960,7 @@ func (b *Backend) BindPipeline(pipe driver.Pipeline) {
|
||||
panic("no active render pass")
|
||||
}
|
||||
C.renderEncSetRenderPipelineState(enc, p.pipeline)
|
||||
b.topology = p.topology
|
||||
}
|
||||
|
||||
func (b *Backend) BindProgram(prog driver.Program) {
|
||||
|
||||
@@ -113,10 +113,11 @@ type framebuffer struct {
|
||||
}
|
||||
|
||||
type pipeline struct {
|
||||
prog *program
|
||||
inputs []shader.InputLocation
|
||||
layout driver.VertexLayout
|
||||
blend driver.BlendDesc
|
||||
prog *program
|
||||
inputs []shader.InputLocation
|
||||
layout driver.VertexLayout
|
||||
blend driver.BlendDesc
|
||||
topology driver.Topology
|
||||
}
|
||||
|
||||
type buffer struct {
|
||||
@@ -822,16 +823,16 @@ func (b *Backend) SetBlend(enable bool) {
|
||||
b.glstate.set(b.funcs, gl.BLEND, enable)
|
||||
}
|
||||
|
||||
func (b *Backend) DrawElements(mode driver.DrawMode, off, count int) {
|
||||
func (b *Backend) DrawElements(off, count int) {
|
||||
b.prepareDraw()
|
||||
// off is in 16-bit indices, but DrawElements take a byte offset.
|
||||
byteOff := off * 2
|
||||
b.funcs.DrawElements(toGLDrawMode(mode), count, gl.UNSIGNED_SHORT, byteOff)
|
||||
b.funcs.DrawElements(toGLDrawMode(b.state.pipeline.topology), count, gl.UNSIGNED_SHORT, byteOff)
|
||||
}
|
||||
|
||||
func (b *Backend) DrawArrays(mode driver.DrawMode, off, count int) {
|
||||
func (b *Backend) DrawArrays(off, count int) {
|
||||
b.prepareDraw()
|
||||
b.funcs.DrawArrays(toGLDrawMode(mode), off, count)
|
||||
b.funcs.DrawArrays(toGLDrawMode(b.state.pipeline.topology), off, count)
|
||||
}
|
||||
|
||||
func (b *Backend) prepareDraw() {
|
||||
@@ -843,11 +844,11 @@ func (b *Backend) prepareDraw() {
|
||||
p.prog.updateUniforms()
|
||||
}
|
||||
|
||||
func toGLDrawMode(mode driver.DrawMode) gl.Enum {
|
||||
func toGLDrawMode(mode driver.Topology) gl.Enum {
|
||||
switch mode {
|
||||
case driver.DrawModeTriangleStrip:
|
||||
case driver.TopologyTriangleStrip:
|
||||
return gl.TRIANGLE_STRIP
|
||||
case driver.DrawModeTriangles:
|
||||
case driver.TopologyTriangles:
|
||||
return gl.TRIANGLES
|
||||
default:
|
||||
panic("unsupported draw mode")
|
||||
@@ -918,10 +919,11 @@ func (b *Backend) NewPipeline(desc driver.PipelineDesc) (driver.Pipeline, error)
|
||||
}
|
||||
}
|
||||
return &pipeline{
|
||||
prog: p,
|
||||
inputs: vsrc.Inputs,
|
||||
layout: layout,
|
||||
blend: desc.BlendDesc,
|
||||
prog: p,
|
||||
inputs: vsrc.Inputs,
|
||||
layout: layout,
|
||||
blend: desc.BlendDesc,
|
||||
topology: desc.Topology,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user