Ogre RT Shader System

http://www.ogre3d.org/tikiwiki/tiki-index.php?page=RT+Shader+System&structure=Development


Introduction(说明)


The Run Time Shader System, or RTSS for short, is a component of Ogre. This 
component is used to generate shaders on the fly based on object material 
properties, scene setup and other user definitions.

实时着色器系统(简称RTSS)是Ogre的组件之一,用来生成挂接在物体材质属性,场景设置,或其他用户自定义位置的着色器


Historical background(历史背景)


When the early graphic cards came into the market they contained a fixed but large set of functions with which you could influence how 3D object were rendered. These included influencing object positions using matrices, calculating the effect of textures on a pixel, calculating the effect of lights on vertices and so on. These set of functions and their implementation in hardware became later known as the graphic card fixed pipeline (or Fixed Function Pipeline). 

早期的显卡包含复杂而庞大的函数集,你可以使用这些函数对3d物体渲染施加影响。包括使用矩阵影响物体位置,计算像素纹理特效,计算顶点等。这些函数及这些函数在硬件上的实现就是后来的显卡固定管道(也叫固定函数管道)。


As graphic cards became more powerful and graphic application became more complex, a need for new ways to manipulate the rendering of 3D models became apparent. This need saw the introduction of shaders.

随着显卡越来越高效,图形应用越来越复杂,需要使用更好的方法操作3d模型渲染,着色器应运而生。

Shaders are small custom made programs that run directly on the graphics card. Using these programs, one could replace the calculations that were made by the fixed pipeline and add new functionality. However there was a catch: If shaders are used on an object, the object can no longer use any of the functionality of the fixed pipeline. Any calculation that was used in the fixed pipeline needed to be recreated in the shaders. With early graphics applications this was not problematic. Shaders were simple and their numbers were kept low. However as applications grew in complexity this meant that the need for shaders grew as well. As a programmer you were left with 2 choices, both bad. Either create an exuberant amount of small shaders that soon became too many to effectively maintain. Or create an uber shader, a huge complex shader, that soon became too complex to effectively maintain as well.

着色器是直接运行在显卡上的小型定制程序。使用这些程序,可以替代固定管道计算并增加新功能。但是,如果一个物体使用了着色器,这个物体就再也不能使用固定管道的功能了。所有固定管道使用的计算需要在着色器中重新创建。对于早期图形程序,着色器简单且种类有限,但随着应用变得越来越复杂,就需要着色器也越来越复杂种类越来越多,对程序员来说只有两种糟糕的选择。创建一群小的着色器,或者创建一个超级,巨大复杂的着色器,前者很快就会因为种类繁多变得难以维护,后者很快会变得复杂的难以维护。

The RTSS seeks to fix those problems by automatically generating shaders based on the operations previously required from the fixed pipeline and new capabilities required by the user.
实时着色器(RTSS)试图通过如下方法解决上面的问题:基于先前固定管道操作需求和用户新功能需求,动态生成着色器。

With the introduction of the version 11 of Direct3D, a new reason for having an RTSS like system became apparent. With D3D11 support for fixed pipeline functionality was removed. Meaning, you can only render objects using shaders. The RTSS becomes an excellent tool for this purpose.

从D3D11的说明文档中,我们更有理由相信我们需要实时着色器系统,D3D11放弃了固定管道功能的支持,这意味着,你只能使用着色器渲染物体。针对D3D的这种意图实时着色器将是一个很优秀的方式。


Pros and Cons(优点与不足)



Writing shading programs became a very common task when developing 3D based application during the last couple of years. 
Most of the visual effects used by 3D based applications involve shader programs. 
Here is just a short list of some common effects using shaders


Hardware animation (a.k.a hardware skinning)
Soft shadows
Normal/Bump maps
Specular maps
Advanced multi-texturing effects
...
最近几年,对于3d应用,着色器编程工作已经变得很普遍。

多数3d应用的视觉特效都包含着色器程序  

这里有一些使用着色器的产生普通特效的例子

硬件动画(硬件蒙皮?)

软阴影(??)

正常/碰撞地图

镜面地图(??)

高级多材质特效(??)



Writing shaders by hand is in many cases the best solution as one has full control of the shader code and hence optimizations based on the target scene nature can be made, etc.
So why use a runtime shader system anyway?

在很多情况下手写shader是完全控制shader代码和优化目标自然现象的最佳方案

那么我们为什么要使用RTSS呢

Save development time e.g. when your target scene has dynamic lights and the number changes, fog changes, ... and the number of material attributes increases the total count of needed shaders dramatically. It can easily cross 100 and it becomes a time consuming development task.
Reusable code - once you've written the shader extension you can use it anywhere due to its independent nature.
Custom shaders extension library - enjoy the shared library of effects created by the community. Unlike hand written shader code, which may require many adjustments to be plugged into your own shader code, using the extensions library requires minimum changes.

比如说节省开发时间。当场景中有动态灯光,物体数量改变,烟雾改变,...材质属性数量使需要的shader显著增多。shader很容易超过100个,这时开发工作就会消耗很多时间。

