Unity Manual


  • 首页

  • 分类

  • 公益404

  • 搜索

Accessing shader properties in Cg/HLSL

发表于 2017-07-10 | 更新于 2017-07-10 | 分类于 Graphics , Graphics Reference , Shader Reference , Writing vertex and fragment shaders , Accessing shader properties in Cg/HLSL

网址:Accessing shader properties in Cg/HLSL

Accessing shader properties in Cg/HLSL

Property types in ShaderLab map to Cg/HLSL variable types this way

  • Color and Vector properties map to float4, half4 or fixed4 variables.
  • Range and Float properties map to float, half or fixed variables.
  • Texture properties map to sampler2D variables for regular (2D) textures; Cubemaps map to samplerCUBE; and 3D textures map to sampler3D.

Writing vertex and fragment shaders

发表于 2017-07-10 | 更新于 2017-07-10 | 分类于 Graphics , Graphics Reference , Shader Reference , Writing vertex and fragment shaders

网址:Writing vertex and fragment shaders

HLSL snippets

  • #pragma vertex name - compile function name as the vertex shader.
  • #pragma fragment name - compile function name as the fragment shader.
  • #pragma geometry name - compile function name as DX10 geometry shader. Having this option automatically turns on - #pragma target 4.0, described below.
  • #pragma hull name - compile function name as DX11 hull shader. Having this option automatically turns on - #pragma target 5.0, described below.
  • #pragma domain name - compile function name as DX11 domain shader. Having this option automatically turns on #pragma target 5.0, described below.

  • #pragma target name - which shader target to compile to. See Shader Compilation Targets page for details.

  • #pragma only_renderers space separated names - compile shader only for given renderers. By default shaders are compiled for all renderers. See Renderers below for details.
  • #pragma exclude_renderers space separated names - do not compile shader for given renderers. By default shaders are compiled for all renderers. See Renderers below for details.
  • #pragma multi_compile … - for working with multiple shader variants.
  • #pragma enable_d3d11_debug_symbols - generate debug information for shaders compiled for DirectX 11, this will allow you to debug shaders via Visual Studio 2012 (or higher) Graphics debugger.
  • #pragma hardware_tier_variants renderer name - generate multiple shader hardware variants of each compiled shader, for each hardware tier that could run the selected renderer. See Renderers below for details.

Rendering platforms

  • d3d9 - Direct3D 9
  • d3d11 - Direct3D 11/12
  • glcore - OpenGL 3.x/4.x
  • gles - OpenGL ES 2.0
  • gles3 - OpenGL ES 3.x
  • metal - iOS/Mac Metal
  • vulkan - Vulkan
  • d3d11_9x - Direct3D 11 9.x feature level, as commonly used on WSA platforms
  • xboxone - Xbox One
  • ps4 - PlayStation 4
  • psp2 - PlayStation Vita
  • n3ds - Nintendo 3DS
  • wiiu - Nintendo Wii U

Optimizing Shader Load Time

发表于 2017-07-07 | 更新于 2017-07-10 | 分类于 Graphics , Graphics Overview , Optimizing graphics performance , Optimizing Shader Load Time

网址:Optimizing Shader Load Time

Optimizing Shader Load Time

  • 太多 shader 影响 build time,还会增加游戏大小
  • 读取太多 shader 会影响启动时间,也会增加内存

Shader build time stripping

  • Unity 会在 Build 的时候检查用到的 shader
  • 检查 #pragma shader_feature 的使用
  • 检查场景中 Fog 和 Lightmapping 的使用

Default Unity shader loading behavior

  • Unity 读取 shader 的时候,只有当使用到具体的变种的 shader 的时候,才会去创建,这样子可以减少读取时间还有内存占用

Shader Variant Collections

  • ShaderVariantCollection 用于标记 shader 关键字
  • Graphics Settings 可以根据场景状况显示使用的 shader variant(包括运行时)

Frame Debugger

发表于 2017-07-07 | 更新于 2017-07-07 | 分类于 Graphics , Graphics Overview , Optimizing graphics performance , Frame Debugger

网址:Frame Debugger

Remote Frame Debugger

  • iOS 不支持
  • 发包时候得开启 Development Build、
  • PC 得开启 Run in Background
  • 游戏得在前台,特别注意 PC 包,无法和 Editor 同时在前台

Modeling characters for optimal performance

发表于 2017-07-07 | 更新于 2017-07-07 | 分类于 Graphics , Graphics Overview , Optimizing graphics performance , Modeling characters for optimal performance

网址:Modeling characters for optimal performance

Modeling characters for optimal performance

Use a single skinned Mesh Renderer

  • Unity 使用剔除和包围盒来优化动画
  • 限制是使用一个动画组件和一个蒙皮网格

