Fix ubo alignment+Move point lights to ubo

This commit is contained in:
bloeys
2024-09-15 15:51:26 +04:00
parent 91807a4093
commit bcb46d1699
5 changed files with 102 additions and 406 deletions

View File

@ -173,7 +173,7 @@ func (dt ElementType) Size() int32 {
} }
} }
func (dt ElementType) GlStd140BaseAlignment() uint8 { func (dt ElementType) GlStd140SizeBytes() uint8 {
switch dt { switch dt {
@ -187,22 +187,22 @@ func (dt ElementType) GlStd140BaseAlignment() uint8 {
case DataTypeVec2: case DataTypeVec2:
return 4 * 2 return 4 * 2
// Vec3 has the same alignment as vec4
case DataTypeVec3: case DataTypeVec3:
fallthrough return 4 * 3
case DataTypeVec4: case DataTypeVec4:
return 4 * 4 return 4 * 4
// Matrices follow: (vec4Alignment) * numColumns // Matrices follow: (vec4Alignment) * numColumns
case DataTypeMat2: case DataTypeMat2:
return (4 * 4) * 2 return 2 * 2 * 4
case DataTypeMat3: case DataTypeMat3:
return (4 * 4) * 3 return 3 * 3 * 4
case DataTypeMat4: case DataTypeMat4:
return (4 * 4) * 4 return 4 * 4 * 4
case DataTypeStruct: case DataTypeStruct:
logging.ErrLog.Fatalf("ElementType.GlStd140BaseAlignment of DataTypeStruct is not supported") logging.ErrLog.Fatalf("ElementType.GlStd140SizeBytes of DataTypeStruct is not supported")
return 0 return 0
default: default:

View File

@ -88,6 +88,8 @@ func addUniformBufferFieldsToArray(startAlignedOffset uint16, arrayToAddTo *[]Un
// offset + (boundary - alignErr) == 100 + (16 - 4) == 112; 112 % 16 == 0, meaning its a boundary // offset + (boundary - alignErr) == 100 + (16 - 4) == 112; 112 % 16 == 0, meaning its a boundary
// //
// Note that arrays of scalars/vectors are always aligned to 16 bytes, like a vec4 // Note that arrays of scalars/vectors are always aligned to 16 bytes, like a vec4
//
// Official spec and full details in subsection 'Standard Uniform Block Layout' at http://www.opengl.org/registry/specs/ARB/uniform_buffer_object.txt
var alignmentBoundary uint16 = 16 var alignmentBoundary uint16 = 16
if f.Count == 1 { if f.Count == 1 {
alignmentBoundary = f.Type.GlStd140AlignmentBoundary() alignmentBoundary = f.Type.GlStd140AlignmentBoundary()
@ -124,7 +126,19 @@ func addUniformBufferFieldsToArray(startAlignedOffset uint16, arrayToAddTo *[]Un
alignedOffset += subfieldsAlignedOffset * f.Count alignedOffset += subfieldsAlignedOffset * f.Count
} else { } else {
alignedOffset = newField.AlignedOffset + alignmentBoundary*f.Count*multiplier - startAlignedOffset
// Elements advance the alignedOffset by their actual byte size.
// Aligned offset is padded if the place its at is not aligned to the boundary required by the next element.
//
// The exception is structs, because fields after a struct field are always aligned at a 16 byte boundary.
//
// For example, a vec3 starting at offset 80, taking 12 bytes, would put the aligned offset at 92.
// If the next element is a float32 (alignment boundary = 4) then no padding is required and
// the float will start at 92 and end at 96.
// However, if the element after the vec3 is a vec3 (alignment boundary = 16), then it would require
// a padding of 4 bytes so that it can start at 96, which is aligned to 16. In this case the second vec3
// would start at 96 and end at 96+12=108.
alignedOffset = newField.AlignedOffset + uint16(f.Type.GlStd140SizeBytes())*f.Count*multiplier - startAlignedOffset
} }
} }
@ -224,8 +238,8 @@ func setStruct(fields []UniformBufferField, buf []byte, inputStruct any, maxFiel
logging.ErrLog.Panicf("UniformBuffer.SetStruct called with a value that is not a struct. Val=%v\n", inputStruct) logging.ErrLog.Panicf("UniformBuffer.SetStruct called with a value that is not a struct. Val=%v\n", inputStruct)
} }
// Needed because fieldIndex can move faster than struct fields in case of struct fields
structFieldIndex := 0 structFieldIndex := 0
// structFieldCount := structVal.NumField()
for fieldIndex := 0; fieldIndex < len(fields) && fieldIndex < maxFieldsToConsume; fieldIndex++ { for fieldIndex := 0; fieldIndex < len(fields) && fieldIndex < maxFieldsToConsume; fieldIndex++ {
ubField := &fields[fieldIndex] ubField := &fields[fieldIndex]

450
main.go
View File

@ -3,7 +3,6 @@ package main
import ( import (
"fmt" "fmt"
"os" "os"
"reflect"
"runtime" "runtime"
"runtime/pprof" "runtime/pprof"
"strconv" "strconv"
@ -98,6 +97,10 @@ type PointLight struct {
Radius float32 Radius float32
Falloff float32 Falloff float32
// MaxBias is the max shadow bias applied for this light.
// A usual value is 0.05
MaxBias float32
// NearPlane is the distance where if the pixel // NearPlane is the distance where if the pixel
// is closer to the light than this distance, no shadow will be casted. // is closer to the light than this distance, no shadow will be casted.
// //
@ -105,10 +108,6 @@ type PointLight struct {
// Same idea a camera near plane. // Same idea a camera near plane.
NearPlane float32 NearPlane float32
// MaxBias is the max shadow bias applied for this light.
// A usual value is 0.05
MaxBias float32
// Far plane is the max distance at which shadows from this // Far plane is the max distance at which shadows from this
// light will show. // light will show.
// //
@ -217,13 +216,30 @@ type DirLightUboData struct {
Dir gglm.Vec3 Dir gglm.Vec3
DiffuseColor gglm.Vec3 DiffuseColor gglm.Vec3
SpecularColor gglm.Vec3 SpecularColor gglm.Vec3
Shadowmap int32
} }
type PointLightUboData struct {
Pos gglm.Vec3
DiffuseColor gglm.Vec3
SpecularColor gglm.Vec3
Radius float32
Falloff float32
MaxBias float32
NearPlane float32
FarPlane float32
}
type LightsUboData struct { type LightsUboData struct {
DirLight DirLightUboData DirLight DirLightUboData
PointLights [POINT_LIGHT_COUNT]PointLightUboData
} }
const ( const (
// These must match the shader values
POINT_LIGHT_COUNT = 8
SPOT_LIGHT_COUNT = 4
UNSCALED_WINDOW_WIDTH = 1280 UNSCALED_WINDOW_WIDTH = 1280
UNSCALED_WINDOW_HEIGHT = 720 UNSCALED_WINDOW_HEIGHT = 720
@ -316,13 +332,13 @@ var (
DiffuseColor: gglm.NewVec3(63.0/255, 63.0/255, 63.0/255), DiffuseColor: gglm.NewVec3(63.0/255, 63.0/255, 63.0/255),
SpecularColor: gglm.NewVec3(1, 1, 1), SpecularColor: gglm.NewVec3(1, 1, 1),
} }
pointLights = [...]PointLight{ pointLights = [POINT_LIGHT_COUNT]PointLight{
{ {
Pos: gglm.NewVec3(0, 4, -3), Pos: gglm.NewVec3(0, 4, -3),
DiffuseColor: gglm.NewVec3(1, 0, 0), DiffuseColor: gglm.NewVec3(1, 0, 0),
SpecularColor: gglm.NewVec3(1, 1, 1), SpecularColor: gglm.NewVec3(1, 1, 1),
Falloff: 1.0,
Radius: 10, Radius: 10,
Falloff: 1.0,
MaxBias: 0.05, MaxBias: 0.05,
NearPlane: 0.2, NearPlane: 0.2,
FarPlane: 20 * pointLightRadiusToFarPlaneRatio, FarPlane: 20 * pointLightRadiusToFarPlaneRatio,
@ -331,8 +347,8 @@ var (
Pos: gglm.NewVec3(5, 0, 0), Pos: gglm.NewVec3(5, 0, 0),
DiffuseColor: gglm.NewVec3(1, 1, 1), DiffuseColor: gglm.NewVec3(1, 1, 1),
SpecularColor: gglm.NewVec3(1, 1, 1), SpecularColor: gglm.NewVec3(1, 1, 1),
Falloff: 1.0,
Radius: 10, Radius: 10,
Falloff: 1.0,
MaxBias: 0.05, MaxBias: 0.05,
NearPlane: 0.2, NearPlane: 0.2,
FarPlane: 20 * pointLightRadiusToFarPlaneRatio, FarPlane: 20 * pointLightRadiusToFarPlaneRatio,
@ -341,8 +357,8 @@ var (
Pos: gglm.NewVec3(-3, 4, 3), Pos: gglm.NewVec3(-3, 4, 3),
DiffuseColor: gglm.NewVec3(1, 1, 1), DiffuseColor: gglm.NewVec3(1, 1, 1),
SpecularColor: gglm.NewVec3(1, 1, 1), SpecularColor: gglm.NewVec3(1, 1, 1),
Falloff: 1.0,
Radius: 10, Radius: 10,
Falloff: 1.0,
MaxBias: 0.05, MaxBias: 0.05,
NearPlane: 0.2, NearPlane: 0.2,
FarPlane: 20 * pointLightRadiusToFarPlaneRatio, FarPlane: 20 * pointLightRadiusToFarPlaneRatio,
@ -350,7 +366,7 @@ var (
} }
spotLightDir0 = gglm.NewVec3(1.5, -0.9, 0) spotLightDir0 = gglm.NewVec3(1.5, -0.9, 0)
spotLights = [...]SpotLight{ spotLights = [SPOT_LIGHT_COUNT]SpotLight{
{ {
Pos: gglm.NewVec3(-4, 7, 5), Pos: gglm.NewVec3(-4, 7, 5),
Dir: *spotLightDir0.Normalize(), Dir: *spotLightDir0.Normalize(),
@ -673,15 +689,16 @@ func (g *Game) Init() {
// Fbos and lights // Fbos and lights
g.initFbos() g.initFbos()
g.updateLights() g.applyLightUpdates()
// Ubos // Ubos
g.initUbos() g.initUbos()
testUbos()
// Initial camera update // Initial camera update
cam.Update() cam.Update()
updateAllProjViewMats(cam.ProjMat, cam.ViewMat) updateAllProjViewMats(cam.ProjMat, cam.ViewMat)
g.applyLightUpdates()
} }
func (g *Game) initUbos() { func (g *Game) initUbos() {
@ -701,15 +718,33 @@ func (g *Game) initUbos() {
lightsUbo = buffers.NewUniformBuffer( lightsUbo = buffers.NewUniformBuffer(
[]buffers.UniformBufferFieldInput{ []buffers.UniformBufferFieldInput{
{Id: 0, Type: buffers.DataTypeStruct, Subfields: []buffers.UniformBufferFieldInput{ // Dir light
{Id: 1, Type: buffers.DataTypeVec3}, {Id: 0, Type: buffers.DataTypeStruct,
{Id: 2, Type: buffers.DataTypeVec3}, Subfields: []buffers.UniformBufferFieldInput{
{Id: 3, Type: buffers.DataTypeVec3}, {Id: 1, Type: buffers.DataTypeVec3}, // 12 00
{Id: 4, Type: buffers.DataTypeInt32}, {Id: 2, Type: buffers.DataTypeVec3}, // 12 16
}}, {Id: 3, Type: buffers.DataTypeVec3}, // 12 32
},
},
// Point lights
{Id: 5, Type: buffers.DataTypeStruct,
Count: POINT_LIGHT_COUNT,
Subfields: []buffers.UniformBufferFieldInput{
{Id: 6, Type: buffers.DataTypeVec3}, // 12 48
{Id: 7, Type: buffers.DataTypeVec3}, // 12 64
{Id: 8, Type: buffers.DataTypeVec3}, // 12 80
{Id: 9, Type: buffers.DataTypeFloat32}, // 04 92
{Id: 10, Type: buffers.DataTypeFloat32}, // 04 96
{Id: 11, Type: buffers.DataTypeFloat32}, // 04 100
{Id: 12, Type: buffers.DataTypeFloat32}, // 04 104
{Id: 13, Type: buffers.DataTypeFloat32}, // 04 108
},
},
}, },
) )
fmt.Printf("\n==Lights UBO (id=%d)==\nSize=%d\nFields: %+v\n\n", lightsUbo.Id, lightsUbo.Size, lightsUbo.Fields)
lightsUbo.SetBindPoint(1) lightsUbo.SetBindPoint(1)
groundMat.SetUniformBlockBindingPoint("Lights", 1) groundMat.SetUniformBlockBindingPoint("Lights", 1)
whiteMat.SetUniformBlockBindingPoint("Lights", 1) whiteMat.SetUniformBlockBindingPoint("Lights", 1)
@ -717,256 +752,6 @@ func (g *Game) initUbos() {
palleteMat.SetUniformBlockBindingPoint("Lights", 1) palleteMat.SetUniformBlockBindingPoint("Lights", 1)
} }
func testUbos() {
xx := []int{1, 2, 3, 4}
xx2 := [4]int{1, 2, 3, 4}
fmt.Printf("XX: %v; Kind: %v; Elem Type: %v\n", reflect.ValueOf(xx), reflect.ValueOf(xx).Type().Kind(), reflect.ValueOf(xx).Type().Elem().Kind())
fmt.Printf("XX: %v; Kind: %v; Elem Type: %v\n", reflect.ValueOf(xx2), reflect.ValueOf(xx).Kind(), reflect.ValueOf(xx).Type().Elem().Kind())
ubo := buffers.NewUniformBuffer([]buffers.UniformBufferFieldInput{
{Id: 0, Type: buffers.DataTypeFloat32}, // 04 00
{Id: 1, Type: buffers.DataTypeVec3}, // 16 16
{Id: 2, Type: buffers.DataTypeFloat32}, // 04 32
{Id: 3, Type: buffers.DataTypeMat2}, // 32 48
}) // Total size: 48+32 = 80
ubo.Bind()
println("!!!!!!!!!!!!! Id:", ubo.Id, "; Size:", ubo.Size)
fmt.Printf("%+v\n", ubo.Fields)
ubo.SetFloat32(0, 99)
ubo.SetFloat32(2, 199)
ubo.SetVec3(1, &gglm.Vec3{Data: [3]float32{33, 33, 33}})
ubo.SetMat2(3, &gglm.Mat2{Data: [2][2]float32{{1, 3}, {2, 4}}})
var v gglm.Vec3
var m2 gglm.Mat2
var x, x2 float32
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 0, 4, gl.Ptr(&x))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 32, 4, gl.Ptr(&x2))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 16, 12, gl.Ptr(&v.Data[0]))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 48, 16, gl.Ptr(&m2.Data[0][0]))
fmt.Printf("x=%f; x2=%f; v3=%s; m2=%s\n", x, x2, v.String(), m2.String())
ubo.SetVec3(1, &gglm.Vec3{Data: [3]float32{-123, 33, 33}})
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 16, 12, gl.Ptr(&v.Data[0]))
type TestUBO struct {
FirstF32 float32
V3 gglm.Vec3
SecondF32 float32
M2 gglm.Mat2
}
s := TestUBO{
FirstF32: 1.5,
V3: gglm.Vec3{Data: [3]float32{11, 22, 33}},
SecondF32: 9.5,
M2: gglm.Mat2{Data: [2][2]float32{{6, 8}, {7, 9}}},
}
ubo.SetStruct(s)
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 0, 4, gl.Ptr(&x))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 32, 4, gl.Ptr(&x2))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 16, 12, gl.Ptr(&v.Data[0]))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 48, 16, gl.Ptr(&m2.Data[0][0]))
fmt.Printf("x=%f; x2=%f; v3=%s; m2=%s\n", x, x2, v.String(), m2.String())
//
// Ubo2
//
type TestUBO2 struct {
F32 float32
V3 gglm.Vec3
F32Slice []float32
I32 int32
I32Slice []int32
V3Slice []gglm.Vec3
V4Slice []gglm.Vec4
Mat2Slice []gglm.Mat2
Mat3Slice []gglm.Mat3
Mat4Slice []gglm.Mat4
}
s2 := TestUBO2{
F32: 1.5,
V3: gglm.Vec3{Data: [3]float32{11, 22, 33}},
F32Slice: []float32{-1, -2, -3, -4},
I32: 55,
I32Slice: []int32{41, 42, 43},
V3Slice: []gglm.Vec3{gglm.NewVec3(1.1, 1.2, 1.3), gglm.NewVec3(2.1, 2.2, 2.3)},
V4Slice: []gglm.Vec4{gglm.NewVec4(1.1, 1.2, 1.3, 1.4), gglm.NewVec4(2.1, 2.2, 2.3, 2.4)},
Mat2Slice: []gglm.Mat2{gglm.NewMat2Diag(1.1), gglm.NewMat2Diag(2.1)},
Mat3Slice: []gglm.Mat3{gglm.NewMat3Diag(3.1), gglm.NewMat3Diag(4.1)},
Mat4Slice: []gglm.Mat4{gglm.NewMat4Diag(5.1), gglm.NewMat4Diag(6.1)},
}
ubo2 := buffers.NewUniformBuffer([]buffers.UniformBufferFieldInput{
{Id: 0, Type: buffers.DataTypeFloat32},
{Id: 1, Type: buffers.DataTypeVec3},
{Id: 2, Type: buffers.DataTypeFloat32, Count: 4},
{Id: 3, Type: buffers.DataTypeInt32},
{Id: 4, Type: buffers.DataTypeInt32, Count: 3},
{Id: 5, Type: buffers.DataTypeVec3, Count: 2},
{Id: 6, Type: buffers.DataTypeVec4, Count: 2},
{Id: 7, Type: buffers.DataTypeMat2, Count: 2},
{Id: 8, Type: buffers.DataTypeMat3, Count: 2},
{Id: 9, Type: buffers.DataTypeMat4, Count: 2},
})
ubo2.Bind()
ubo2.SetStruct(s2)
var someInt32 int32
fArr := [4 * 4]float32{}
i32Arr := [3 * 4]int32{}
vec3Slice := [2 * 4]float32{}
vec4Slice := [2 * 4]float32{}
mat2Slice := [2 * 2 * 4]float32{}
mat3Slice := [2 * 3 * 4]float32{}
mat4Slice := [2 * 4 * 4]float32{}
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 0, 4, gl.Ptr(&x))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 16, 12, gl.Ptr(&v.Data[0]))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 32, 16*4, gl.Ptr(&fArr[0]))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 32+16*4, 4, gl.Ptr(&someInt32))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 32+16*4+16, 16*3, gl.Ptr(&i32Arr[0]))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 32+16*4+16+16*3, 16*2, gl.Ptr(&vec3Slice[0]))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 32+16*4+16+16*3+16*2, 16*2, gl.Ptr(&vec4Slice[0]))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 32+16*4+16+16*3+16*2+16*2, 2*16*2, gl.Ptr(&mat2Slice[0]))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 32+16*4+16+16*3+16*2+16*2+2*16*2, 2*16*3, gl.Ptr(&mat3Slice[0]))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 32+16*4+16+16*3+16*2+16*2+2*16*2+2*16*3, 2*16*4, gl.Ptr(&mat4Slice[0]))
fmt.Printf("f32=%f; v3=%s; f32Slice=%v; i32=%d; i32Arr=%v; v3Slice=%v; v4Slice=%v; mat2Slice=%v; mat3Slice=%v; mat4Slice=%v\n", x, v.String(), fArr, someInt32, i32Arr, vec3Slice, vec4Slice, mat2Slice, mat3Slice, mat4Slice)
//
// Ubo3
//
type TestUBO3_Z struct {
V int32
}
type TestUBO3_Y struct {
V TestUBO3_Z
}
type TestUBO3_X struct {
V TestUBO3_Y
}
type TestUBO3_0 struct {
X int32
}
type TestUBO3_1 struct {
F32 float32
V3 gglm.Vec3
Zero TestUBO3_0
}
type TestUBO3_2 struct {
F32 float32
S TestUBO3_1
XX int32
Z2 TestUBO3_0
XX2 int32
Abcd [2]TestUBO3_X
XX3 int32
}
ubo3 := buffers.NewUniformBuffer([]buffers.UniformBufferFieldInput{
{Id: 0, Type: buffers.DataTypeFloat32}, // 04 00
{Id: 1, Type: buffers.DataTypeStruct, Subfields: []buffers.UniformBufferFieldInput{ // 00 16
{Id: 2, Type: buffers.DataTypeFloat32}, // 04 16
{Id: 3, Type: buffers.DataTypeVec3}, // 16 32
{Id: 4, Type: buffers.DataTypeStruct, Subfields: []buffers.UniformBufferFieldInput{ // 00 48
{Id: 5, Type: buffers.DataTypeInt32}, // 04 48
}},
}},
{Id: 6, Type: buffers.DataTypeInt32}, // 04 64
{Id: 7, Type: buffers.DataTypeStruct, Subfields: []buffers.UniformBufferFieldInput{ // 00 80
{Id: 8, Type: buffers.DataTypeInt32}, // 04 80
}},
{Id: 9, Type: buffers.DataTypeInt32}, // 04 96
{Id: 10, Type: buffers.DataTypeStruct, Count: 2, Subfields: []buffers.UniformBufferFieldInput{ // 00 112
{Id: 11, Type: buffers.DataTypeStruct, Subfields: []buffers.UniformBufferFieldInput{ // 00 112
{Id: 12, Type: buffers.DataTypeStruct, Subfields: []buffers.UniformBufferFieldInput{ // 00 112
{Id: 13, Type: buffers.DataTypeInt32}, // 04 112
}},
}},
}},
{Id: 14, Type: buffers.DataTypeInt32},
}) // 116
ubo3.Bind()
ubo3.SetBindPoint(2)
groundMat.SetUniformBlockBindingPoint("Test2", 2)
fmt.Printf("\n==UBO3 (id=%d)==\nSize=%d\nFields: %+v\n\n", ubo3.Id, ubo3.Size, ubo3.Fields)
s3 := TestUBO3_2{
F32: 76.1,
S: TestUBO3_1{
F32: 89.9,
V3: gglm.NewVec3(7.1, 7.2, 7.3),
Zero: TestUBO3_0{
X: 33,
},
},
XX: 41,
Z2: TestUBO3_0{
X: 8,
},
XX2: 321,
Abcd: [2]TestUBO3_X{
{
V: TestUBO3_Y{
V: TestUBO3_Z{
V: 9911,
},
},
},
{
V: TestUBO3_Y{
V: TestUBO3_Z{
V: 9922,
},
},
},
},
XX3: 818,
}
ubo3.SetStruct(s3)
ubo3F32 := float32(0.0)
ubo3SF32 := float32(0.0)
ubo3SV3 := gglm.Vec3{}
ubo3SZeroX := 0
ubo3Xx := 0
ubo3SZ2X := 0
ubo3Xx2 := 0
ubo3AbcdV1 := 0
ubo3AbcdV2 := 0
ubo3Xx3 := 0
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 0, 4, gl.Ptr(&ubo3F32))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 16, 4, gl.Ptr(&ubo3SF32))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 32, 16, gl.Ptr(&ubo3SV3.Data[0]))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 48, 4, gl.Ptr(&ubo3SZeroX))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 64, 4, gl.Ptr(&ubo3Xx))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 64, 4, gl.Ptr(&ubo3Xx))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 80, 4, gl.Ptr(&ubo3SZ2X))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 96, 4, gl.Ptr(&ubo3Xx2))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 112, 4, gl.Ptr(&ubo3AbcdV1))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 128, 4, gl.Ptr(&ubo3AbcdV2))
gl.GetBufferSubData(gl.UNIFORM_BUFFER, 144, 4, gl.Ptr(&ubo3Xx3))
fmt.Printf("ubo3_f32=%f\nubo3_s_f32=%f\nubo3_s_v3=%s\nubo3_s_zero_x=%d\nubo3_xx=%d\nubo3_z2_x=%d\nubo3_xx2=%d\nubo3_abcd_v1=%d\nubo3_abcd_v2=%d\nubo3_xx3=%d\n", ubo3F32, ubo3SF32, ubo3SV3.String(), ubo3SZeroX, ubo3Xx, ubo3SZ2X, ubo3Xx2, ubo3AbcdV1, ubo3AbcdV2, ubo3Xx3)
}
func (g *Game) initFbos() { func (g *Game) initFbos() {
// @TODO: Resize window sized fbos on window resize // @TODO: Resize window sized fbos on window resize
@ -1031,73 +816,22 @@ func (g *Game) initFbos() {
assert.T(hdrFbo.IsComplete(), "Hdr fbo is not complete after init") assert.T(hdrFbo.IsComplete(), "Hdr fbo is not complete after init")
} }
func (g *Game) updateLights() { // applyLightUpdates updates materials and light ubo using
// data from the game's light structs
func (g *Game) applyLightUpdates() {
// Directional light // Directional light
lightsUboData.DirLight = DirLightUboData{ lightsUboData.DirLight = DirLightUboData(dirLight)
Dir: dirLight.Dir, whiteMat.ShadowMapTex1 = dirLightDepthMapFbo.Attachments[0].Id
DiffuseColor: dirLight.DiffuseColor, containerMat.ShadowMapTex1 = dirLightDepthMapFbo.Attachments[0].Id
SpecularColor: dirLight.SpecularColor, groundMat.ShadowMapTex1 = dirLightDepthMapFbo.Attachments[0].Id
Shadowmap: int32(dirLightDepthMapFbo.Attachments[0].Id), palleteMat.ShadowMapTex1 = dirLightDepthMapFbo.Attachments[0].Id
}
whiteMat.ShadowMapTex1 = uint32(lightsUboData.DirLight.Shadowmap)
containerMat.ShadowMapTex1 = uint32(lightsUboData.DirLight.Shadowmap)
groundMat.ShadowMapTex1 = uint32(lightsUboData.DirLight.Shadowmap)
palleteMat.ShadowMapTex1 = uint32(lightsUboData.DirLight.Shadowmap)
// Point lights // Point lights
for i := 0; i < len(pointLights); i++ { for i := 0; i < len(pointLights); i++ {
p := &pointLights[i] p := &pointLights[i]
indexString := "pointLights[" + strconv.Itoa(i) + "]" lightsUboData.PointLights[i] = PointLightUboData(*p)
posStr := indexString + ".pos"
whiteMat.SetUnifVec3(posStr, &p.Pos)
containerMat.SetUnifVec3(posStr, &p.Pos)
groundMat.SetUnifVec3(posStr, &p.Pos)
palleteMat.SetUnifVec3(posStr, &p.Pos)
diffuseStr := indexString + ".diffuseColor"
whiteMat.SetUnifVec3(diffuseStr, &p.DiffuseColor)
containerMat.SetUnifVec3(diffuseStr, &p.DiffuseColor)
groundMat.SetUnifVec3(diffuseStr, &p.DiffuseColor)
palleteMat.SetUnifVec3(diffuseStr, &p.DiffuseColor)
specularStr := indexString + ".specularColor"
whiteMat.SetUnifVec3(specularStr, &p.SpecularColor)
containerMat.SetUnifVec3(specularStr, &p.SpecularColor)
groundMat.SetUnifVec3(specularStr, &p.SpecularColor)
palleteMat.SetUnifVec3(specularStr, &p.SpecularColor)
falloffStr := indexString + ".falloff"
whiteMat.SetUnifFloat32(falloffStr, p.Falloff)
containerMat.SetUnifFloat32(falloffStr, p.Falloff)
groundMat.SetUnifFloat32(falloffStr, p.Falloff)
palleteMat.SetUnifFloat32(falloffStr, p.Falloff)
radiusStr := indexString + ".radius"
whiteMat.SetUnifFloat32(radiusStr, p.Radius)
containerMat.SetUnifFloat32(radiusStr, p.Radius)
groundMat.SetUnifFloat32(radiusStr, p.Radius)
palleteMat.SetUnifFloat32(radiusStr, p.Radius)
maxBiasStr := indexString + ".maxBias"
whiteMat.SetUnifFloat32(maxBiasStr, p.MaxBias)
containerMat.SetUnifFloat32(maxBiasStr, p.MaxBias)
groundMat.SetUnifFloat32(maxBiasStr, p.MaxBias)
palleteMat.SetUnifFloat32(maxBiasStr, p.MaxBias)
nearPlaneStr := indexString + ".nearPlane"
whiteMat.SetUnifFloat32(nearPlaneStr, p.NearPlane)
containerMat.SetUnifFloat32(nearPlaneStr, p.NearPlane)
groundMat.SetUnifFloat32(nearPlaneStr, p.NearPlane)
palleteMat.SetUnifFloat32(nearPlaneStr, p.NearPlane)
farPlaneStr := indexString + ".farPlane"
whiteMat.SetUnifFloat32(farPlaneStr, p.FarPlane)
containerMat.SetUnifFloat32(farPlaneStr, p.FarPlane)
groundMat.SetUnifFloat32(farPlaneStr, p.FarPlane)
palleteMat.SetUnifFloat32(farPlaneStr, p.FarPlane)
} }
whiteMat.CubemapArrayTex = pointLightDepthMapFbo.Attachments[0].Id whiteMat.CubemapArrayTex = pointLightDepthMapFbo.Attachments[0].Id
@ -1289,73 +1023,29 @@ func (g *Game) showDebugWindow() {
continue continue
} }
indexString := "pointLights[" + indexNumString + "]"
if imgui.DragFloat3("Pos", &pl.Pos.Data) { if imgui.DragFloat3("Pos", &pl.Pos.Data) {
updateLights = true
posStr := indexString + ".pos"
whiteMat.SetUnifVec3(posStr, &pl.Pos)
containerMat.SetUnifVec3(posStr, &pl.Pos)
groundMat.SetUnifVec3(posStr, &pl.Pos)
palleteMat.SetUnifVec3(posStr, &pl.Pos)
} }
if imgui.ColorEdit3("Diffuse Color", &pl.DiffuseColor.Data) { if imgui.ColorEdit3("Diffuse Color", &pl.DiffuseColor.Data) {
updateLights = true
diffStr := indexString + ".diffuseColor"
whiteMat.SetUnifVec3(diffStr, &pl.DiffuseColor)
containerMat.SetUnifVec3(diffStr, &pl.DiffuseColor)
groundMat.SetUnifVec3(diffStr, &pl.DiffuseColor)
palleteMat.SetUnifVec3(diffStr, &pl.DiffuseColor)
} }
if imgui.ColorEdit3("Specular Color", &pl.SpecularColor.Data) { if imgui.ColorEdit3("Specular Color", &pl.SpecularColor.Data) {
updateLights = true
specularStr := indexString + ".specularColor"
whiteMat.SetUnifVec3(specularStr, &pl.SpecularColor)
containerMat.SetUnifVec3(specularStr, &pl.SpecularColor)
groundMat.SetUnifVec3(specularStr, &pl.SpecularColor)
palleteMat.SetUnifVec3(specularStr, &pl.SpecularColor)
} }
if imgui.DragFloatV("Falloff", &pl.Falloff, 0.1, 0, 100, "%.3f", imgui.SliderFlagsNone) { if imgui.DragFloatV("Falloff", &pl.Falloff, 0.1, 0, 100, "%.3f", imgui.SliderFlagsNone) {
updateLights = true
falloffStr := indexString + ".falloff"
whiteMat.SetUnifFloat32(falloffStr, pl.Falloff)
containerMat.SetUnifFloat32(falloffStr, pl.Falloff)
groundMat.SetUnifFloat32(falloffStr, pl.Falloff)
palleteMat.SetUnifFloat32(falloffStr, pl.Falloff)
} }
if imgui.DragFloatV("Radius", &pl.Radius, 0.2, 0, 500, "%.3f", imgui.SliderFlagsNone) { if imgui.DragFloatV("Radius", &pl.Radius, 0.2, 0, 500, "%.3f", imgui.SliderFlagsNone) {
updateLights = true
radiusStr := indexString + ".radius"
whiteMat.SetUnifFloat32(radiusStr, pl.Radius)
containerMat.SetUnifFloat32(radiusStr, pl.Radius)
groundMat.SetUnifFloat32(radiusStr, pl.Radius)
palleteMat.SetUnifFloat32(radiusStr, pl.Radius)
farPlaneStr := indexString + ".farPlane"
pl.FarPlane = pl.Radius * pointLightRadiusToFarPlaneRatio pl.FarPlane = pl.Radius * pointLightRadiusToFarPlaneRatio
whiteMat.SetUnifFloat32(farPlaneStr, pl.FarPlane)
containerMat.SetUnifFloat32(farPlaneStr, pl.FarPlane)
groundMat.SetUnifFloat32(farPlaneStr, pl.FarPlane)
palleteMat.SetUnifFloat32(farPlaneStr, pl.FarPlane)
} }
if imgui.DragFloatV("Max Bias", &pl.MaxBias, 0.01, 0, 10, "%.3f", imgui.SliderFlagsNone) { if imgui.DragFloatV("Max Bias", &pl.MaxBias, 0.01, 0, 10, "%.3f", imgui.SliderFlagsNone) {
updateLights = true
maxBiasStr := indexString + ".maxBias"
whiteMat.SetUnifFloat32(maxBiasStr, pl.MaxBias)
containerMat.SetUnifFloat32(maxBiasStr, pl.MaxBias)
groundMat.SetUnifFloat32(maxBiasStr, pl.MaxBias)
palleteMat.SetUnifFloat32(maxBiasStr, pl.MaxBias)
} }
imgui.TreePop() imgui.TreePop()
@ -1443,7 +1133,7 @@ func (g *Game) showDebugWindow() {
} }
if updateLights { if updateLights {
g.updateLights() g.applyLightUpdates()
} }
// Demo fbo // Demo fbo

View File

@ -146,7 +146,8 @@ func (m *Material) GetAttribLoc(attribName string) int32 {
return loc return loc
} }
loc = gl.GetAttribLocation(m.ShaderProg.Id, gl.Str(attribName+"\x00")) name := gl.Str(attribName + "\x00")
loc = gl.GetAttribLocation(m.ShaderProg.Id, name)
assert.T(loc != -1, "Attribute '"+attribName+"' doesn't exist on material "+m.Name) assert.T(loc != -1, "Attribute '"+attribName+"' doesn't exist on material "+m.Name)
m.AttribLocs[attribName] = loc m.AttribLocs[attribName] = loc
return loc return loc
@ -159,7 +160,8 @@ func (m *Material) GetUnifLoc(uniformName string) int32 {
return loc return loc
} }
loc = gl.GetUniformLocation(m.ShaderProg.Id, gl.Str(uniformName+"\x00")) name := gl.Str(uniformName + "\x00")
loc = gl.GetUniformLocation(m.ShaderProg.Id, name)
assert.T(loc != -1, "Uniform '"+uniformName+"' doesn't exist on material "+m.Name) assert.T(loc != -1, "Uniform '"+uniformName+"' doesn't exist on material "+m.Name)
m.UnifLocs[uniformName] = loc m.UnifLocs[uniformName] = loc
return loc return loc

