Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Material shader assembly system (v0.6.5) #32

Merged
merged 36 commits into from Sep 27, 2022
Merged

Conversation

codeonwort
Copy link
Owner

@codeonwort codeonwort commented Sep 27, 2022

This system needs more refinement but I'm so exhausted. Merging now as basic functionalities are working.

Initial backlog

Currently all material shader variations are hard-coded.

I can't make a full GUI-based material editor now, but at least let's make a material assembler that generates material shaders.

Material shaders only define specialized things - vertex position offset, texture input, Gbuffer attributes, so on. Common code is provided by a material template.

The assembler generates final material shaders, replacing various parts of a material template with a given concrete material.

Complete rework of material shader management

Previous implementation defined a hard-coded base pass shader, a subclass of Material, and a corresponding render pass class.

For example, for a material that samples PBR textures:

  • Vertex shader deferred_pack_pbr_texture_vs.glsl
  • Fragment shader deferred_pack_pbr_texture_fs.glsl
  • class PBRTextureMaterial : public Material { ... };
  • class Deferred_Pack_PBR_Texture : public Deferred_Pack_Base { ... };

These things were defined for each material type, and I also had to add ad-hoc logic for depth prepass and shadowmap pass if a material has non-trivial depth only shader (that is, final position is not just MVP * localPosition, due to vertex animation or manual control of gl_FragDepth, etc.).

Everything above is now gone. No separate VS/FS shaders, no subclasses, no separate render pass classes.

  • Now there is a material template that acts as a kind of uber shader. It supports both deferred shading and forward shading pipeline.
  • Material files provide minimal definitions to fill placeholders in the template, including shading model, uniforms, input textures, gbuffer output, and vertex position offset. At startup the engine reads material files and generate MaterialShaders.
  • When instantiating a Material, it is related to a MaterialShader. Material clones uniform and texture parameters info from its MaterialShader.
  • If multiple Materials are created from the same MaterialShader, they have different instance IDs. Even if two Materials have the same MaterialShader, they can have their own parameters.

In rendering pipeline, mesh batches are split into an opaque mesh list and a translucent mesh list. Each list is first sorted by material shader, then by material instance ID. The opaque list is rendered in depth prepass and base pass. The translucent one is rendered in translucency pass.

TODO

  • Detect which input elements and texture units are necessary in more smart way.
  • Cleanup parsing logic in material_shader_assembler.cpp. It's quite dirty.
  • Support default values for uniform parameters.
  • Support hot-reload for material shaders.

RenderDoc says no error but when an actual drawcall is issued, pipeline viewer says 'no vertex shader bound'.

What on earth is happening :/
glShaderSource() must end with '\n'.
- Fix wrong calculation of vertex position offset.
- Set texture parameters for materials.
Let's debug in Nsight as RenderDoc says nothing
Now I can delete triplanar mapping code from PBRTextureMaterial.
- Resolve unlit after lighting passes.
This time not a driver bug but my silly typo. Also fix a case where
material UBO size is zero.
Material template now supports both deferred shading and forward shading.
Currently opaque materials always use deferred shading path, and translucent
materials always use forward shading path. If I abstract lighting
calculation further, I'm finally able to support full forward & deferred shading
pipeline both!
@codeonwort codeonwort self-assigned this Sep 27, 2022
@codeonwort codeonwort added the feature Add new feature label Sep 27, 2022
@codeonwort codeonwort merged commit 622f1d8 into master Sep 27, 2022
@codeonwort codeonwort deleted the material-assembler branch September 27, 2022 20:05
@codeonwort codeonwort added this to Done in Pathos Sep 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Add new feature
Projects
Pathos
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

1 participant