• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Anfaenger

Designing efficient Material/Shader system

10 posts in this topic

I'm having trouble designing a new material system for my engine.

My primary goals are:

0) Minimal CPU overhead and memory footprint

1) I'd like to avoid setting shader parameters (uniforms) one by one
(as in 'shader scripts' approach, 'DirectX effects - style': query location by variable name, need to store metadata about uniforms, etc.).
but that means creating custom structs with the matching layout and 'hardcoding' shaders which is bad (?).

2) Improve efficiency via multithreading (issue graphics API calls in a separate 'render' thread, render commands buffering).
I intend on using two command buffers, there'll be a special UpdateConstantBuffer command,
and i'll need to copy entire constant/uniform buffer contents (each shader parameter) into the command buffer (?).
Seems like a lot of data to memcpy() around...

What are your ideas for designing a powerful, flexible and efficient graphics material system ?

Currently, i have a C++ wrapper class for each shader (generated with a tool, map CB, cast to struct, set params, unmap -> no metadata needed),
and several material classes (Phong, Plain, SSS) which i find cumbersome to use.
i'd rather have one material class, but then i don't know how to set proper shaders,
because material will be generic and shaders are hardcoded.
0

Share this post


Link to post
Share on other sites
Thank you, L. Spiro, for your reply, this is an excellent solution!


Though it surely takes a very long time to implement properly.



1. Do you also perform sematic checking (otherwise, when you translate into HLSL -> line numbers differ -> error messages useless) in your translator?


2. What parser generator did you use (or rolled your own hand-written recursive decent) ?

and i'm sure, there's a plethora of optimizations that can be done for generating the most efficient C++ wrapper code.


(i decided to use Direct3D 11 deferred contexts instead of writing a command buffer class,
so my low-level wrappers will work both with the immediate context and deferred contexts on other threads.)



3. Is it fine to create a separate constant buffer for each shader class if needed?
e.g. i have a few global, shared CBs, and i create new CBs for each shader (if it declares its own CB(s)).

4. Is it better to use a universal, generic material class instead of separate CWaterMaterial, CTerrainMaterial, CSkinMaterial ?
And how should material system talk to the low-level shader system? Through hardcoded shader variable semantics (e.g. WORLD_MTX, VIEW_POS, DIFFUSE_COLOR) ?
0

Share this post


Link to post
Share on other sites
Great, this clears up a lot.

For fast iteration, a system for reloading shaders on-the-fly is a 'must-have'.
But only affected files should be recompiled.

1. Do you track which files get #included into source files and how?
Do you keep some sort of a project file with compile timestamps ?

2. What if some shader doesn't have a particular semantic?
i'd like to avoid checks for invalid handles before making every UpdateShaderConstantByHandle() call.

3. Where are material parameters (e.g. colors, texture layers, etc.) stored?
i assume there're no such inefficient solutions as 'map<string,handle>'.
0

Share this post


Link to post
Share on other sites
1. How do you update shader constants?
- at runtime, keep shader metadata (info about used variables: name, type, offset, dimension);
- get parameter handle by string name or hardcoded semantic;
- if the handle is valid:
- - - SetParamByHandle() (copy float, matrix, int, bool into the parameter buffer);
- - - see if UniformBuffer::isDirty flag is 'true' and update the corresponding constant buffer;
?

sounds a bit messy and suboptimal, imho.

2. How do you implement support for multiple passes?

3. Could you please sketch your material class in pseudocode?

i guess, materials should only contain data declarations, no actual rendering code.
0

Share this post


Link to post
Share on other sites
Thanks, Hodgman, this info is super useful!

1. Where do you keep 'shader resources' (such as pointers to material textures) and how do you bind them to the pipeline?

2. Do your shaders carry additional information about allowed render passes, vertex formats, etc. ?
(e.g. shader for filling g-buffer should only be used during SCENE_PASS_FILL_GBUFFER.)

casting to a native C struct is a better solution than hardcoded semantics, imho.
i still can't imagine having no material class (dunno how i would reference materials in assets and set material properties).
0

Share this post


Link to post
Share on other sites
[quote name='Anfaenger' timestamp='1344084647' post='4966088']
i still can't imagine having no material class (dunno how i would reference materials in assets and set material properties).
[/quote]

I think the idea is that you don't set material properties from within the game per se. Is sounds like the renderer has no idea what artist-defined parameters it's setting, it just loads a pre-processed cbuffer blob and plugs it into the shader. Likely I'm simplifying :)

Interesting stuff, Hodgman.
0

Share this post


Link to post
Share on other sites
Thanks for the answers, hopefully, my next material system will be much better than a nasty combination of inheritance abuse and multitude of .fx files.

i've decided to bite the bullet and write my own shader language parser/translator.
it will also be used for parsing resource declarations (render targets, sampler,depth-stencil,rasterizer and blend states, state blocks and vertex formats)
and generating most of boilerplate code (especially D3D11-style verbose initialization stuff, description structs should be filled in automatically).
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0