View File

@ -27,8 +27,8 @@ struct PointLight {
vec3 pos; vec3 pos;
vec3 diffuseColor; vec3 diffuseColor;
vec3 specularColor; vec3 specularColor;
float falloff;
float radius; float radius;
float falloff;
float maxBias; float maxBias;
float nearPlane; float nearPlane;
float farPlane; float farPlane;
@ -50,12 +50,12 @@ layout (std140) uniform GlobalMatrices {
layout (std140) uniform Lights { layout (std140) uniform Lights {
DirLight dirLight; DirLight dirLight;
PointLight pointLights[NUM_POINT_LIGHTS];
}; };
// //
// Uniforms // Uniforms
// //
uniform PointLight pointLights[NUM_POINT_LIGHTS];
uniform SpotLight spotLights[NUM_SPOT_LIGHTS]; uniform SpotLight spotLights[NUM_SPOT_LIGHTS];
uniform mat4 modelMat; uniform mat4 modelMat;
uniform mat4 dirLightProjViewMat; uniform mat4 dirLightProjViewMat;
@ -78,16 +78,6 @@ out vec3 tangentSpotLightPositions[NUM_SPOT_LIGHTS];
out vec3 tangentSpotLightDirections[NUM_SPOT_LIGHTS]; out vec3 tangentSpotLightDirections[NUM_SPOT_LIGHTS];
out vec3 tangentPointLightPositions[NUM_POINT_LIGHTS]; out vec3 tangentPointLightPositions[NUM_POINT_LIGHTS];
struct Test1 {
float ff;
vec3 v3;
};
layout (std140) uniform Test2 {
float f1;
Test1 s;
};
void main() void main()
{ {
vertUV0 = vertUV0In; vertUV0 = vertUV0In;
@ -180,13 +170,12 @@ struct PointLight {
vec3 pos; vec3 pos;
vec3 diffuseColor; vec3 diffuseColor;
vec3 specularColor; vec3 specularColor;
float falloff;
float radius; float radius;
float falloff;
float maxBias; float maxBias;
float nearPlane; float nearPlane;
float farPlane; float farPlane;
}; };
uniform PointLight pointLights[NUM_POINT_LIGHTS];
uniform samplerCubeArray pointLightCubeShadowMaps; uniform samplerCubeArray pointLightCubeShadowMaps;
struct SpotLight { struct SpotLight {
@ -207,6 +196,7 @@ layout (std140) uniform GlobalMatrices {
layout (std140) uniform Lights { layout (std140) uniform Lights {
DirLight dirLight; DirLight dirLight;
PointLight pointLights[NUM_POINT_LIGHTS];
}; };
uniform vec3 ambientColor = vec3(0.2, 0.2, 0.2); uniform vec3 ambientColor = vec3(0.2, 0.2, 0.2);