重复利用代码。一旦你写下一个shader的扩展功能,由于它独立的特性你可以在任何地方再次使用它。

通用shader扩展库。你可以享受公司开发的共享库中的特效,而不用自己手写,需要多次调试才能嵌入到自己原有shader代码中shader代码,共享扩展库只需要最小的修改。


Core features of the system 系统主要特性

Runtime shader generation synchronized with scene state. Each time scene 
settings change, a new set of shaders is generated.

实时Shader与场景状态同步更新。场景设置一旦更改,一组新的shader就会被生成出来。


Full FFP (Fixed Function Pipeline) emulation. This feature is most useful 
combined with render system that doesn't provide any FFP functionality (OpenGL 
ES 2.0, D3D10, D3D11 etc).

固定管道模拟,这个特性对于那些不提供固定管道的渲染系统(OpenGL 
ES 2.0, D3D10, D3D11 etc)是非常有用的


Shader language independent interface: the logic representation of the shader 
programs is completely independent from the target shader language. You can 
generate code for different shader languages from the same program.
Pluggable interface allows extending the target shader languages set.
Pluggable interface allows adding new shader based effects to the system in a 
seamless way. Each effect code will be automatically combined with the rest of 
the shader code.

shader语言独立的接口:shader程序的逻辑实现和shader语言是完全独立的,

你可以用不同的shader语言编写同样的功能。

可插拔的接口支持扩展shader语言集

可插拔的接口支持以无缝的方式向系统增加一个新的基于特效的shader


Smart program management: each shader program is created only once and may be 
used by multiple passes.

敏捷程序管理:每个shader程序只需要创建一次就可以被使用在多个渲染通路中。


Automatic vertex shader compacting mechanism: no more compacting variables by 
hand. In case the amount of used vertex shader output registers exceeds the 
maximum allowed (12 to 32, depending on D3DPSHADERCAPS2_0.NumTemps),

a compacting algorithm packs the vertex shader outputs and adds unpack code in the fragment shader side.

自动顶点shader压缩机制:不需要手动压缩变量,避免用到的顶点shader输出量数超出登记的最大允许值
这里还提供了把顶点shader输出压包的压缩算法,在shader片段上的解压算法。

Material script support, for both export and import.

材质脚本导入导出支持


High system overview 系统概述

Main components主要组件



The following is an partial list of components within the RTSS. These components are listed as they have great importance in understanding controlling and later extending the RTSS system. 

下面列出RTSS中的一些组件,这些组件对于理解控制和扩展RTSS系统有重要作用。


ShaderGenerator
The ShaderGenerator is the main interface to the RTSS system. Through it you can request to generate and destroy the shaders, influence from what parts to create the shaders, and control general system settings such as the shading language and shader caching. 
着色器生成器

着色器生成器是RTSS系统的主要接口,你可以使用着色器生成器生成和销毁着色器,决定在哪创建着色器,控制诸如着色器语言,着色器缓存等系统设置。


RenderState classes
A render state describes the different components that a shader will be created from. These components are referred to as SubRenderStates. 

RenderStates exist on 2 levels: scheme and pass. Scheme RenderStates describe the SubRenderStates that will be used when creating a shader for a given material scheme. Pass RenderState describe the SubRenderStates that will be used when creating a specific pass of a specific material. When a shader is generated for a given material the system combines the SubRenderStates from both RenderStates to create a shader specific for a material pass in a specific scheme.

渲染状态类
渲染状态描述了需要创建shader的不同组件,这些组件被叫做子渲染状态。

渲染状态有2个登记:




SubRenderState classes


Sub-render states (SRS) are components designed to generate the code of the RTSS shaders. Each SRS usually has a specific role to fill within the shader's construction. These components can be combined in different combinations to create shaders with different capabilities.


There are 5 basic SRSs. These are used to recreate the functionality provided by the fixed pipeline and are added by default to every scheme RenderState:


FFPTransform (see: OgreShaderFFPTransform.h/.cpp) - responsible for adding code to the vertex shader which computes the position of the vertex in projection space
FFPColour (see: OgreShaderFFPColour.h/.cpp) - responsible for adding code to the shaders that calculate the base diffuse and specular color of the object regardless of lights or textures. The color is calculated based on the ambient, diffuse, specular and emissive properties of the object and scene, color tracking and the specified hardware buffer color.
FFPLighting (see: OgreShaderFFPLighting.h/.cpp) - responsible for adding code to the shaders that calculate the luminescence added to the object by light. Then add that value to the color calculated by the color SRS stage.
FFPTexture (see: OgreShaderFFPTexturing.h/.cpp) - responsible for adding code that modulates the color of the pixels based on textures assigned to the material.
FFPFog (see: OgreShaderFFPFog.h/.cpp) - responsible for adding code that modulates the color of a pixel based on the scene or object fog parameters.
  
Note: There are many more sub render states that already exist in the Ogre system and new ones can be added. Some of the existing SRSs include capabilities such as: per-pixel lighting, texture atlas, advanced texture blend, bump mapping, efficient multiple lights (sample), textured fog (sample), etc...

转载于:https://www.cnblogs.com/nafio/p/9137617.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值