Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partially fixes GPU rendering #1205

Merged
merged 1 commit into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion korge-sandbox/src/commonMain/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ suspend fun main() = Korge(
//Demo(::MainStressButtons),
//Demo(::MainGifAnimation),
//Demo(::MainTransitionFilter),
Demo(::MainFilters),
//Demo(::MainFilters),
Demo(::MainGpuVectorRendering),
//Demo(::MainGpuVectorRendering),
//Demo(::MainTextMetrics),
//Demo(::MainBunnymark),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.soywiz.korge.input.*
import com.soywiz.korge.scene.Scene
import com.soywiz.korge.ui.uiButton
import com.soywiz.korge.view.*
import com.soywiz.korge.view.filter.*
import com.soywiz.korge.view.vector.*
import com.soywiz.korim.bitmap.*
import com.soywiz.korim.color.*
Expand All @@ -24,6 +25,19 @@ import com.soywiz.korma.geom.vector.StrokeInfo

class MainGpuVectorRendering : Scene() {
override suspend fun SContainer.sceneMain() {
gpuShapeView { }

graphics(GraphicsRenderer.GPU) {
it.antialiased = false
//it.debugDrawOnlyAntialiasedBorder = false
fill(Colors.GREEN, winding = Winding.EVEN_ODD) {
//circle(100, 100, 100)
//circleHole(100, 100, 50)
rect(100, 100, 100, 100)
rectHole(120, 120, 30, 30)
}
}//.filters(BlurFilter())
//return

//return

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,15 @@ class RenderContext constructor(
temp2d.copyFrom(this.viewMat2D)
this.viewMat2D.copyFrom(matrix)
this.viewMat.copyFrom(matrix)
uniforms[DefaultShaders.u_ViewMat] = this.viewMat
//println("viewMat: $viewMat, matrix: $matrix")
try {
callback()
} finally {
flush()
this.viewMat.copyFrom(temp)
this.viewMat2D.copyFrom(temp2d)
uniforms[DefaultShaders.u_ViewMat] = this.viewMat
}
}
}
Expand Down Expand Up @@ -378,7 +380,7 @@ class RenderContext constructor(
/////////////// FROM AG ///////////////////

val frameFrameBuffers = LinkedHashSet<AGFrameBuffer>()
val frameBuffers: Pool<AGFrameBuffer> = Pool { AGFrameBuffer() }
val frameBuffers: Pool<AGFrameBuffer> = Pool { AGFrameBuffer(id = it) }
val frameBufferStack = FastArrayList<AGFrameBuffer>()

inline fun doRender(block: () -> Unit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,9 @@ open class GpuShapeView(
bufferWidth = currentFrameBuffer.width
bufferHeight = currentFrameBuffer.height
//println("bufferWidth=$bufferWidth, bufferHeight=$bufferHeight")
//println("ctx.currentFrameBuffer: ${ctx.currentFrameBuffer}")
ctx.renderToTexture(bufferWidth, bufferHeight, {
//println("!!Render to texture!! :: ctx.currentFrameBuffer: ${ctx.currentFrameBuffer}")
renderCommands(ctx, doRequireTexture)
}, hasDepth = false, hasStencil = true, msamples = 1) { texture ->
ctx.useBatcher {
Expand Down Expand Up @@ -442,6 +444,7 @@ open class GpuShapeView(
gpuShapeViewCommands.addVertex(x.toFloat(), y.toFloat(), len = len, maxLen = maxLen)
}
val vertexEnd = gpuShapeViewCommands.verticesEnd()
//println("bb.getBounds()=${bb.getBounds()} - ${bb.getBounds().toAGScissor()}")
return PointsResult(bb.getBounds().toAGScissor(), points.size + 2, vertexStart, vertexEnd)
}

Expand Down Expand Up @@ -491,8 +494,11 @@ open class GpuShapeView(
val isSimpleDraw = shapeIsConvex && shape.clip == null && !debugDrawOnlyAntialiasedBorder
//val isSimpleDraw = false
val pathDataList = getPointsForPathList(shape.path, if (isSimpleDraw) AGDrawType.TRIANGLE_STRIP else AGDrawType.TRIANGLE_FAN)
val pathBoundsNoExpended = BoundsBuilder().also { bb -> pathDataList.fastForEach { bb.add(it.bounds) } }.getBounds()
val pathBounds: AGScissor = pathBoundsNoExpended.clone().expand(2, 2, 2, 2).toAGScissor()
val pathBoundsNoExpanded = BoundsBuilder().also { bb -> pathDataList.fastForEach {
//println("bounds=${it.bounds}")
bb.add(it.bounds)
} }.getBounds()
val pathBounds: AGScissor = pathBoundsNoExpanded.clone().expand(2, 2, 2, 2).toAGScissor()

val clipDataStart = gpuShapeViewCommands.verticesStart()
val clipData = shape.clip?.let { getPointsForPath(it, AGDrawType.TRIANGLE_FAN) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,24 +139,27 @@ class GpuShapeViewCommands {
var scissor = AGScissor.NIL
//list.vertexArrayObjectSet(ag, GpuShapeViewPrograms.LAYOUT_POS_TEX_FILL_DIST, bufferVertexData) {
val uniforms = batcher.uniforms
//println("----")
//println("----")
//println("----")
commands.fastForEach { cmd ->
//println("cmd:$cmd :: ${ctx.currentFrameBuffer}")
when (cmd) {
//is FinishCommand -> list.flush()
is ScissorCommand -> {
scissor = cmd.scissor
}

is ClearCommand -> {
ctx.clear(stencil = cmd.i, clearColor = false, clearStencil = true, clearDepth = false)
}

is ShapeCommand -> {
val paintShader = cmd.paintShader
//println("cmd.vertexCount=${cmd.vertexCount}, cmd.vertexIndex=${cmd.vertexIndex}, paintShader=$paintShader")
batcher.simulateBatchStats(cmd.vertexCount)
//println(paintShader.uniforms)
tempUniforms.clear()
paintShader?.uniforms?.let { resolve(ctx, it, paintShader.texUniforms) }
tempUniforms.put(uniforms)
tempUniforms.put(paintShader?.uniforms)
val pixelScale = decomposed.scaleAvg / ctx.bp.globalToWindowScaleAvg
//val pixelScale = 1f
Expand All @@ -169,7 +172,10 @@ class GpuShapeViewCommands {
ag.draw(AGBatch(
ctx.currentFrameBuffer.base,
ctx.currentFrameBuffer.info,
program = cmd.program ?: program,
vertexData = vertices,
//indices = indices,
scissor = scissor.applyMatrixBounds(tempMat),
uniforms = tempUniforms,
stencilOpFunc = cmd.stencilOpFunc,
stencilRef = cmd.stencilRef,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
AGClear(frameBuffer=AGFrameBufferBase(isMain=true), frameBufferInfo=AGFrameBufferInfo(width=1280, height=720, hasDepth=true, hasStencil=true, samples=1), color=#00000000, depth=1.0, stencil=0, clearColor=true, clearDepth=true, clearStencil=true)
finish()
AGClear(frameBuffer=AGFrameBufferBase(isMain=true), frameBufferInfo=AGFrameBufferInfo(width=1280, height=720, hasDepth=true, hasStencil=true, samples=1), color=#00000000, depth=1.0, stencil=0, clearColor=true, clearDepth=true, clearStencil=true)
AGBatch(frameBuffer=AGFrameBufferBase(isMain=true), frameBufferInfo=AGFrameBufferInfo(width=1280, height=720, hasDepth=true, hasStencil=true, samples=1), vertexData=AGVertexArrayObject(list=[AGVertexData(layout=VertexLayout[a_Pos, a_Tex, a_Col, a_Col2], buffer=AGBuffer(96)), AGVertexData(layout=VertexLayout[a_TexIndex], buffer=AGBuffer(4)), AGVertexData(layout=VertexLayout[a_Wrap], buffer=AGBuffer(4))]), indices=AGBuffer(12), indexType=USHORT, program=Program(name=BatchBuilder2D.Tinted.NoAdd, attributes=[a_Tex, a_TexIndex, a_Wrap, a_Col, a_Col2, a_Pos], uniforms=[u_ProjMat, u_ViewMat, u_Tex0, u_Tex1, u_Tex2, u_Tex3]), uniforms=AGUniformValues(u_ProjMat=AGUniformValue[Uniform(u_ProjMat)][AGValue[Mat4]([[0.0015625, 0, 0, 0, 0, -0.0027777778, 0, 0, 0, 0, -1, 0, -1, 1, 0, 1]])], u_ViewMat=AGUniformValue[Uniform(u_ViewMat)][AGValue[Mat4]([[1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]])], u_Tex0=AGUniformValue[Uniform(u_Tex0)][AGValue[Sampler2D]([[-1],AGTexture(size=1,1,pre=true),AGTextureUnitInfo(wrap=CLAMP_TO_EDGE, linear=true, trilinear=true)])], u_Tex1=AGUniformValue[Uniform(u_Tex1)][AGValue[Sampler2D]([[-1]])], u_Tex2=AGUniformValue[Uniform(u_Tex2)][AGValue[Sampler2D]([[-1]])], u_Tex3=AGUniformValue[Uniform(u_Tex3)][AGValue[Sampler2D]([[-1]])]), blending=Blending(outRGB = (srcRGB * 1) + (dstRGB * (1 - srcA)), outA = (srcA * 1) + (dstA * (1 - srcA))), stencilOpFunc=AGStencilOpFunc(data=5591072), stencilRef=AGStencilReference(data=16776960), colorMask=AGColorMask(data=15), depthAndFrontFace=AGDepthAndFrontFace(data=33550336), scissor=Scissor(x=0, y=0, width=1280, height=720), cullFace=NONE, drawType=TRIANGLES, drawOffset=0, vertexCount=6, instances=1)
AGBatch(frameBuffer=AGFrameBufferBase(isMain=true), frameBufferInfo=AGFrameBufferInfo(width=1280, height=720, hasDepth=true, hasStencil=true, samples=1), vertexData=AGVertexArrayObject(list=[AGVertexData(layout=VertexLayout[a_Pos, a_Tex, a_Col, a_Col2], buffer=AGBuffer(96)), AGVertexData(layout=VertexLayout[a_TexIndex], buffer=AGBuffer(4)), AGVertexData(layout=VertexLayout[a_Wrap], buffer=AGBuffer(4))]), indices=AGBuffer(12), indexType=USHORT, program=Program(name=BatchBuilder2D.Tinted.NoAdd, attributes=[a_Tex, a_TexIndex, a_Wrap, a_Col, a_Col2, a_Pos], uniforms=[u_ProjMat, u_ViewMat, u_Tex0, u_Tex1, u_Tex2, u_Tex3]), uniforms=AGUniformValues(u_ProjMat=AGUniformValue[Uniform(u_ProjMat)][AGValue[Mat4]([[0.0015625, 0, 0, 0, 0, -0.0027777778, 0, 0, 0, 0, -1, 0, -1, 1, 0, 1]])], u_ViewMat=AGUniformValue[Uniform(u_ViewMat)][AGValue[Mat4]([[1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]])], u_Tex0=AGUniformValue[Uniform(u_Tex0)][AGValue[Sampler2D]([[-1],AGTexture(size=1,1,pre=true),AGTextureUnitInfo(wrap=CLAMP_TO_EDGE, linear=true, trilinear=true)])], u_Tex1=AGUniformValue[Uniform(u_Tex1)][AGValue[Sampler2D]([[-1]])], u_Tex2=AGUniformValue[Uniform(u_Tex2)][AGValue[Sampler2D]([[-1]])], u_Tex3=AGUniformValue[Uniform(u_Tex3)][AGValue[Sampler2D]([[-1]])]), blending=Blending(outRGB = (srcRGB * 1) + (dstRGB * (1 - srcA)), outA = (srcA * 1) + (dstA * (1 - srcA))), stencilOpFunc=AGStencilOpFunc(enabled=false, compareMode=ALWAYS, actions=[KEEP, KEEP, KEEP]), stencilRef=AGStencilReference(referenceValue=0, readMask=255, writeMask=255), colorMask=AGColorMask(RGBA), depthAndFrontFace=AGDepthAndFrontFace(data=33550336), scissor=Scissor(x=0, y=0, width=1280, height=720), cullFace=NONE, drawType=TRIANGLES, drawOffset=0, vertexCount=6, instances=1)
finish()
Original file line number Diff line number Diff line change
@@ -1 +1 @@
AGBatch(frameBuffer=AGFrameBufferBase(isMain=true), frameBufferInfo=AGFrameBufferInfo(width=640, height=480, hasDepth=true, hasStencil=true, samples=1), vertexData=AGVertexArrayObject(list=[AGVertexData(layout=VertexLayout[a_Pos, a_Tex, a_Col, a_Col2], buffer=AGBuffer(384)), AGVertexData(layout=VertexLayout[a_TexIndex], buffer=AGBuffer(16)), AGVertexData(layout=VertexLayout[a_Wrap], buffer=AGBuffer(16))]), indices=AGBuffer(48), indexType=USHORT, program=Program(name=BatchBuilder2D.Tinted.NoAdd, attributes=[a_Tex, a_TexIndex, a_Wrap, a_Col, a_Col2, a_Pos], uniforms=[u_ProjMat, u_ViewMat, u_Tex0, u_Tex1, u_Tex2, u_Tex3]), uniforms=AGUniformValues(u_ProjMat=AGUniformValue[Uniform(u_ProjMat)][AGValue[Mat4]([[0.003125, 0, 0, 0, 0, -0.004166667, 0, 0, 0, 0, -1, 0, -1, 1, 0, 1]])], u_ViewMat=AGUniformValue[Uniform(u_ViewMat)][AGValue[Mat4]([[1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]])], u_Tex0=AGUniformValue[Uniform(u_Tex0)][AGValue[Sampler2D]([[-1],AGTexture(size=361,512,pre=true),AGTextureUnitInfo(wrap=CLAMP_TO_EDGE, linear=true, trilinear=true)])], u_Tex1=AGUniformValue[Uniform(u_Tex1)][AGValue[Sampler2D]([[-1]])], u_Tex2=AGUniformValue[Uniform(u_Tex2)][AGValue[Sampler2D]([[-1]])], u_Tex3=AGUniformValue[Uniform(u_Tex3)][AGValue[Sampler2D]([[-1]])]), blending=Blending(outRGB = (srcRGB * 1) + (dstRGB * (1 - srcA)), outA = (srcA * 1) + (dstA * (1 - srcA))), stencilOpFunc=AGStencilOpFunc(data=5591072), stencilRef=AGStencilReference(data=16776960), colorMask=AGColorMask(data=15), depthAndFrontFace=AGDepthAndFrontFace(data=33550336), scissor=null, cullFace=NONE, drawType=TRIANGLES, drawOffset=0, vertexCount=24, instances=1)
AGBatch(frameBuffer=AGFrameBufferBase(isMain=true), frameBufferInfo=AGFrameBufferInfo(width=640, height=480, hasDepth=true, hasStencil=true, samples=1), vertexData=AGVertexArrayObject(list=[AGVertexData(layout=VertexLayout[a_Pos, a_Tex, a_Col, a_Col2], buffer=AGBuffer(384)), AGVertexData(layout=VertexLayout[a_TexIndex], buffer=AGBuffer(16)), AGVertexData(layout=VertexLayout[a_Wrap], buffer=AGBuffer(16))]), indices=AGBuffer(48), indexType=USHORT, program=Program(name=BatchBuilder2D.Tinted.NoAdd, attributes=[a_Tex, a_TexIndex, a_Wrap, a_Col, a_Col2, a_Pos], uniforms=[u_ProjMat, u_ViewMat, u_Tex0, u_Tex1, u_Tex2, u_Tex3]), uniforms=AGUniformValues(u_ProjMat=AGUniformValue[Uniform(u_ProjMat)][AGValue[Mat4]([[0.003125, 0, 0, 0, 0, -0.004166667, 0, 0, 0, 0, -1, 0, -1, 1, 0, 1]])], u_ViewMat=AGUniformValue[Uniform(u_ViewMat)][AGValue[Mat4]([[1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]])], u_Tex0=AGUniformValue[Uniform(u_Tex0)][AGValue[Sampler2D]([[-1],AGTexture(size=361,512,pre=true),AGTextureUnitInfo(wrap=CLAMP_TO_EDGE, linear=true, trilinear=true)])], u_Tex1=AGUniformValue[Uniform(u_Tex1)][AGValue[Sampler2D]([[-1]])], u_Tex2=AGUniformValue[Uniform(u_Tex2)][AGValue[Sampler2D]([[-1]])], u_Tex3=AGUniformValue[Uniform(u_Tex3)][AGValue[Sampler2D]([[-1]])]), blending=Blending(outRGB = (srcRGB * 1) + (dstRGB * (1 - srcA)), outA = (srcA * 1) + (dstA * (1 - srcA))), stencilOpFunc=AGStencilOpFunc(enabled=false, compareMode=ALWAYS, actions=[KEEP, KEEP, KEEP]), stencilRef=AGStencilReference(referenceValue=0, readMask=255, writeMask=255), colorMask=AGColorMask(RGBA), depthAndFrontFace=AGDepthAndFrontFace(data=33550336), scissor=null, cullFace=NONE, drawType=TRIANGLES, drawOffset=0, vertexCount=24, instances=1)
Loading