Skip to content

Commit

Permalink
Optimizations and Troubleshooting improvements (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
mokiat authored Sep 14, 2024
1 parent b67f309 commit 301338e
Show file tree
Hide file tree
Showing 24 changed files with 152 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: "1.22"
go-version: "1.23"

- name: Run Tests
run: go run github.com/onsi/ginkgo/v2/ginkgo -r -randomize-all
10 changes: 5 additions & 5 deletions game/graphics/internal/uniform_lighting.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
)

type LightUniform struct {
ShadowMatrices [8]sprec.Mat4
ShadowMatrices [4]sprec.Mat4
ModelMatrix sprec.Mat4

ShadowCascades [8]sprec.Vec2
ShadowCascades [4]sprec.Vec2

Color sprec.Vec3
Intensity float32
Expand All @@ -20,14 +20,14 @@ type LightUniform struct {
}

func (u LightUniform) Std140Plot(plotter *blob.Plotter) {
// 8 x mat4
// 4 x mat4
for _, matrix := range u.ShadowMatrices {
plotter.PlotSPMat4(matrix)
}
// mat4
plotter.PlotSPMat4(u.ModelMatrix)

// 8 x vec4
// 4 x vec4
for _, cascade := range u.ShadowCascades {
plotter.PlotSPVec2(cascade)
plotter.Skip(2 * 4)
Expand All @@ -45,5 +45,5 @@ func (u LightUniform) Std140Plot(plotter *blob.Plotter) {
}

func (u LightUniform) Std140Size() uint32 {
return 8*64 + 64 + 8*4*4 + 4*4 + 4*4
return 4*64 + 64 + 4*16 + 16 + 16
}
4 changes: 2 additions & 2 deletions game/graphics/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ func WithDirectionalShadowMapSize(size int) Option {
// WithDirectionalShadowMapCascadeCount configures the maximum number of
// cascades that the directional shadow maps will have.
//
// This value cannot be smaller than 1 and larger than 8 and will be clamped.
// This value cannot be smaller than 1 and larger than 4 and will be clamped.
func WithDirectionalShadowMapCascadeCount(count int) Option {
return func(c *config) {
c.DirectionalShadowMapCascadeCount = max(1, min(count, 8))
c.DirectionalShadowMapCascadeCount = max(1, min(count, 4))
}
}

Expand Down
9 changes: 9 additions & 0 deletions game/graphics/stage_bloom.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,14 @@ func (s *BloomStage) Allocate() {
s.allocateTextures(1, 1)

s.downsampleProgram = s.api.CreateProgram(render.ProgramInfo{
Label: "Bloom Downsample Program",
SourceCode: s.shaders.BloomDownsampleSet(),
TextureBindings: []render.TextureBinding{
render.NewTextureBinding("lackingSourceImage", bloomDownsampleHDRImageSlot),
},
})
s.downsamplePipeline = s.api.CreatePipeline(render.PipelineInfo{
Label: "Bloom Downsample Pipeline",
Program: s.downsampleProgram,
VertexArray: quadShape.VertexArray(),
Topology: quadShape.Topology(),
Expand All @@ -102,12 +104,14 @@ func (s *BloomStage) Allocate() {
BlendEnabled: false,
})
s.downsampleSampler = s.api.CreateSampler(render.SamplerInfo{
Label: "Bloom Downsample Sampler",
Wrapping: render.WrapModeClamp,
Filtering: render.FilterModeNearest,
Mipmapping: false,
})

s.blurProgram = s.api.CreateProgram(render.ProgramInfo{
Label: "Bloom Blur Program",
SourceCode: s.shaders.BloomBlurSet(),
TextureBindings: []render.TextureBinding{
render.NewTextureBinding("lackingSourceImage", bloomBlurSourceImageSlot),
Expand All @@ -117,6 +121,7 @@ func (s *BloomStage) Allocate() {
},
})
s.blurPipeline = s.api.CreatePipeline(render.PipelineInfo{
Label: "Bloom Blur Pipeline",
Program: s.blurProgram,
VertexArray: quadShape.VertexArray(),
Topology: quadShape.Topology(),
Expand Down Expand Up @@ -235,26 +240,30 @@ func (s *BloomStage) allocateTextures(width, height uint32) {
s.framebufferHeight = height

s.pingTexture = s.api.CreateColorTexture2D(render.ColorTexture2DInfo{
Label: "Bloom Ping Texture",
Width: width,
Height: height,
GenerateMipmaps: false,
GammaCorrection: false,
Format: render.DataFormatRGBA16F,
})
s.pingFramebuffer = s.api.CreateFramebuffer(render.FramebufferInfo{
Label: "Bloom Ping Framebuffer",
ColorAttachments: [4]opt.T[render.TextureAttachment]{
opt.V(render.PlainTextureAttachment(s.pingTexture)),
},
})

s.pongTexture = s.api.CreateColorTexture2D(render.ColorTexture2DInfo{
Label: "Bloom Pong Texture",
Width: width,
Height: height,
GenerateMipmaps: false,
GammaCorrection: false,
Format: render.DataFormatRGBA16F,
})
s.pongFramebuffer = s.api.CreateFramebuffer(render.FramebufferInfo{
Label: "Bloom Pong Framebuffer",
ColorAttachments: [4]opt.T[render.TextureAttachment]{
opt.V(render.PlainTextureAttachment(s.pongTexture)),
},
Expand Down
9 changes: 9 additions & 0 deletions game/graphics/stage_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,19 @@ func (d *commonStageData) Allocate() {
d.coneShape = internal.CreateConeShape(d.api)

d.nearestSampler = d.api.CreateSampler(render.SamplerInfo{
Label: "Nearest Sampler",
Wrapping: render.WrapModeClamp,
Filtering: render.FilterModeNearest,
Mipmapping: false,
})
d.linearSampler = d.api.CreateSampler(render.SamplerInfo{
Label: "Linear Sampler",
Wrapping: render.WrapModeClamp,
Filtering: render.FilterModeLinear,
Mipmapping: false,
})
d.depthSampler = d.api.CreateSampler(render.SamplerInfo{
Label: "Depth Sampler",
Wrapping: render.WrapModeClamp,
Filtering: render.FilterModeLinear,
Comparison: opt.V(render.ComparisonLess),
Expand All @@ -90,6 +93,7 @@ func (d *commonStageData) Allocate() {

gog.Mutate(d.directionalShadowMaps, func(shadowMap *internal.DirectionalShadowMap) {
shadowMap.ArrayTexture = d.api.CreateDepthTexture2DArray(render.DepthTexture2DArrayInfo{
Label: "Directional Shadow Map Texture",
Width: uint32(d.directionalShadowMapSize),
Height: uint32(d.directionalShadowMapSize),
Layers: uint32(d.directionalShadowMapCascadeCount),
Expand All @@ -98,6 +102,7 @@ func (d *commonStageData) Allocate() {
shadowMap.Cascades = make([]internal.DirectionalShadowMapCascade, d.directionalShadowMapCascadeCount)
gog.MutateIndex(shadowMap.Cascades, func(j int, cascade *internal.DirectionalShadowMapCascade) {
cascade.Framebuffer = d.api.CreateFramebuffer(render.FramebufferInfo{
Label: "Directional Shadow Map Framebuffer",
DepthAttachment: opt.V(render.TextureAttachment{
Texture: shadowMap.ArrayTexture,
Depth: uint32(j),
Expand All @@ -108,24 +113,28 @@ func (d *commonStageData) Allocate() {

gog.Mutate(d.spotShadowMaps, func(shadowMap *internal.SpotShadowMap) {
shadowMap.Texture = d.api.CreateDepthTexture2D(render.DepthTexture2DInfo{
Label: "Spot Shadow Map Texture",
Width: uint32(d.spotShadowMapSize),
Height: uint32(d.spotShadowMapSize),
Comparable: true,
})
shadowMap.Framebuffer = d.api.CreateFramebuffer(render.FramebufferInfo{
Label: "Spot Shadow Map Framebuffer",
DepthAttachment: opt.V(render.PlainTextureAttachment(shadowMap.Texture)),
})
})

gog.Mutate(d.pointShadowMaps, func(shadowMap *internal.PointShadowMap) {
shadowMap.ArrayTexture = d.api.CreateDepthTexture2DArray(render.DepthTexture2DArrayInfo{
Label: "Point Shadow Map Texture",
Width: uint32(d.pointShadowMapSize),
Height: uint32(d.pointShadowMapSize),
Layers: 6,
Comparable: true,
})
for i := range 6 {
shadowMap.Framebuffers[i] = d.api.CreateFramebuffer(render.FramebufferInfo{
Label: "Point Shadow Map Framebuffer",
DepthAttachment: opt.V(render.TextureAttachment{
Texture: shadowMap.ArrayTexture,
Depth: uint32(i),
Expand Down
1 change: 1 addition & 0 deletions game/graphics/stage_depth_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func (s *DepthSourceStage) PostRender() {

func (s *DepthSourceStage) allocateTextures() {
s.depthTexture = s.api.CreateDepthTexture2D(render.DepthTexture2DInfo{
Label: "Depth Source Texture",
Width: s.framebufferWidth,
Height: s.framebufferHeight,
})
Expand Down
4 changes: 4 additions & 0 deletions game/graphics/stage_forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func (s *ForwardStage) allocateFramebuffer() {
s.depthTexture = s.input.DepthTexture()

s.framebuffer = s.api.CreateFramebuffer(render.FramebufferInfo{
Label: "Forward Framebuffer",
ColorAttachments: [4]opt.T[render.TextureAttachment]{
opt.V(render.PlainTextureAttachment(s.hdrTexture)),
},
Expand All @@ -121,6 +122,7 @@ func (s *ForwardStage) releaseFramebuffer() {
func (s *ForwardStage) allocateDebug() {
s.debugVertexData = make([]byte, debugBufferSize)
s.debugVertexBuffer = s.api.CreateVertexBuffer(render.BufferInfo{
Label: "Debug Vertex Buffer",
Dynamic: true,
Data: s.debugVertexData,
})
Expand All @@ -138,13 +140,15 @@ func (s *ForwardStage) allocateDebug() {
})

s.debugProgram = s.api.CreateProgram(render.ProgramInfo{
Label: "Debug Program",
SourceCode: s.shaders.DebugSet(),
TextureBindings: []render.TextureBinding{},
UniformBindings: []render.UniformBinding{
render.NewUniformBinding("Camera", internal.UniformBufferBindingCamera),
},
})
s.debugPipeline = s.api.CreatePipeline(render.PipelineInfo{
Label: "Debug Pipeline",
Program: s.debugProgram,
VertexArray: s.debugVertexArray,
Topology: render.TopologyLineList,
Expand Down
1 change: 1 addition & 0 deletions game/graphics/stage_forward_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func (s *ForwardSourceStage) PostRender() {

func (s *ForwardSourceStage) allocateTextures() {
s.hdrTexture = s.api.CreateColorTexture2D(render.ColorTexture2DInfo{
Label: "Forward Source Texture",
Width: s.framebufferWidth,
Height: s.framebufferHeight,
GenerateMipmaps: false,
Expand Down
1 change: 1 addition & 0 deletions game/graphics/stage_geometry.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func (s *GeometryStage) allocateFramebuffer() {
s.depthTexture = s.input.DepthTexture()

s.framebuffer = s.api.CreateFramebuffer(render.FramebufferInfo{
Label: "Geometry Framebuffer",
ColorAttachments: [4]opt.T[render.TextureAttachment]{
opt.V(render.PlainTextureAttachment(s.albedoMetallicTexture)),
opt.V(render.PlainTextureAttachment(s.normalRoughnessTexture)),
Expand Down
2 changes: 2 additions & 0 deletions game/graphics/stage_geometry_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,15 @@ func (s *GeometrySourceStage) PostRender() {

func (s *GeometrySourceStage) allocateTextures() {
s.albedoMetallicTexture = s.api.CreateColorTexture2D(render.ColorTexture2DInfo{
Label: "Geometry Source Albedo Texture",
Width: s.framebufferWidth,
Height: s.framebufferHeight,
GenerateMipmaps: false,
GammaCorrection: false,
Format: render.DataFormatRGBA8,
})
s.normalRoughnessTexture = s.api.CreateColorTexture2D(render.ColorTexture2DInfo{
Label: "Geometry Source Normal Texture",
Width: s.framebufferWidth,
Height: s.framebufferHeight,
GenerateMipmaps: false,
Expand Down
22 changes: 16 additions & 6 deletions game/graphics/stage_lighting.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func (s *LightingStage) allocateFramebuffer() {
s.hdrTexture = s.input.HDRTexture()

s.framebuffer = s.api.CreateFramebuffer(render.FramebufferInfo{
Label: "Lighting Framebuffer",
ColorAttachments: [4]opt.T[render.TextureAttachment]{
opt.V(render.PlainTextureAttachment(s.hdrTexture)),
},
Expand All @@ -143,13 +144,15 @@ func (s *LightingStage) allocatePipelines() {
coneShape := s.data.ConeShape()

s.noShadowTexture = s.api.CreateDepthTexture2D(render.DepthTexture2DInfo{
Label: "No Shadow Texture",
Width: 1,
Height: 1,
Comparable: true,
// TODO: Initialize to furthest possible depth value
})

s.ambientLightProgram = s.api.CreateProgram(render.ProgramInfo{
Label: "Ambient Light Program",
SourceCode: s.shaders.AmbientLightSet(),
TextureBindings: []render.TextureBinding{
render.NewTextureBinding("fbColor0TextureIn", internal.TextureBindingLightingFramebufferColor0),
Expand All @@ -163,6 +166,7 @@ func (s *LightingStage) allocatePipelines() {
},
})
s.ambientLightPipeline = s.api.CreatePipeline(render.PipelineInfo{
Label: "Ambient Light Pipeline",
Program: s.ambientLightProgram,
VertexArray: quadShape.VertexArray(),
Topology: quadShape.Topology(),
Expand All @@ -184,6 +188,7 @@ func (s *LightingStage) allocatePipelines() {
})

s.pointLightProgram = s.api.CreateProgram(render.ProgramInfo{
Label: "Point Light Program",
SourceCode: s.shaders.PointLightSet(),
TextureBindings: []render.TextureBinding{
render.NewTextureBinding("fbColor0TextureIn", internal.TextureBindingLightingFramebufferColor0),
Expand All @@ -196,6 +201,7 @@ func (s *LightingStage) allocatePipelines() {
},
})
s.pointLightPipeline = s.api.CreatePipeline(render.PipelineInfo{
Label: "Point Light Pipeline",
Program: s.pointLightProgram,
VertexArray: sphereShape.VertexArray(),
Topology: sphereShape.Topology(),
Expand All @@ -217,6 +223,7 @@ func (s *LightingStage) allocatePipelines() {
})

s.spotLightProgram = s.api.CreateProgram(render.ProgramInfo{
Label: "Spot Light Program",
SourceCode: s.shaders.SpotLightSet(),
TextureBindings: []render.TextureBinding{
render.NewTextureBinding("fbColor0TextureIn", internal.TextureBindingLightingFramebufferColor0),
Expand All @@ -229,6 +236,7 @@ func (s *LightingStage) allocatePipelines() {
},
})
s.spotLightPipeline = s.api.CreatePipeline(render.PipelineInfo{
Label: "Spot Light Pipeline",
Program: s.spotLightProgram,
VertexArray: coneShape.VertexArray(),
Topology: coneShape.Topology(),
Expand All @@ -250,6 +258,7 @@ func (s *LightingStage) allocatePipelines() {
})

s.directionalLightProgram = s.api.CreateProgram(render.ProgramInfo{
Label: "Directional Light Program",
SourceCode: s.shaders.DirectionalLightSet(),
TextureBindings: []render.TextureBinding{
render.NewTextureBinding("fbColor0TextureIn", internal.TextureBindingLightingFramebufferColor0),
Expand All @@ -263,6 +272,7 @@ func (s *LightingStage) allocatePipelines() {
},
})
s.directionalLightPipeline = s.api.CreatePipeline(render.PipelineInfo{
Label: "Directional Light Pipeline",
Program: s.directionalLightProgram,
VertexArray: quadShape.VertexArray(),
Topology: quadShape.Topology(),
Expand Down Expand Up @@ -342,8 +352,8 @@ func (s *LightingStage) renderPointLight(ctx StageContext, light *PointLight) {
depthTexture := s.input.DepthTexture()

lightPlacement := ubo.WriteUniform(uniformBuffer, internal.LightUniform{
ShadowMatrices: [8]sprec.Mat4{}, // irrelevant
ShadowCascades: [8]sprec.Vec2{}, // irrelevant
ShadowMatrices: [4]sprec.Mat4{}, // irrelevant
ShadowCascades: [4]sprec.Vec2{}, // irrelevant
ModelMatrix: light.gfxMatrix(),
Color: dtos.Vec3(light.emitColor),
Intensity: 1.0,
Expand Down Expand Up @@ -384,8 +394,8 @@ func (s *LightingStage) renderSpotLight(ctx StageContext, light *SpotLight) {
depthTexture := s.input.DepthTexture()

lightPlacement := ubo.WriteUniform(uniformBuffer, internal.LightUniform{
ShadowMatrices: [8]sprec.Mat4{}, // irrelevant
ShadowCascades: [8]sprec.Vec2{}, // irrelevant
ShadowMatrices: [4]sprec.Mat4{}, // irrelevant
ShadowCascades: [4]sprec.Vec2{}, // irrelevant
ModelMatrix: light.gfxMatrix(),
Color: dtos.Vec3(light.emitColor),
Intensity: 1.0,
Expand Down Expand Up @@ -429,9 +439,9 @@ func (s *LightingStage) renderDirectionalLight(ctx StageContext, light *Directio
depthTexture := s.input.DepthTexture()

lightUniform := internal.LightUniform{
ShadowMatrices: [8]sprec.Mat4{},
ShadowMatrices: [4]sprec.Mat4{},
ShadowCascades: [4]sprec.Vec2{},
ModelMatrix: light.gfxMatrix(),
ShadowCascades: [8]sprec.Vec2{},
Color: dtos.Vec3(light.emitColor),
Intensity: 1.0,
Range: 0.0, // irrelevant
Expand Down
Loading

0 comments on commit 301338e

Please sign in to comment.