Material: Make scene optional, fix typo, other minor changes.

This commit is contained in:
Alberto Torres 2025-03-18 16:43:47 +01:00
parent fb50b3a018
commit 0b5cd6f8d2

View file

@ -40,7 +40,7 @@ when defined(nimdoc):
# Forward declarations
func add_line_numbers(s: string, first: int=1):string
proc newMaterial*(engine: MyouEngine, name: string, scene: Scene,
proc newMaterial*(engine: MyouEngine, name: string, scene: Scene = nil,
vertex = "",
fragment = "",
textures = initOrderedTable[string, Texture](),
@ -122,7 +122,7 @@ func add_line_numbers(s: string, first: int=1):string =
lines[i] = $(i+first) & " " & line
return lines.join("\n")
proc newMaterial*(engine: MyouEngine, name: string, scene: Scene,
proc newMaterial*(engine: MyouEngine, name: string, scene: Scene = nil,
vertex = "",
fragment = "",
textures = initOrderedTable[string, Texture](),
@ -166,7 +166,7 @@ proc newSolidMaterial*(engine: MyouEngine, name: string, color: Vec4): Material
}}""",
)
proc newVertexColordMaterial*(engine: MyouEngine, name: string): Material =
proc newVertexColorMaterial*(engine: MyouEngine, name = "vertex color"): Material =
newMaterial(engine, name, nil,
fragment = dedent """
in vec4 vcol;
@ -190,7 +190,7 @@ proc get_shader*(self: Material, mesh: Mesh): Shader =
shader = new Shader
self.shaders[mesh.layout] = shader
self.last_shader = shader
shader.initShader(self.engine, self, mesh.data.layout & mesh.data.tf_layout,
shader.initShader(self.engine, self, mesh.data.layout & mesh.data.tf_layout,
mesh.vertex_modifiers, mesh.material_defines)
return shader
@ -404,7 +404,7 @@ proc initShader*(self: Shader, engine: MyouEngine, material: Material,
@[]) & varyings_assign & modifiers_post_bodies &
"gl_Position = proj_co;\n}"
).join("\n ")
vs = (version_line & extension_lines & vs_head & varyings_uniform_decl &
vs = (version_line & extension_lines & vs_head & varyings_uniform_decl &
attribute_lines & vertex_shader_library & modifiers_uniforms & varyings_decl & vs_body).join("\n")
when defined(myouStoreGLSL):
self.vs_code = vs
@ -502,6 +502,8 @@ proc initShader*(self: Shader, engine: MyouEngine, material: Material,
# TODO: use KHR_parallel_shader_compile
# TODO: identify unused uniforms (at least unused textures)
var ubo_names = newSeqOfCap[string](material.ubos.len)
self.ubos.setLen 0
for ubo in material.ubos & modifier_ubos:
@ -515,31 +517,31 @@ proc initShader*(self: Shader, engine: MyouEngine, material: Material,
assert ubo.name notin ubo_names, "There's more than one UBO with name " & ubo.name & " in {material.name}"
ubo_names.add ubo.name
ubo_names.setLen 0
self.texture_locations.setLen 0
var extra_location_count = 0
for name in material.textures.keys:
self.texture_locations.add glGetUniformLocation(prog, name.cstring)
if defined(myouEnsureTextureLocations) and not defined(release):
assert self.texture_locations[^1] != -1, "invalid texture location for " & name
self.cubemap_locations.setLen 0
self.shadowmap_location = -1
if material.scene != nil:
self.shadowmap_location = glGetUniformLocation(prog, "shadow_maps".cstring)
if self.shadowmap_location != -1:
extra_location_count.inc
for i in 0 ..< material.scene.max_cubemaps:
# TODO: test with a single glUniform1iv instead of changing
# the individual array elements
self.cubemap_locations.add glGetUniformLocation(prog,
self.cubemap_locations.add glGetUniformLocation(prog,
(&"cube_maps[{i}]").cstring)
# when not defined(release):
# assert self.shadowmap_locations[^1] != -1
# dump material.name
# dump self.cubemap_locations
assert self.texture_locations.len + self.cubemap_locations.len +
extra_location_count <= self.engine.renderer.max_textures
@ -551,5 +553,3 @@ proc use*(self: Shader): GLuint {.inline,discardable.} =
proc destroy*(self: Shader) =
self.program = 0.GPUProgram