Get rid of allocations on SetUniform calls, allowing us to pass ref again

This commit is contained in:
bloeys
2024-05-13 04:33:54 +04:00
parent f0a12879f8
commit c782e8c312
4 changed files with 186 additions and 137 deletions

234
main.go
View File

@ -523,8 +523,8 @@ func (g *Game) Init() {
// Create materials and assign any unused texture slots to black
//
screenQuadMat = materials.NewMaterial("Screen Quad Mat", "./res/shaders/screen-quad.glsl")
screenQuadMat.SetUnifVec2("scale", demoFboScale)
screenQuadMat.SetUnifVec2("offset", demoFboOffset)
screenQuadMat.SetUnifVec2("scale", &demoFboScale)
screenQuadMat.SetUnifVec2("offset", &demoFboOffset)
screenQuadMat.SetUnifInt32("material.diffuse", int32(materials.TextureSlot_Diffuse))
tonemappedScreenQuadMat = materials.NewMaterial("Tonemapped Screen Quad Mat", "./res/shaders/tonemapped-screen-quad.glsl")
@ -541,11 +541,11 @@ func (g *Game) Init() {
whiteMat.SetUnifInt32("material.specular", int32(materials.TextureSlot_Specular))
whiteMat.SetUnifInt32("material.normal", int32(materials.TextureSlot_Normal))
whiteMat.SetUnifInt32("material.emission", int32(materials.TextureSlot_Emission))
whiteMat.SetUnifVec3("ambientColor", ambientColor)
whiteMat.SetUnifVec3("ambientColor", &ambientColor)
whiteMat.SetUnifFloat32("material.shininess", whiteMat.Shininess)
whiteMat.SetUnifVec3("dirLight.dir", dirLight.Dir)
whiteMat.SetUnifVec3("dirLight.diffuseColor", dirLight.DiffuseColor)
whiteMat.SetUnifVec3("dirLight.specularColor", dirLight.SpecularColor)
whiteMat.SetUnifVec3("dirLight.dir", &dirLight.Dir)
whiteMat.SetUnifVec3("dirLight.diffuseColor", &dirLight.DiffuseColor)
whiteMat.SetUnifVec3("dirLight.specularColor", &dirLight.SpecularColor)
whiteMat.SetUnifInt32("dirLight.shadowMap", int32(materials.TextureSlot_ShadowMap1))
whiteMat.SetUnifInt32("pointLightCubeShadowMaps", int32(materials.TextureSlot_Cubemap_Array))
whiteMat.SetUnifInt32("spotLightShadowMaps", int32(materials.TextureSlot_ShadowMap_Array1))
@ -559,11 +559,11 @@ func (g *Game) Init() {
containerMat.SetUnifInt32("material.specular", int32(materials.TextureSlot_Specular))
containerMat.SetUnifInt32("material.normal", int32(materials.TextureSlot_Normal))
containerMat.SetUnifInt32("material.emission", int32(materials.TextureSlot_Emission))
containerMat.SetUnifVec3("ambientColor", ambientColor)
containerMat.SetUnifVec3("ambientColor", &ambientColor)
containerMat.SetUnifFloat32("material.shininess", containerMat.Shininess)
containerMat.SetUnifVec3("dirLight.dir", dirLight.Dir)
containerMat.SetUnifVec3("dirLight.diffuseColor", dirLight.DiffuseColor)
containerMat.SetUnifVec3("dirLight.specularColor", dirLight.SpecularColor)
containerMat.SetUnifVec3("dirLight.dir", &dirLight.Dir)
containerMat.SetUnifVec3("dirLight.diffuseColor", &dirLight.DiffuseColor)
containerMat.SetUnifVec3("dirLight.specularColor", &dirLight.SpecularColor)
containerMat.SetUnifInt32("dirLight.shadowMap", int32(materials.TextureSlot_ShadowMap1))
containerMat.SetUnifInt32("pointLightCubeShadowMaps", int32(materials.TextureSlot_Cubemap_Array))
containerMat.SetUnifInt32("spotLightShadowMaps", int32(materials.TextureSlot_ShadowMap_Array1))
@ -577,11 +577,11 @@ func (g *Game) Init() {
groundMat.SetUnifInt32("material.specular", int32(materials.TextureSlot_Specular))
groundMat.SetUnifInt32("material.normal", int32(materials.TextureSlot_Normal))
groundMat.SetUnifInt32("material.emission", int32(materials.TextureSlot_Emission))
groundMat.SetUnifVec3("ambientColor", ambientColor)
groundMat.SetUnifVec3("ambientColor", &ambientColor)
groundMat.SetUnifFloat32("material.shininess", groundMat.Shininess)
groundMat.SetUnifVec3("dirLight.dir", dirLight.Dir)
groundMat.SetUnifVec3("dirLight.diffuseColor", dirLight.DiffuseColor)
groundMat.SetUnifVec3("dirLight.specularColor", dirLight.SpecularColor)
groundMat.SetUnifVec3("dirLight.dir", &dirLight.Dir)
groundMat.SetUnifVec3("dirLight.diffuseColor", &dirLight.DiffuseColor)
groundMat.SetUnifVec3("dirLight.specularColor", &dirLight.SpecularColor)
groundMat.SetUnifInt32("dirLight.shadowMap", int32(materials.TextureSlot_ShadowMap1))
groundMat.SetUnifInt32("pointLightCubeShadowMaps", int32(materials.TextureSlot_Cubemap_Array))
groundMat.SetUnifInt32("spotLightShadowMaps", int32(materials.TextureSlot_ShadowMap_Array1))
@ -594,11 +594,11 @@ func (g *Game) Init() {
palleteMat.SetUnifInt32("material.specular", int32(materials.TextureSlot_Specular))
palleteMat.SetUnifInt32("material.normal", int32(materials.TextureSlot_Normal))
palleteMat.SetUnifInt32("material.emission", int32(materials.TextureSlot_Emission))
palleteMat.SetUnifVec3("ambientColor", ambientColor)
palleteMat.SetUnifVec3("ambientColor", &ambientColor)
palleteMat.SetUnifFloat32("material.shininess", palleteMat.Shininess)
palleteMat.SetUnifVec3("dirLight.dir", dirLight.Dir)
palleteMat.SetUnifVec3("dirLight.diffuseColor", dirLight.DiffuseColor)
palleteMat.SetUnifVec3("dirLight.specularColor", dirLight.SpecularColor)
palleteMat.SetUnifVec3("dirLight.dir", &dirLight.Dir)
palleteMat.SetUnifVec3("dirLight.diffuseColor", &dirLight.DiffuseColor)
palleteMat.SetUnifVec3("dirLight.specularColor", &dirLight.SpecularColor)
palleteMat.SetUnifInt32("dirLight.shadowMap", int32(materials.TextureSlot_ShadowMap1))
palleteMat.SetUnifInt32("pointLightCubeShadowMaps", int32(materials.TextureSlot_Cubemap_Array))
palleteMat.SetUnifInt32("spotLightShadowMaps", int32(materials.TextureSlot_ShadowMap_Array1))
@ -723,20 +723,20 @@ func (g *Game) updateLights() {
p := &pointLights[i]
indexString := "pointLights[" + strconv.Itoa(i) + "]"
whiteMat.SetUnifVec3(indexString+".pos", p.Pos)
containerMat.SetUnifVec3(indexString+".pos", p.Pos)
groundMat.SetUnifVec3(indexString+".pos", p.Pos)
palleteMat.SetUnifVec3(indexString+".pos", p.Pos)
whiteMat.SetUnifVec3(indexString+".pos", &p.Pos)
containerMat.SetUnifVec3(indexString+".pos", &p.Pos)
groundMat.SetUnifVec3(indexString+".pos", &p.Pos)
palleteMat.SetUnifVec3(indexString+".pos", &p.Pos)
whiteMat.SetUnifVec3(indexString+".diffuseColor", p.DiffuseColor)
containerMat.SetUnifVec3(indexString+".diffuseColor", p.DiffuseColor)
groundMat.SetUnifVec3(indexString+".diffuseColor", p.DiffuseColor)
palleteMat.SetUnifVec3(indexString+".diffuseColor", p.DiffuseColor)
whiteMat.SetUnifVec3(indexString+".diffuseColor", &p.DiffuseColor)
containerMat.SetUnifVec3(indexString+".diffuseColor", &p.DiffuseColor)
groundMat.SetUnifVec3(indexString+".diffuseColor", &p.DiffuseColor)
palleteMat.SetUnifVec3(indexString+".diffuseColor", &p.DiffuseColor)
whiteMat.SetUnifVec3(indexString+".specularColor", p.SpecularColor)
containerMat.SetUnifVec3(indexString+".specularColor", p.SpecularColor)
groundMat.SetUnifVec3(indexString+".specularColor", p.SpecularColor)
palleteMat.SetUnifVec3(indexString+".specularColor", p.SpecularColor)
whiteMat.SetUnifVec3(indexString+".specularColor", &p.SpecularColor)
containerMat.SetUnifVec3(indexString+".specularColor", &p.SpecularColor)
groundMat.SetUnifVec3(indexString+".specularColor", &p.SpecularColor)
palleteMat.SetUnifVec3(indexString+".specularColor", &p.SpecularColor)
whiteMat.SetUnifFloat32(indexString+".constant", p.Constant)
containerMat.SetUnifFloat32(indexString+".constant", p.Constant)
@ -773,25 +773,25 @@ func (g *Game) updateLights() {
indexString := "spotLights[" + strconv.Itoa(i) + "]"
whiteMat.SetUnifVec3(indexString+".pos", l.Pos)
containerMat.SetUnifVec3(indexString+".pos", l.Pos)
groundMat.SetUnifVec3(indexString+".pos", l.Pos)
palleteMat.SetUnifVec3(indexString+".pos", l.Pos)
whiteMat.SetUnifVec3(indexString+".pos", &l.Pos)
containerMat.SetUnifVec3(indexString+".pos", &l.Pos)
groundMat.SetUnifVec3(indexString+".pos", &l.Pos)
palleteMat.SetUnifVec3(indexString+".pos", &l.Pos)
whiteMat.SetUnifVec3(indexString+".dir", l.Dir)
containerMat.SetUnifVec3(indexString+".dir", l.Dir)
groundMat.SetUnifVec3(indexString+".dir", l.Dir)
palleteMat.SetUnifVec3(indexString+".dir", l.Dir)
whiteMat.SetUnifVec3(indexString+".dir", &l.Dir)
containerMat.SetUnifVec3(indexString+".dir", &l.Dir)
groundMat.SetUnifVec3(indexString+".dir", &l.Dir)
palleteMat.SetUnifVec3(indexString+".dir", &l.Dir)
whiteMat.SetUnifVec3(indexString+".diffuseColor", l.DiffuseColor)
containerMat.SetUnifVec3(indexString+".diffuseColor", l.DiffuseColor)
groundMat.SetUnifVec3(indexString+".diffuseColor", l.DiffuseColor)
palleteMat.SetUnifVec3(indexString+".diffuseColor", l.DiffuseColor)
whiteMat.SetUnifVec3(indexString+".diffuseColor", &l.DiffuseColor)
containerMat.SetUnifVec3(indexString+".diffuseColor", &l.DiffuseColor)
groundMat.SetUnifVec3(indexString+".diffuseColor", &l.DiffuseColor)
palleteMat.SetUnifVec3(indexString+".diffuseColor", &l.DiffuseColor)
whiteMat.SetUnifVec3(indexString+".specularColor", l.SpecularColor)
containerMat.SetUnifVec3(indexString+".specularColor", l.SpecularColor)
groundMat.SetUnifVec3(indexString+".specularColor", l.SpecularColor)
palleteMat.SetUnifVec3(indexString+".specularColor", l.SpecularColor)
whiteMat.SetUnifVec3(indexString+".specularColor", &l.SpecularColor)
containerMat.SetUnifVec3(indexString+".specularColor", &l.SpecularColor)
groundMat.SetUnifVec3(indexString+".specularColor", &l.SpecularColor)
palleteMat.SetUnifVec3(indexString+".specularColor", &l.SpecularColor)
whiteMat.SetUnifFloat32(indexString+".innerCutoff", innerCutoffCos)
containerMat.SetUnifFloat32(indexString+".innerCutoff", innerCutoffCos)
@ -863,10 +863,10 @@ func (g *Game) showDebugWindow() {
imgui.Text("Ambient Light")
if imgui.DragFloat3("Ambient Color", &ambientColor.Data) {
whiteMat.SetUnifVec3("ambientColor", ambientColor)
containerMat.SetUnifVec3("ambientColor", ambientColor)
groundMat.SetUnifVec3("ambientColor", ambientColor)
palleteMat.SetUnifVec3("ambientColor", ambientColor)
whiteMat.SetUnifVec3("ambientColor", &ambientColor)
containerMat.SetUnifVec3("ambientColor", &ambientColor)
groundMat.SetUnifVec3("ambientColor", &ambientColor)
palleteMat.SetUnifVec3("ambientColor", &ambientColor)
}
imgui.Spacing()
@ -877,24 +877,24 @@ func (g *Game) showDebugWindow() {
imgui.Checkbox("Render Directional Light Shadows", &renderDirLightShadows)
if imgui.DragFloat3("Direction", &dirLight.Dir.Data) {
whiteMat.SetUnifVec3("dirLight.dir", dirLight.Dir)
containerMat.SetUnifVec3("dirLight.dir", dirLight.Dir)
groundMat.SetUnifVec3("dirLight.dir", dirLight.Dir)
palleteMat.SetUnifVec3("dirLight.dir", dirLight.Dir)
whiteMat.SetUnifVec3("dirLight.dir", &dirLight.Dir)
containerMat.SetUnifVec3("dirLight.dir", &dirLight.Dir)
groundMat.SetUnifVec3("dirLight.dir", &dirLight.Dir)
palleteMat.SetUnifVec3("dirLight.dir", &dirLight.Dir)
}
if imgui.DragFloat3("Diffuse Color", &dirLight.DiffuseColor.Data) {
whiteMat.SetUnifVec3("dirLight.diffuseColor", dirLight.DiffuseColor)
containerMat.SetUnifVec3("dirLight.diffuseColor", dirLight.DiffuseColor)
groundMat.SetUnifVec3("dirLight.diffuseColor", dirLight.DiffuseColor)
palleteMat.SetUnifVec3("dirLight.diffuseColor", dirLight.DiffuseColor)
whiteMat.SetUnifVec3("dirLight.diffuseColor", &dirLight.DiffuseColor)
containerMat.SetUnifVec3("dirLight.diffuseColor", &dirLight.DiffuseColor)
groundMat.SetUnifVec3("dirLight.diffuseColor", &dirLight.DiffuseColor)
palleteMat.SetUnifVec3("dirLight.diffuseColor", &dirLight.DiffuseColor)
}
if imgui.DragFloat3("Specular Color", &dirLight.SpecularColor.Data) {
whiteMat.SetUnifVec3("dirLight.specularColor", dirLight.SpecularColor)
containerMat.SetUnifVec3("dirLight.specularColor", dirLight.SpecularColor)
groundMat.SetUnifVec3("dirLight.specularColor", dirLight.SpecularColor)
palleteMat.SetUnifVec3("dirLight.specularColor", dirLight.SpecularColor)
whiteMat.SetUnifVec3("dirLight.specularColor", &dirLight.SpecularColor)
containerMat.SetUnifVec3("dirLight.specularColor", &dirLight.SpecularColor)
groundMat.SetUnifVec3("dirLight.specularColor", &dirLight.SpecularColor)
palleteMat.SetUnifVec3("dirLight.specularColor", &dirLight.SpecularColor)
}
imgui.DragFloat3("dPos", &dirLightPos.Data)
@ -932,24 +932,24 @@ func (g *Game) showDebugWindow() {
indexString := "pointLights[" + indexNumString + "]"
if imgui.DragFloat3("Pos", &pl.Pos.Data) {
whiteMat.SetUnifVec3(indexString+".pos", pl.Pos)
containerMat.SetUnifVec3(indexString+".pos", pl.Pos)
groundMat.SetUnifVec3(indexString+".pos", pl.Pos)
palleteMat.SetUnifVec3(indexString+".pos", pl.Pos)
whiteMat.SetUnifVec3(indexString+".pos", &pl.Pos)
containerMat.SetUnifVec3(indexString+".pos", &pl.Pos)
groundMat.SetUnifVec3(indexString+".pos", &pl.Pos)
palleteMat.SetUnifVec3(indexString+".pos", &pl.Pos)
}
if imgui.DragFloat3("Diffuse Color", &pl.DiffuseColor.Data) {
whiteMat.SetUnifVec3(indexString+".diffuseColor", pl.DiffuseColor)
containerMat.SetUnifVec3(indexString+".diffuseColor", pl.DiffuseColor)
groundMat.SetUnifVec3(indexString+".diffuseColor", pl.DiffuseColor)
palleteMat.SetUnifVec3(indexString+".diffuseColor", pl.DiffuseColor)
whiteMat.SetUnifVec3(indexString+".diffuseColor", &pl.DiffuseColor)
containerMat.SetUnifVec3(indexString+".diffuseColor", &pl.DiffuseColor)
groundMat.SetUnifVec3(indexString+".diffuseColor", &pl.DiffuseColor)
palleteMat.SetUnifVec3(indexString+".diffuseColor", &pl.DiffuseColor)
}
if imgui.DragFloat3("Specular Color", &pl.SpecularColor.Data) {
whiteMat.SetUnifVec3(indexString+".specularColor", pl.SpecularColor)
containerMat.SetUnifVec3(indexString+".specularColor", pl.SpecularColor)
groundMat.SetUnifVec3(indexString+".specularColor", pl.SpecularColor)
palleteMat.SetUnifVec3(indexString+".specularColor", pl.SpecularColor)
whiteMat.SetUnifVec3(indexString+".specularColor", &pl.SpecularColor)
containerMat.SetUnifVec3(indexString+".specularColor", &pl.SpecularColor)
groundMat.SetUnifVec3(indexString+".specularColor", &pl.SpecularColor)
palleteMat.SetUnifVec3(indexString+".specularColor", &pl.SpecularColor)
}
imgui.TreePop()
@ -975,31 +975,31 @@ func (g *Game) showDebugWindow() {
indexString := "spotLights[" + indexNumString + "]"
if imgui.DragFloat3("Pos", &l.Pos.Data) {
whiteMat.SetUnifVec3(indexString+".pos", l.Pos)
containerMat.SetUnifVec3(indexString+".pos", l.Pos)
groundMat.SetUnifVec3(indexString+".pos", l.Pos)
palleteMat.SetUnifVec3(indexString+".pos", l.Pos)
whiteMat.SetUnifVec3(indexString+".pos", &l.Pos)
containerMat.SetUnifVec3(indexString+".pos", &l.Pos)
groundMat.SetUnifVec3(indexString+".pos", &l.Pos)
palleteMat.SetUnifVec3(indexString+".pos", &l.Pos)
}
if imgui.DragFloat3("Dir", &l.Dir.Data) {
whiteMat.SetUnifVec3(indexString+".dir", l.Dir)
containerMat.SetUnifVec3(indexString+".dir", l.Dir)
groundMat.SetUnifVec3(indexString+".dir", l.Dir)
palleteMat.SetUnifVec3(indexString+".dir", l.Dir)
whiteMat.SetUnifVec3(indexString+".dir", &l.Dir)
containerMat.SetUnifVec3(indexString+".dir", &l.Dir)
groundMat.SetUnifVec3(indexString+".dir", &l.Dir)
palleteMat.SetUnifVec3(indexString+".dir", &l.Dir)
}
if imgui.DragFloat3("Diffuse Color", &l.DiffuseColor.Data) {
whiteMat.SetUnifVec3(indexString+".diffuseColor", l.DiffuseColor)
containerMat.SetUnifVec3(indexString+".diffuseColor", l.DiffuseColor)
groundMat.SetUnifVec3(indexString+".diffuseColor", l.DiffuseColor)
palleteMat.SetUnifVec3(indexString+".diffuseColor", l.DiffuseColor)
whiteMat.SetUnifVec3(indexString+".diffuseColor", &l.DiffuseColor)
containerMat.SetUnifVec3(indexString+".diffuseColor", &l.DiffuseColor)
groundMat.SetUnifVec3(indexString+".diffuseColor", &l.DiffuseColor)
palleteMat.SetUnifVec3(indexString+".diffuseColor", &l.DiffuseColor)
}
if imgui.DragFloat3("Specular Color", &l.SpecularColor.Data) {
whiteMat.SetUnifVec3(indexString+".specularColor", l.SpecularColor)
containerMat.SetUnifVec3(indexString+".specularColor", l.SpecularColor)
groundMat.SetUnifVec3(indexString+".specularColor", l.SpecularColor)
palleteMat.SetUnifVec3(indexString+".specularColor", l.SpecularColor)
whiteMat.SetUnifVec3(indexString+".specularColor", &l.SpecularColor)
containerMat.SetUnifVec3(indexString+".specularColor", &l.SpecularColor)
groundMat.SetUnifVec3(indexString+".specularColor", &l.SpecularColor)
palleteMat.SetUnifVec3(indexString+".specularColor", &l.SpecularColor)
}
if imgui.DragFloat("Inner Cutoff Radians", &l.InnerCutoffRad) {
@ -1129,10 +1129,10 @@ var (
func (g *Game) Render() {
whiteMat.SetUnifVec3("camPos", cam.Pos)
containerMat.SetUnifVec3("camPos", cam.Pos)
groundMat.SetUnifVec3("camPos", cam.Pos)
palleteMat.SetUnifVec3("camPos", cam.Pos)
whiteMat.SetUnifVec3("camPos", &cam.Pos)
containerMat.SetUnifVec3("camPos", &cam.Pos)
groundMat.SetUnifVec3("camPos", &cam.Pos)
palleteMat.SetUnifVec3("camPos", &cam.Pos)
rotatingCubeTrMat1.Rotate(rotatingCubeSpeedDeg1*gglm.Deg2Rad*timing.DT(), 0, 1, 0)
rotatingCubeTrMat2.Rotate(rotatingCubeSpeedDeg2*gglm.Deg2Rad*timing.DT(), 1, 1, 0)
@ -1175,12 +1175,12 @@ func (g *Game) renderDirectionalLightShadowmap() {
// Set some uniforms
dirLightProjViewMat := dirLight.GetProjViewMat()
whiteMat.SetUnifMat4("dirLightProjViewMat", dirLightProjViewMat)
containerMat.SetUnifMat4("dirLightProjViewMat", dirLightProjViewMat)
groundMat.SetUnifMat4("dirLightProjViewMat", dirLightProjViewMat)
palleteMat.SetUnifMat4("dirLightProjViewMat", dirLightProjViewMat)
whiteMat.SetUnifMat4("dirLightProjViewMat", &dirLightProjViewMat)
containerMat.SetUnifMat4("dirLightProjViewMat", &dirLightProjViewMat)
groundMat.SetUnifMat4("dirLightProjViewMat", &dirLightProjViewMat)
palleteMat.SetUnifMat4("dirLightProjViewMat", &dirLightProjViewMat)
depthMapMat.SetUnifMat4("projViewMat", dirLightProjViewMat)
depthMapMat.SetUnifMat4("projViewMat", &dirLightProjViewMat)
// Start rendering
dirLightDepthMapFbo.BindWithViewport()
@ -1199,8 +1199,8 @@ func (g *Game) renderDirectionalLightShadowmap() {
if showDirLightDepthMapFbo {
screenQuadMat.DiffuseTex = dirLightDepthMapFbo.Attachments[0].Id
screenQuadMat.SetUnifVec2("offset", dirLightDepthMapFboOffset)
screenQuadMat.SetUnifVec2("scale", dirLightDepthMapFboScale)
screenQuadMat.SetUnifVec2("offset", &dirLightDepthMapFboOffset)
screenQuadMat.SetUnifVec2("scale", &dirLightDepthMapFboScale)
screenQuadMat.Bind()
window.Rend.DrawVertexArray(screenQuadMat, screenQuadVao, 0, 6)
}
@ -1217,13 +1217,13 @@ func (g *Game) renderSpotLightShadowmaps() {
// Set render uniforms
projViewMat := l.GetProjViewMat()
whiteMat.SetUnifMat4(projViewMatIndexStr, projViewMat)
containerMat.SetUnifMat4(projViewMatIndexStr, projViewMat)
groundMat.SetUnifMat4(projViewMatIndexStr, projViewMat)
palleteMat.SetUnifMat4(projViewMatIndexStr, projViewMat)
whiteMat.SetUnifMat4(projViewMatIndexStr, &projViewMat)
containerMat.SetUnifMat4(projViewMatIndexStr, &projViewMat)
groundMat.SetUnifMat4(projViewMatIndexStr, &projViewMat)
palleteMat.SetUnifMat4(projViewMatIndexStr, &projViewMat)
// Set depth uniforms
arrayDepthMapMat.SetUnifMat4("projViewMats["+indexStr+"]", projViewMat)
arrayDepthMapMat.SetUnifMat4("projViewMats["+indexStr+"]", &projViewMat)
}
// Render
@ -1248,14 +1248,14 @@ func (g *Game) renderPointLightShadowmaps() {
p := &pointLights[i]
// Generic uniforms
omnidirDepthMapMat.SetUnifVec3("lightPos", p.Pos)
omnidirDepthMapMat.SetUnifVec3("lightPos", &p.Pos)
omnidirDepthMapMat.SetUnifInt32("cubemapIndex", int32(i))
omnidirDepthMapMat.SetUnifFloat32("farPlane", p.FarPlane)
// Set projView matrices
projViewMats := p.GetProjViewMats(float32(pointLightDepthMapFbo.Width), float32(pointLightDepthMapFbo.Height))
for j := 0; j < len(projViewMats); j++ {
omnidirDepthMapMat.SetUnifMat4("cubemapProjViewMats["+strconv.Itoa(j)+"]", projViewMats[j])
omnidirDepthMapMat.SetUnifMat4("cubemapProjViewMats["+strconv.Itoa(j)+"]", &projViewMats[j])
}
g.RenderScene(&omnidirDepthMapMat)
@ -1282,8 +1282,8 @@ func (g *Game) renderDemoFbo() {
demoFbo.UnBind()
screenQuadMat.DiffuseTex = demoFbo.Attachments[0].Id
screenQuadMat.SetUnifVec2("offset", demoFboOffset)
screenQuadMat.SetUnifVec2("scale", demoFboScale)
screenQuadMat.SetUnifVec2("offset", &demoFboOffset)
screenQuadMat.SetUnifVec2("scale", &demoFboScale)
window.Rend.DrawVertexArray(screenQuadMat, screenQuadVao, 0, 6)
}
@ -1386,12 +1386,12 @@ func updateAllProjViewMats(projMat, viewMat gglm.Mat4) {
projViewMat := *projMat.Clone().Mul(&viewMat)
unlitMat.SetUnifMat4("projViewMat", projViewMat)
whiteMat.SetUnifMat4("projViewMat", projViewMat)
containerMat.SetUnifMat4("projViewMat", projViewMat)
groundMat.SetUnifMat4("projViewMat", projViewMat)
palleteMat.SetUnifMat4("projViewMat", projViewMat)
debugDepthMat.SetUnifMat4("projViewMat", projViewMat)
unlitMat.SetUnifMat4("projViewMat", &projViewMat)
whiteMat.SetUnifMat4("projViewMat", &projViewMat)
containerMat.SetUnifMat4("projViewMat", &projViewMat)
groundMat.SetUnifMat4("projViewMat", &projViewMat)
palleteMat.SetUnifMat4("projViewMat", &projViewMat)
debugDepthMat.SetUnifMat4("projViewMat", &projViewMat)
// Update skybox projViewMat
skyboxViewMat := viewMat.Clone()
@ -1402,5 +1402,5 @@ func updateAllProjViewMats(projMat, viewMat gglm.Mat4) {
skyboxViewMat.Set(3, 1, 0)
skyboxViewMat.Set(3, 2, 0)
skyboxViewMat.Set(3, 3, 0)
skyboxMat.SetUnifMat4("projViewMat", *projMat.Clone().Mul(skyboxViewMat))
skyboxMat.SetUnifMat4("projViewMat", projMat.Clone().Mul(skyboxViewMat))
}

View File

@ -1,6 +1,8 @@
package materials
import (
_ "unsafe"
"github.com/bloeys/gglm/gglm"
"github.com/bloeys/nmage/assert"
"github.com/bloeys/nmage/assets"
@ -9,12 +11,11 @@ import (
"github.com/go-gl/gl/v4.1-core/gl"
)
// @TODO: Unfortunately some uniform gl functions allocate because Go can't prove that the
// gl function doesn't keep a pointer to the vectors/matrices we pass in, leading to them
// escaping to the heap.
// @TODO: This noescape magic is to avoid heap allocations done when
// passing vectors or matrices into cgo via set uniform calls.
//
// Perhaps later we move to our own gl functions that are more optimized? assuming such an optimization
// is even possible.
// But I would rather this kind of stuff is done on the gl wrapper level.
// Should we wrap the OpenGL APIs we use ourself?
var (
lastMatId uint32
@ -165,28 +166,76 @@ func (m *Material) SetUnifFloat32(uniformName string, val float32) {
gl.ProgramUniform1f(m.ShaderProg.Id, m.GetUnifLoc(uniformName), val)
}
func (m *Material) SetUnifVec2(uniformName string, vec2 gglm.Vec2) {
gl.ProgramUniform2fv(m.ShaderProg.Id, m.GetUnifLoc(uniformName), 1, &vec2.Data[0])
func (m *Material) SetUnifVec2(uniformName string, vec2 *gglm.Vec2) {
internalSetUnifVec2(m.ShaderProg.Id, m.GetUnifLoc(uniformName), vec2)
}
func (m *Material) SetUnifVec3(uniformName string, vec3 gglm.Vec3) {
gl.ProgramUniform3fv(m.ShaderProg.Id, m.GetUnifLoc(uniformName), 1, &vec3.Data[0])
//go:noescape
//go:linkname internalSetUnifVec2 github.com/bloeys/nmage/materials.SetUnifVec2
func internalSetUnifVec2(shaderProgId uint32, unifLoc int32, vec2 *gglm.Vec2)
func SetUnifVec2(shaderProgId uint32, unifLoc int32, vec2 *gglm.Vec2) {
gl.ProgramUniform2fv(shaderProgId, unifLoc, 1, &vec2.Data[0])
}
func (m *Material) SetUnifVec4(uniformName string, vec4 gglm.Vec4) {
gl.ProgramUniform4fv(m.ShaderProg.Id, m.GetUnifLoc(uniformName), 1, &vec4.Data[0])
func (m *Material) SetUnifVec3(uniformName string, vec3 *gglm.Vec3) {
internalSetUnifVec3(m.ShaderProg.Id, m.GetUnifLoc(uniformName), vec3)
}
func (m *Material) SetUnifMat2(uniformName string, mat2 gglm.Mat2) {
gl.ProgramUniformMatrix2fv(m.ShaderProg.Id, m.GetUnifLoc(uniformName), 1, false, &mat2.Data[0][0])
//go:noescape
//go:linkname internalSetUnifVec3 github.com/bloeys/nmage/materials.SetUnifVec3
func internalSetUnifVec3(shaderProgId uint32, unifLoc int32, vec3 *gglm.Vec3)
func SetUnifVec3(shaderProgId uint32, unifLoc int32, vec3 *gglm.Vec3) {
gl.ProgramUniform3fv(shaderProgId, unifLoc, 1, &vec3.Data[0])
}
func (m *Material) SetUnifMat3(uniformName string, mat3 gglm.Mat3) {
gl.ProgramUniformMatrix3fv(m.ShaderProg.Id, m.GetUnifLoc(uniformName), 1, false, &mat3.Data[0][0])
func (m *Material) SetUnifVec4(uniformName string, vec4 *gglm.Vec4) {
setUnifVec4(m.ShaderProg.Id, m.GetUnifLoc(uniformName), vec4)
}
func (m *Material) SetUnifMat4(uniformName string, mat4 gglm.Mat4) {
gl.ProgramUniformMatrix4fv(m.ShaderProg.Id, m.GetUnifLoc(uniformName), 1, false, &mat4.Data[0][0])
//go:noescape
//go:linkname setUnifVec4 github.com/bloeys/nmage/materials.SetUnifVec4
func setUnifVec4(shaderProgId uint32, unifLoc int32, vec4 *gglm.Vec4)
func SetUnifVec4(shaderProgId uint32, unifLoc int32, vec4 *gglm.Vec4) {
gl.ProgramUniform4fv(shaderProgId, unifLoc, 1, &vec4.Data[0])
}
func (m *Material) SetUnifMat2(uniformName string, mat2 *gglm.Mat2) {
setUnifMat2(m.ShaderProg.Id, m.GetUnifLoc(uniformName), mat2)
}
//go:noescape
//go:linkname setUnifMat2 github.com/bloeys/nmage/materials.SetUnifMat2
func setUnifMat2(shaderProgId uint32, unifLoc int32, mat2 *gglm.Mat2)
func SetUnifMat2(shaderProgId uint32, unifLoc int32, mat2 *gglm.Mat2) {
gl.ProgramUniformMatrix2fv(shaderProgId, unifLoc, 1, false, &mat2.Data[0][0])
}
func (m *Material) SetUnifMat3(uniformName string, mat3 *gglm.Mat3) {
setUnifMat3(m.ShaderProg.Id, m.GetUnifLoc(uniformName), mat3)
}
//go:noescape
//go:linkname setUnifMat3 github.com/bloeys/nmage/materials.SetUnifMat3
func setUnifMat3(shaderProgId uint32, unifLoc int32, mat3 *gglm.Mat3)
func SetUnifMat3(shaderProgId uint32, unifLoc int32, mat3 *gglm.Mat3) {
gl.ProgramUniformMatrix3fv(shaderProgId, unifLoc, 1, false, &mat3.Data[0][0])
}
func (m *Material) SetUnifMat4(uniformName string, mat4 *gglm.Mat4) {
setUnifMat4(m.ShaderProg.Id, m.GetUnifLoc(uniformName), mat4)
}
//go:noescape
//go:linkname setUnifMat4 github.com/bloeys/nmage/materials.SetUnifMat4
func setUnifMat4(shaderProgId uint32, unifLoc int32, mat4 *gglm.Mat4)
func SetUnifMat4(shaderProgId uint32, unifLoc int32, mat4 *gglm.Mat4) {
gl.ProgramUniformMatrix4fv(shaderProgId, unifLoc, 1, false, &mat4.Data[0][0])
}
func (m *Material) Delete() {

View File

@ -30,12 +30,12 @@ func (r *Rend3DGL) DrawMesh(mesh meshes.Mesh, modelMat gglm.TrMat, mat materials
}
if mat.Settings.Has(materials.MaterialSettings_HasModelMtx) {
mat.SetUnifMat4("modelMat", modelMat.Mat4)
mat.SetUnifMat4("modelMat", &modelMat.Mat4)
}
if mat.Settings.Has(materials.MaterialSettings_HasNormalMtx) {
normalMat := modelMat.Clone().InvertAndTranspose().ToMat3()
mat.SetUnifMat3("normalMat", normalMat)
mat.SetUnifMat3("normalMat", &normalMat)
}
for i := 0; i < len(mesh.SubMeshes); i++ {

View File

@ -70,7 +70,7 @@ func (i *ImguiInfo) Render(winWidth, winHeight float32, fbWidth, fbHeight int32)
// @PERF: only update the ortho matrix on window resize
orthoMat := gglm.Ortho(0, float32(winWidth), 0, float32(winHeight), 0, 20)
i.Mat.SetUnifMat4("ProjMtx", orthoMat.Mat4)
i.Mat.SetUnifMat4("ProjMtx", &orthoMat.Mat4)
gl.BindSampler(0, 0) // Rely on combined texture/sampler state.
// Recreate the VAO every time