M2/Rendering: Difference between revisions

From wowdev
Jump to navigation Jump to search
(Reorg continued)
Line 1: Line 1:
= M2Element =
== M2Element ==


An <tt>M2</tt> is rendered as a series of <tt>M2Element</tt>s which are constructed at runtime in <tt>CM2SceneRender::BeginDraw()</tt>, and sorted prior to drawing.
An <tt>M2</tt> is rendered as a series of <tt>M2Element</tt>s which are constructed at runtime in <tt>CM2SceneRender::BeginDraw()</tt>, and sorted prior to drawing.


== Element Alpha ==
=== Element Alpha ===


For a given <tt>M2Element</tt>, whole-element alpha is calculated by multiplying the following things together:
For a given <tt>M2Element</tt>, whole-element alpha is calculated by multiplying the following things together:
Line 17: Line 17:
</syntaxhighlight>
</syntaxhighlight>


== Element Sorting ==
=== Element Sorting ===


TODO
TODO


= Blending Modes =
== Blending Modes ==


Blending modes used when drawing M2 batches can be found on the [[Rendering]] page.
Blending modes used when drawing M2 batches can be found on the [[Rendering]] page.


== M2Material Blending Mode to Pixel Blending State ==
=== M2Material Blending Mode to Pixel Blending State ===


In Wrath of the Lich King (and possibly other clients), the value listed in <tt>M2Material</tt> <tt>blending_mode</tt> is not a direct lookup in the pixel blending state table. The value is translated through the following mapping table: <tt>{ 0, 1, 2, 10, 3, 4, 5 }</tt>
In Wrath of the Lich King (and possibly other clients), the value listed in <tt>M2Material</tt> <tt>blending_mode</tt> is not a direct lookup in the pixel blending state table. The value is translated through the following mapping table: <tt>{ 0, 1, 2, 10, 3, 4, 5 }</tt>


= Fog Modes =
== Fog Modes ==


Because blending modes adjust how color is applied during a draw call, fog color often has to be modified accordingly. The client maps blending modes to fog modes using <tt>s_fogModeList</tt>.
Because blending modes adjust how color is applied during a draw call, fog color often has to be modified accordingly. The client maps blending modes to fog modes using <tt>s_fogModeList</tt>.
Line 74: Line 74:
|}
|}


= Lighting Modes =
== Lighting Modes ==


Similar to fog modes above, blending modes also impact lighting. The client maps blending modes to lighting modes using <tt>s_shadedList</tt>.
Similar to fog modes above, blending modes also impact lighting. The client maps blending modes to lighting modes using <tt>s_shadedList</tt>.
Line 97: Line 97:
|}
|}


= Alpha Testing =
== Alpha Testing ==


Alpha testing is enabled for most or all <tt>M2Element</tt>s being rendered. The alpha comparison function is always <tt>D3DCMP_GREATEREQUAL</tt>, which discards pixels with alpha values lower than the provided <tt>alphaRef</tt>.
Alpha testing is enabled for most or all <tt>M2Element</tt>s being rendered. The alpha comparison function is always <tt>D3DCMP_GREATEREQUAL</tt>, which discards pixels with alpha values lower than the provided <tt>alphaRef</tt>.
Line 122: Line 122:
|}
|}


= Shading =
== Shading ==


== Vertex Shaders ==
=== Vertex Shaders ===


TODO
TODO


== Pixel Shaders ==
=== Pixel Shaders ===


The following pixel shaders are used to blend mesh color and textures together, prior to drawing the relevant geometry to the framebuffer.
The following pixel shaders are used to blend mesh color and textures together, prior to drawing the relevant geometry to the framebuffer.
Line 292: Line 292:
* In the interest of clarity / brevity, no lighting or fogging operations are shown
* In the interest of clarity / brevity, no lighting or fogging operations are shown


= Particle Emitters =
== Particle Emitters ==


== CParticleMat ==
=== CParticleMat ===


