Shader Program in Gamebryo
Introduction Shader? A complete Rendering Effect to apply to an object Shader Program – Vertex/Pixel Shader Shader Programs Assembly code -.vsh/.psh Compiled assembly code -.vso Microsoft HLSL -.hlsl nVidiaCG -.cg
Introduction (cont.) Shader Libraries NSB/NSF FX CG Software Vertex Processing DEVDESC_HAL_SWVERTEX / DEVDESC_HAL_MIXEDVERTEX Switching Process Mode NiD3DRenderState::SetSoftwareVertexProcessing
How to use shader program Write a Shader NSB/NSF, FX, CG Derivation from NiShader Win32 – NiD3DShaderInterface, NiD3DShader, NiD3DDefaultShader Derive NiShaderLibrary Create a Shader Library a bunch of shaders put into the shader library Register Shader Library to a Shader Factory NiShaderFactory::LoadAndRegisterShaderLibrary Use shaders through the shader factory
Shader Library A collection of shaders available to the application at run-time Provide Method Retrieve shader Retreive shader description To Use Shader Library Register to Shader Factory NiShaderFactory::LoadAndRegisterShaderLibrary
Shader System Class Interaction Diagram
Class Detail NiShader A Common interface for NiShader-NiRenderer interaction NiShader-NiRenderer interaction Obtaining a shader from the active material PreProcessPipeline Update Render-State : UsesNIRenderState Flag UpdatePipeline For all passes SetupRenderingPass SetupTransformations PrepareGeometryForRendering – Packing streams SetStreamSource, SetIndices SetupShaderPrograms PostProcessPipeline
Class Detail (cont.) NiShaderLibrary A collection of shaders available to the application at run-time Provide Method Retrieve shader Retreive shader description To Use Shader Library Register to Shader Factory NiShaderFactory::LoadAndRegisterShaderLibrary
Class Detail (cont.) NiShaderFactory Handling shaders through shader-libraries Provide Callback Method NISHADERFACTORY_CLASSCREATIONCALLBACK NISHADERFACTORY_RUNPARSERCALLBACK NISHADERFACTORY_ERRORCALLBACK Interface for Global Constants of shaders NiGPUProgram Vertex, pixel shader program
Class Detail (cont.) NiShaderConstantMap NiShaderConstantMapEntry Constant Defined common transformations, material colors Attribute per-object settings of constant values Global Operator result of some mathematical operation on other constant map entries Object represents an object in the scene – light, projected effects Data Driven NiExtraData
Class Detail (cont.) Decription classes NiShaderDescBase NiShaderDescBase NiShaderRequirementDesc NiShaderRequirementDesc NiShaderAttributeDesc NiShaderAttributeDesc NiShaderDesc NiShaderDesc NiShaderLibraryDesc NiShaderLibraryDesc
NSF/NSB Shaders Provide a data-driven method for generating and using Gamebryo's shader system Write NSF Text based file Fast Prototyping Generate NSB from NSF Binary type file NiBinaryShaderLib Reference external shader file
NiMaterial System Determine proper shader in given circumstance Generate vertex and pixel shader program code on the fly NiMaterial NiSingleShaderMaterial NiSingleShaderMaterial NiFragmentMaterial NiFragmentMaterial NiStandardMaterial NiStandardMaterial
NiMaterial NiSingleShaderMaterial NiFragmentMaterial Connected graph Shade tree ( Shade tree NiMaterialDescriptor 128-bit bitfield GenerateDescriptor Is the object skinned? What per-vertex data exists on the object? Is the object lit? How many lights are affecting it? What types of lights are affecting it? What textures are applied to the object? What UV sets do they use? Are there tangent-space bases per-vertex? Is the object affected by specularity?
NiMaterial (cont.) NiFragmentMaterial (cont.) Create shade tree NiMaterialConfigurator – shade tree NiGPUProgramDescriptor NiMaterialNode – tree node NiMaterialResource – uniform constants NiMaterialResourceBinding – connections Fallbacks Breaking into several pass or eliminate certain lights or textures
NiMaterial (cont.) NiStandardMaterial implements the default Gamebryo rendering pipeline
NiMaterial (cont.)
NiStandardMaterial (cont.) Fallback method SplitPerPixelLights SplitPerVertexLights SplitTextureMaps DropParallaxMaps DropParallaxMapThenSplitLights