Use as few materials as possible

  • 一般一个模型一个 material 就行了,除非是为了使用不一样的 shader,一般一个模型两到三个材质球就足够了

Use as few bones as possible

  • PC 一般50~60骨骼
  • 手机低于30

Polygon count

  • PC 一般1500~4000个面
  • 手机一般300~1500个面

Keep forward and inverse kinematics separate

  • When animations are imported, a model’s inverse kinematic (IK) nodes are baked into forward kinematics (FK) and as a result, Unity doesn’t need the IK nodes at all. However, if they are left in the model then they will have a CPU overhead even though they don’t affect the animation. You can delete the redundant IK nodes in Unity or in the modeling tool, according to your preference. Ideally, you should keep separate IK and FK hierarchies during modeling to make it easier to remove the IK nodes when necessary.

Draw call batching

发表于 2017-07-07 | 更新于 2017-07-07 | 分类于 Graphics , Graphics Overview , Optimizing graphics performance , Draw call batching

网址:Draw call batching

Draw call batching

  1. 动态合批:for small enough Meshes, this transforms their vertices on the CPU, groups many similar vertices together, and draws them all in one go.
  2. 静态合批: combines static (not moving) GameObjects into big Meshes, and renders them in a faster way.
  • 动态合批会增加 CPU 消耗,静态合批会增加内存和存储消耗

Material set-up for batching

  • 只有共用一个 material 的物体可以合批
  • 如果两个 material 仅仅是贴图不一样,可以合并图集
  • 阴影可以动态合批,因为使用的相同的 pass

Dynamic batching

  • 由于需要在 CPU 将所有物体的顶点转到世界坐标,所以仅对小物体有更好的优化效果

限制要求

  • Batching dynamic GameObjects has certain overhead per vertex, so batching is applied only to Meshes containing fewer than 900 vertex attributes in total.
    • If your Shader is using Vertex Position, Normal and single UV, then you can batch up to 300 verts, while if your Shader is using Vertex Position, Normal, UV0, UV1 and Tangent, then only 180 verts.
    • Note: attribute count limit might be changed in future.
  • GameObjects are not batched if they contain mirroring on the transform (for example GameObject A with +1 scale and GameObject B with –1 scale cannot be batched together).
  • Using different Material instances causes GameObjects not to batch together, even if they are essentially the same. The exception is shadow caster rendering.
  • GameObjects with lightmaps have additional renderer parameters: lightmap index and offset/scale into the lightmap. Generally, dynamic lightmapped GameObjects should point to exactly the same lightmap location to be batched.
  • Multi-pass Shaders break batching.
    • Almost all Unity Shaders support several Lights in forward rendering, effectively doing additional passes for them. The draw calls for “additional per-pixel lights” are not batched.
    • The Legacy Deferred (light pre-pass) rendering path has dynamic batching disabled, because it has to draw GameObjects twice.

Static batching

  • 一般比动态合批更有效率(不消耗 CPU 转换顶点),但是需要更多的内存
  • 并不减少 draw calls,减少状态转换
  • Batches are limited to 64k vertices and 64k indices on most platforms (48k indices on OpenGLES, 32k indices on macOS).
  • 是在 Build Player 阶段进行的合并网格(尽管如此还是得开启 read write enable,并且会同时存在合并后的网格和原始网格)

Tips

  • Currently, only Mesh Renderers, Trail Renderers, Line Renderers, Particle Systems and Sprite Renderers are batched. This means that skinned Meshes, Cloth, and other types of rendering components are not batched.
  • Semi-transparent Shaders usually require GameObjects to be rendered in back-to-front order for transparency to work. Unity first orders GameObjects in this order, and then tries to batch them, but because the order must be strictly satisfied, this often means less batching can be achieved than with opaque GameObjects.
  • Manually combining GameObjects that are close to each other can be a very good alternative to draw call batching. For example, a static cupboard with lots of drawers often makes sense to just combine into a single Mesh, either in a 3D modeling application or using Mesh.CombineMeshes.

Directional light shadows

发表于 2017-07-06 | 更新于 2017-07-06 | 分类于 Graphics , Graphics Overview , Lighting , Shadows , Directional light shadows

Directional light shadows

Shadow cascades

  • cascaded shadow maps 虽然会增大内存,但是也比纯增加分辨率来得好

Shadow distance

  • shadow cascades 在手机上不支持

Graphics Command Buffers

发表于 2017-07-06 | 更新于 2017-07-06 | 分类于 Graphics , Graphics Overview , Advanced Rendering Features , Graphics Command Buffers

Graphics Command Buffers

  • A high-level overview of how cameras render scene in Unity is shown below. At each point marked with a green dot, you can add command buffers to execute your commands.
    CameraRenderFlowCmdBuffers

Light troubleshooting and performance