<tt>CParticleMat</tt> structs are set up by the client at runtime, based on the data present in the <tt>M2Particle</tt> structs in the M2 data files.
<tt>CParticleMat</tt> structs are set up by the client at runtime, based on the data present in the <tt>M2Particle</tt> structs in the M2 data files.
Line 311: Line 311:
</syntaxhighlight>
</syntaxhighlight>


=== Blending Type ===
==== Blending Type ====


<tt>CParticleMat->blending</tt> is set to the result of the lookup of <tt>M2Particle->blendingType</tt> in <tt>s_gxBlend[]</tt>. The value translates to a mode from <tt>EGxBlend</tt> (pixel blending modes).
<tt>CParticleMat->blending</tt> is set to the result of the lookup of <tt>M2Particle->blendingType</tt> in <tt>s_gxBlend[]</tt>. The value translates to a mode from <tt>EGxBlend</tt> (pixel blending modes).
Line 317: Line 317:
In more recent clients, <tt>s_gxBlend[]</tt> maps 1:1 to <tt>EGxBlend</tt>. In older clients, <tt>s_gxBlend</tt> can be a bit different. [[Rendering#Pixel_Blending|Learn more about pixel blending]].
In more recent clients, <tt>s_gxBlend[]</tt> maps 1:1 to <tt>EGxBlend</tt>. In older clients, <tt>s_gxBlend</tt> can be a bit different. [[Rendering#Pixel_Blending|Learn more about pixel blending]].


=== Flags ===
==== Flags ====


<tt>0.5.3.3368</tt> through <tt>3.3.5.12340</tt>:
<tt>0.5.3.3368</tt> through <tt>3.3.5.12340</tt>:
Line 336: Line 336:
|}
|}


=== Logic ===
==== Logic ====


3.3.5a:
3.3.5a:

Revision as of 19:28, 21 October 2017

M2Element

An M2 is rendered as a series of M2Elements which are constructed at runtime in CM2SceneRender::BeginDraw(), and sorted prior to drawing.

Element Alpha

For a given M2Element, whole-element alpha is calculated by multiplying the following things together:

  • batch->color.alpha is the M2Batch animated color track alpha value (if present)
  • batch->textureWeight is the M2Batch animated texture weight track value (if present); if batch->opCount is > 1, only the first textureWeight value is used
  • m2->alpha is overall model opacity, which can be manipulated by things like distance fading and similar effects; default: 1.0
  element->alpha = batch->color.alpha * batch->textureWeight * m2->alpha;

Element Sorting

TODO

Blending Modes

Blending modes used when drawing M2 batches can be found on the Rendering page.

M2Material Blending Mode to Pixel Blending State

In Wrath of the Lich King (and possibly other clients), the value listed in M2Material blending_mode is not a direct lookup in the pixel blending state table. The value is translated through the following mapping table: { 0, 1, 2, 10, 3, 4, 5 }

Fog Modes

Because blending modes adjust how color is applied during a draw call, fog color often has to be modified accordingly. The client maps blending modes to fog modes using s_fogModeList.

M2Material flag 0x02 causes fog mode 0 (disabled) to always be selected, regardless of blending mode.

In certain cases (possibly when the mesh or the camera is underwater), s_blendDisableUWFog may force fog mode 0 (disabled) to be selected based on blending mode. As of Mists of Pandaria, it appears this applies to Blend_Add and Blend_NoAlphaAdd.

The following fog modes were taken from Mists of Pandaria (build 15662).

Fog Mode Action Blending Modes
0 Disable fog logic in shader -
1 Use fog color (without changes) Blend_Opaque, Blend_AlphaKey, Blend_Alpha
2 Override fog color to 0x000000 (black) Blend_Add, possibly: Blend_NoAlphaAdd
3 Override fog color to 0xFFFFFF (white) Blend_Mod
4 Override fog color to 0x808080 (half white) Blend_Mod2x
5 ? submerged camera related; use ViewSettings::s_fogInfo 2 or 3 -
6 ? liquid plane related; use ViewSettings::s_fogInfo 3 or use CM2Lighting fog -

Lighting Modes

Similar to fog modes above, blending modes also impact lighting. The client maps blending modes to lighting modes using s_shadedList.

M2Material flag 0x01 always causes lighting mode 0 (disabled) to be selected.

The following lighting modes were taken from Mists of Pandaria (build 15662).

Lighting Mode Action Blending Modes
0 Disable lighting logic in shader, including local lights Blend_Mod, Blend_Mod2x
1 Enable lighting logic in shader Blend_Opaque, Blend_AlphaKey, Blend_Alpha, Blend_Add, possibly: Blend_NoAlphaAdd

Alpha Testing

Alpha testing is enabled for most or all M2Elements being rendered. The alpha comparison function is always D3DCMP_GREATEREQUAL, which discards pixels with alpha values lower than the provided alphaRef.

For Blend_AlphaKey blending, the alphaRef value starts at a baseline, and is multiplied by the M2Element->alpha. This multiplication prevents inappropriate pixel discards during things like fades for distance culling. See the M2Element alpha section above for details on what can modify M2Element alpha.

For all other blending modes (ie !(Blend_AlphaKey), the alphaRef value is constant.

Blending Mode alphaRef
!(Blend_AlphaKey) All 1.0 / 255.0 (constant)
Blend_AlphaKey >= Cata (128.0 / 255.0) * element->alpha
<= WotLK (224.0 / 255.0) * element->alpha

Shading

Vertex Shaders

TODO

Pixel Shaders

The following pixel shaders are used to blend mesh color and textures together, prior to drawing the relevant geometry to the framebuffer.

See the texture blending section on the main rendering page for more information on the intent and history of the logic performed in the pixel shaders.

As with the other shaders used by the game, the M2 pixel shaders are housed within BLS files in the shaders directory in the game data.

Shader Name # Tex RGB Logic Alpha Logic
Combiners_Add 1 out.rgb = in.rgb + tex0.rgb; out.a = in.a + tex0.a;
Combiners_Decal 1 out.rgb = mix(in.rgb, tex0.rgb, in.a); out.a = in.a;
Combiners_Fade 1 out.rgb = mix(tex0.rgb, in.rgb, in.a); out.a = in.a;
Combiners_Mod 1 out.rgb = in.rgb * tex0.rgb; out.a = in.a * tex0.a;
Combiners_Mod2x 1 out.rgb = in.rgb * tex0.rgb * 2.0; out.a = in.a * tex0.a * 2.0;
Combiners_Opaque 1 out.rgb = in.rgb * tex0.rgb; out.a = in.a;
Combiners_Add_Add 2 out.rgb = (in.rgb + tex0.rgb) + tex1.rgb; out.a = (in.a + tex0.a) + tex1.a;
Combiners_Add_Mod 2 out.rgb = (in.rgb + tex0.rgb) * tex1.rgb; out.a = (in.a + tex0.a) * tex1.a;
Combiners_Add_Mod2x 2 out.rgb = (in.rgb + tex0.rgb) * tex1.rgb * 2.0; out.a = (in.a + tex0.a) * tex1.a * 2.0;
Combiners_Add_Opaque 2 out.rgb = (in.rgb + tex0.rgb) * tex1.rgb; out.a = in.a + tex0.a;
Combiners_Mod_Add 2 out.rgb = (in.rgb * tex0.rgb) + tex1.rgb; out.a = (in.a * tex0.a) + tex1.a;
Combiners_Mod_AddNA 2 out.rgb = (in.rgb * tex0.rgb) + tex1.rgb; out.a = in.a * tex0.a;
Combiners_Mod_Mod 2 out.rgb = (in.rgb * tex0.rgb) * tex1.rgb; out.a = (in.a * tex0.a) * tex1.a;
Combiners_Mod_Mod2x 2 out.rgb = (in.rgb * tex0.rgb) * tex1.rgb * 2.0; out.a = (in.a * tex0.a) * tex1.a * 2.0;
Combiners_Mod_Mod2xNA 2 out.rgb = (in.rgb * tex0.rgb) * tex1.rgb * 2.0; out.a = in.a * tex0.a;
Combiners_Mod_Opaque 2 out.rgb = (in.rgb * tex0.rgb) * tex1.rgb; out.a = in.a * tex0.a;
Combiners_Mod2x_Add 2 // TODO // TODO
Combiners_Mod2x_Mod2x 2 // TODO // TODO
Combiners_Mod2x_Opaque 2 // TODO // TODO
Combiners_Opaque_Add 2 out.rgb = (in.rgb * tex0.rgb) + tex1.rgb; out.a = in.a + tex1.a;
Combiners_Opaque_AddAlpha 2 out.rgb = (in.rgb * tex0.rgb) + (tex1.rgb * tex1.a); out.a = in.a;
Combiners_Opaque_AddAlpha_Alpha 2 out.rgb = (in.rgb * tex0.rgb) + (tex1.rgb * tex1.a * tex0.a); out.a = in.a;
Combiners_Opaque_AddNA 2 out.rgb = (in.rgb * tex0.rgb) + tex1.rgb; out.a = in.a;
Combiners_Opaque_Mod 2 out.rgb = (in.rgb * tex0.rgb) * tex1.rgb; out.a = in.a * tex1.a;
Combiners_Opaque_Mod2x 2 out.rgb = (in.rgb * tex0.rgb) * tex1.rgb * 2.0; out.a = in.a * tex1.a * 2.0;
Combiners_Opaque_Mod2xNA 2 out.rgb = (in.rgb * tex0.rgb) * tex1.rgb * 2.0; out.a = in.a;
Combiners_Opaque_Mod2xNA_Alpha 2 out.rgb = (in.rgb * tex0.rgb) * mix(tex1.rgb * 2.0, vec3(1.0), tex0.a); out.a = in.a;
Combiners_Opaque_Opaque 2 out.rgb = (in.rgb * tex0.rgb) * tex1.rgb; out.a = in.a;

Table Notes

  • tex0.rgba, tex1.rgba, etc are vec4s containing sampled textures
  • in.rgba is a vec4 containing the input color for the pixel shader, which comes from the output of the vertex shader
  • out.rgba is a vec4 containing the result of texture blending
  • # Tex shows the number of textures required for the given shader
  • In the interest of clarity / brevity, no lighting or fogging operations are shown

Particle Emitters

CParticleMat

CParticleMat structs are set up by the client at runtime, based on the data present in the M2Particle structs in the M2 data files.

The structure is quite simple:

struct CParticleMat {
  uint32_t blending;

  // flags
  __int32 enableLighting : 1;
  __int32 enableFog : 1;
  __int32 enableDepthWrites : 1;
};

Blending Type

CParticleMat->blending is set to the result of the lookup of M2Particle->blendingType in s_gxBlend[]. The value translates to a mode from EGxBlend (pixel blending modes).

In more recent clients, s_gxBlend[] maps 1:1 to EGxBlend. In older clients, s_gxBlend can be a bit different. Learn more about pixel blending.

Flags

0.5.3.3368 through 3.3.5.12340:

Value Flag
0x1 enableLighting
0x2 enableFog
0x4 enableDepthWrites

Logic

3.3.5a:

  CParticleMat newMat;

  newMat->enableLighting = 1;
  newMat->enableFog = 1;
  newMat->enableDepthWrites = 1;

  EGxBlend gxBlend = s_gxBlend[m2Particle->blendingType];

  newMat->blending = gxBlend;

  switch (gxBlend) {
    case GxBlend_Opaque:
    case GxBlend_AlphaKey:
      newMat->enableDepthWrites = 1;
      break;

    case GxBlend_Alpha:
    case GxBlend_Mod:
    case GxBlend_Mod2x:
    case GxBlend_NoAlphaAdd:
      newMat->enableDepthWrites = 0;
      break;

    default:
      break;
  }