18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 15:17:08 +01:00

OpenGL 4.6: a material can have its own vertex shader, mat_<name>.vert

Fetch_Shader() links mat_<name>.vert with the fragment shader of the material
when the file is there (people animated by textures, mat_vat.vert), the common
vertex.vert otherwise. The multi draw variant takes the same shader with
MODEL_DRAWS. The sun shadows of such materials go through the same vertex
shader with MATERIAL_SHADOW and (alpha)shadowmap.frag, with the textures and
params of the material bound; in the indirect shadow pass they are drawn one by
one. The depth prepass leaves them out (Prepass_Class() 0).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GXgG7F4L42c2F9sDSAJbT2
This commit is contained in:
lcddisplay
2026-11-02 10:35:33 +01:00
committed by Claude
parent 03766899af
commit fd3d081191
2 changed files with 86 additions and 29 deletions
+79 -10
View File
@@ -9,6 +9,7 @@ http://mozilla.org/MPL/2.0/.
#include "stdafx.h"
#include "rendering/opengl46/opengl46renderer.h"
#include "rendering/common/shadersource.h"
#include "utilities/color.h"
#include "utilities/Globals.h"
#include "utilities/Timer.h"
@@ -32,6 +33,7 @@ http://mozilla.org/MPL/2.0/.
#include "physics/vehicle.h"
#include "vehicle/DynObj.h"
#include <imgui/imgui_impl_opengl3.h>
#include <filesystem>
#include <algorithm>
#include <format>
@@ -3302,7 +3304,19 @@ std::shared_ptr<gfx::shader_program> opengl46_renderer::Fetch_Shader(const std::
if (it == m_shaders.end())
{
gl::shader fragment("mat_" + name + ".frag");
gl::program *program = new gl::program({fragment, *m_vertex_shader.get()});
gl::program *program{nullptr};
// a material can move its vertices in a vertex shader of its own, mat_<name>.vert (people animated by textures,
// mat_vat.vert), the others take the common one
if (std::filesystem::exists(gfx::shader_source_dir(gfx::shader_api::opengl46) + "mat_" + name + ".vert"))
{
gl::shader vertex("mat_" + name + ".vert");
program = new gl::program({fragment, vertex});
m_ownvertexprograms.insert(program);
}
else
{
program = new gl::program({fragment, *m_vertex_shader.get()});
}
m_shaders.insert({name, std::shared_ptr<gl::program>(program)});
// the variant for the multi draws of the instances, made now rather than when the instances first come into view
if ((Global.gfx_gpucull == 1) && Global.gfx_gpucull_multidraw)
@@ -3537,6 +3551,24 @@ void opengl46_renderer::Bind_Material_Shadow(material_handle const Material)
{
auto &material = m_materials.material(Material);
if (false == m_cubeshadows)
{
// a material which moves its vertices itself casts the shadow of where they are: its vertex shader, with the
// textures and the params it reads them by
if (auto *program{Material_Shadow_Program(Material, material.textures[0] != null_handle)})
{
for (std::size_t unit = 0; unit < gl::MAX_TEXTURES; ++unit)
{
if (material.textures[unit] == null_handle)
break;
m_textures.bind(unit, material.textures[unit]);
}
memcpy(&model_ubs.param[0], &material.params[0], sizeof(model_ubs.param));
program->bind();
return;
}
}
if (material.textures[0] != null_handle)
{
m_textures.bind(0, material.textures[0]);
@@ -4100,13 +4132,11 @@ opengl46_renderer::indirect_counts opengl46_renderer::Render_Instances_Indirect_
for( std::size_t index = 0; index < count; ) {
auto const &draw { m_indirectdraws[ index ] };
auto const texture { Indirect_Shadow_Texture( draw.material ) };
if( ( texture != null_handle ) && ( false == m_textures.mark_as_used( texture ).bind( 0 ) ) ) {
// a texture which isn't there (yet) would leave the one bound before to the alpha test; no shadow until it is
++index;
continue;
}
// the materials with a vertex shader of their own are drawn one by one, with it (Bind_Material_Shadow())
auto const ownvertex = [ this ]( material_handle const Material ) {
return ( false == m_cubeshadows ) && ( Material_Shadow_Program( Material, Indirect_Shadow_Texture( Material ) != null_handle ) != nullptr ); };
auto *multidrawprogram {
m_indirectmultidraw ?
( m_indirectmultidraw && ( false == ownvertex( draw.material ) ) ) ?
( m_cubeshadows ?
m_localshadows.multidraw_program( texture != null_handle ) :
( texture != null_handle ? m_alpha_shadow_multidraw_shader.get() : m_shadow_multidraw_shader.get() ) ) :
@@ -4118,7 +4148,8 @@ opengl46_renderer::indirect_counts opengl46_renderer::Render_Instances_Indirect_
&& ( m_indirectdraws[ last ].group == draw.group )
&& ( m_indirectdraws[ last ].type == draw.type )
&& ( m_indirectcommands[ last ].indexed == m_indirectcommands[ index ].indexed )
&& ( Indirect_Shadow_Texture( m_indirectdraws[ last ].material ) == texture ) ) {
&& ( Indirect_Shadow_Texture( m_indirectdraws[ last ].material ) == texture )
&& ( false == ownvertex( m_indirectdraws[ last ].material ) ) ) {
++last;
}
multidrawprogram->bind();
@@ -4129,8 +4160,10 @@ opengl46_renderer::indirect_counts opengl46_renderer::Render_Instances_Indirect_
index = last;
}
else {
Bind_Material_Shadow( draw.material );
// the block of the draw first: Bind_Material_Shadow() puts the params of the material in it, for the materials which
// read them in their vertex shader
model_ubs = draw.ubs;
Bind_Material_Shadow( draw.material );
model_ubo->update( model_ubs );
m_geometry.draw_indirect( draw.geometry, m_indirectcull->draw_command_offset( Set, index ) );
++counts.draws;
@@ -4195,7 +4228,14 @@ gl::program *opengl46_renderer::Multidraw_Program( gl::program const *Program )
if( shader.second.get() != Program ) { continue; }
try {
gl::shader fragment( "mat_" + shader.first + ".frag", "#define MODEL_DRAWS 1\n" );
variant = std::make_unique<gl::program>( std::vector<std::reference_wrapper<const gl::shader>>{ fragment, *m_vertex_multidraw_shader } );
if( m_ownvertexprograms.count( Program ) > 0 ) {
// the vertex shader of the material itself, in its variant of the multi draws
gl::shader vertex( "mat_" + shader.first + ".vert", "#define MODEL_DRAWS 1\n" );
variant = std::make_unique<gl::program>( std::vector<std::reference_wrapper<const gl::shader>>{ fragment, vertex } );
}
else {
variant = std::make_unique<gl::program>( std::vector<std::reference_wrapper<const gl::shader>>{ fragment, *m_vertex_multidraw_shader } );
}
}
catch( gl::shader_exception const &e ) {
ErrorLog( "material " + shader.first + " drawn without multi draws: " + std::string( e.what() ) );
@@ -4207,6 +4247,35 @@ gl::program *opengl46_renderer::Multidraw_Program( gl::program const *Program )
return result;
}
gl::program *opengl46_renderer::Material_Shadow_Program( material_handle const Material, bool const Alphatest )
{
if( Material == null_handle ) { return nullptr; }
auto const &material { m_materials.material( Material ) };
if( ( false == material.is_good ) || ( material.shader == nullptr ) ) { return nullptr; }
auto const *program { static_cast<gl::program const *>( material.shader.get() ) };
if( m_ownvertexprograms.count( program ) == 0 ) { return nullptr; }
auto &programs { m_materialshadowprograms[ Alphatest ? 1 : 0 ] };
auto const lookup { programs.find( program ) };
if( lookup != programs.end() ) { return lookup->second.get(); }
std::unique_ptr<gl::program> shadow;
for( auto const &shader : m_shaders ) {
if( shader.second.get() != program ) { continue; }
try {
gl::shader vertex( "mat_" + shader.first + ".vert", "#define MATERIAL_SHADOW 1\n" );
gl::shader fragment( Alphatest ? "alphashadowmap.frag" : "shadowmap.frag" );
shadow = std::make_unique<gl::program>( std::vector<std::reference_wrapper<const gl::shader>>{ vertex, fragment } );
}
catch( gl::shader_exception const &e ) {
ErrorLog( "material " + shader.first + " casts its shadow in the pose of the model: " + std::string( e.what() ) );
}
break;
}
auto *result { shadow.get() };
programs.emplace( program, std::move( shadow ) );
return result;
}
// depth of the whole opaque scene of the colour pass, before its colour. the same walk as the colour pass makes (the cab drawn
// early, then the region), so the same things are seen at the same levels of detail; the programs of the depth prepass draw
// them (Bind_Material(), draw()), with the depth a little farther than the surface like the prepass of the instances, so the
+7 -19
View File
@@ -413,6 +413,10 @@ class opengl46_renderer : public gfx_renderer, public gfx::scene_walk {
// the variant of a material program for multi draws (built with MODEL_DRAWS), made the first time it's asked for.
// nullptr if there's none (a program not fetched by Fetch_Shader(), or one which didn't compile)
gl::program *Multidraw_Program( gl::program const *Program );
// the program of the sun shadows of a material which moves its vertices in its own vertex shader (mat_<name>.vert, see
// Fetch_Shader()): that shader with MATERIAL_SHADOW and the shadow fragment shader (Alphatest: alphashadowmap.frag).
// nullptr for the materials of the common vertex shader, which the shadow programs draw as they are
gl::program *Material_Shadow_Program( material_handle const Material, bool const Alphatest );
// how the depth prepass treats the material: 0 left out, 1 drawn without alpha test, 2 with the alpha test of the diffuse texture
int Prepass_Class( material_handle const Material );
// depth of the whole opaque scene of the colour pass, before its colour (Global.gfx_depthprepass_scene): the colour pass
@@ -651,25 +655,9 @@ class opengl46_renderer : public gfx_renderer, public gfx::scene_walk {
// programs of the multi draws: the vertex shader of the materials, variants of the material programs, of the depth prepass
std::unique_ptr<gl::shader> m_vertex_multidraw_shader;
std::unordered_map<gl::program const *, std::unique_ptr<gl::program>> m_multidrawprograms;
// Prewarm_Shaders(): the programs to draw with, the next one, and a small target of the formats of the colour pass with a
// triangle of no area to draw
struct shader_prewarm
{
bool started{ false };
std::vector<gl::program *> programs;
std::size_t next{ 0 };
std::size_t water{ 0 }; // programs of the windscreen water made ahead
bool water_done{ false };
std::unique_ptr<gl::renderbuffer> color;
std::unique_ptr<gl::renderbuffer> depth;
std::unique_ptr<gl::renderbuffer> motion;
std::unique_ptr<gl::framebuffer> target;
// one model block for the variants of the multi draws, which read theirs from a storage buffer (MODEL_DRAW_SSBO_BINDING)
std::unique_ptr<gl::ssbo> draws;
gfx::geometrybank_handle bank{};
gfx::geometry_handle triangle{};
};
shader_prewarm m_prewarm;
// the material programs with a vertex shader of their own (mat_<name>.vert), and their programs of the sun shadows
std::unordered_set<gl::program const *> m_ownvertexprograms;
std::unordered_map<gl::program const *, std::unique_ptr<gl::program>> m_materialshadowprograms[ 2 ];
std::unique_ptr<gl::program> m_prepass_multidraw_shader;
std::unique_ptr<gl::program> m_prepass_alpha_multidraw_shader;
std::unique_ptr<gl::program> m_shadow_multidraw_shader; // and of the sun shadows