发表于 2017-07-06 | 更新于 2017-07-20 | 分类于 Graphics , Graphics Overview , Lighting , Global Illumination , Light troubleshooting and performance

Light troubleshooting and performance

Shadow performance

  • 软阴影比起硬阴影,会更多的影响 GPU,CPU 消耗是相同的

How the size of a shadow map is calculated

  • 点光源比起其它光源更限制贴图大小,是因为使用了立方体贴图阴影,必须同时载入6张图

Troubleshooting shadows

  • 如果发现没有阴影可以根据以下去查
  1. 旧显卡可能不支持阴影
  2. Quality Settings 可以关闭阴影
  3. Mesh Renderers 是否开启了接收和投射阴影
  4. 仅有不透明物体接受和投射阴影
  5. 自定义 shader 必须使用几何队列,并且是像素光照
  6. VertexLit Shader (顶点光照 Shader)可以投射阴影,但是不能接受阴影
  7. 使用前向渲染,一些 shader 只允许最亮的光投射阴影;使用延迟渲染或者使用支持 fullforwardshadows 的表面着色器

Hardware support for shadows

  • Built-in shadows work on almost all devices supported by Unity. The following cards are supported on each platform

PC (Windows/Mac/Linux)

  • Generally all GPUs support shadows. Exceptions might occur in some really old GPUs (for example, Intel GPUs made in 2005).

Mobile

  • iPhone 4 does not support shadows. All later models starting with iPhone 4S and iPad 2 support shadows.
  • Android: Requires Android 4.0 or later, and GL_OES_depth_texture support. Most notably, some Android Tegra 2/3-based Android devices do not have this, so they don’t support shadows.
  • Windows Phone: Shadows are only supported on DX11-class GPUs (Adreno 4xx/5xx).

Optimizing graphics performance

发表于 2017-07-05 | 更新于 2017-07-06 | 分类于 Graphics , Graphics Overview , Optimizing graphics performance

Optimizing graphics performance

Locate high graphics impact

Common bottlenecks and ways to check for them

  1. GPU 一般都限制于填充率或者带宽
  • 因此可以降低分辨率
  1. CPU 一般限制于 batches 数量

Less-common bottlenecks:

  • GPU 太多顶点数,受 shader 的复杂计算影响
  • CPU 太多顶点数

CPU optimization

## Reduce the visible object count. To reduce the amount of work the CPU needs to do

  • 合并物体
  • 减少材质球,合并纹理
  • 减少渲染次数

GPU: Optimizing model geometry

  • 相比建模软件,显卡为了表示模型,有可能会展开更多的顶点
  • 如果顶点有多个法线、uv 坐标、顶点色之类的,必须划分成更多的顶点
  • 虽然模型中的几何量大多和 GPU 相关,但是 Unity 也会在 CPU 处理模型(例如蒙皮网格)

Lighting performance

  • 使用光照贴图大概快2到3倍,并且表现更好

Lights in forward rendering

  • 防止间隔太远的地方的物体合并网格并且受不同的光照,因为光照会应用与整个合并后的网格,尽管并没受到光照
  • 优化像素光照,可以减少 CPU draw calls

GPU: Texture compression and mipmaps

  • 压缩贴图,可以减少读取时间,更少的内存占用,显著提升渲染性能

Texture mipmaps

  • 打开 mipmaps,能让 GPU 使用更低像素的纹理

LOD and per-layer cull distances

  • 使用 LOD
  • camera 设置远距离裁剪
  • 给小物体单独一个 layer,然后通过 Camera.layerCullDistances 单独设置裁剪距离

Realtime shadows

  • 实时阴影会额外增加 CPU draw calls 和 GPU 的处理

GPU: Tips for writing high-performance shaders

Complex mathematical operations

  • 考虑使用查找纹理代替负责数学运算
  • 防止自己计算,尽量使用 Unity 提供的函数
  • Alpha Test 会使片段着色器变慢

Floating point precision

  • 桌面 GPU 忽视浮点精确度

Simple checklist to make your game faster

  • 在 PC 上控制顶点数少于 200K 和 3M
  • 手机上使用 Mobile 和 无光照 shader
  • 尽量少用 material,并且共用一个
  • 对于不动的物体开启 static
  • 只使用一个像素光照
  • 烘焙光照代替动态光
  • 使用压缩纹理
  • 防止使用雾效
  • 使用 Occlusion Culling
  • 使用 skybox “隐藏”远的物体
  • 使用 pixel shaders 或者合并贴图
  • shader 使用 half 精度
  • shader 中减少复杂的数学运算
  • 片段着色器少用贴图

1…4567
Fly

Fly

作者懒得写

69 日志
113 分类
GitHub CSDN
© 2018 Fly
由 Hexo 强力驱动
主题 - NexT.Pisces
0%