Skip to content

Commit 352e8e8

Browse files
committed
tracking 7.2; clarification around SV_Position
1 parent c31440d commit 352e8e8

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

articles/tutorials/advanced/2d_shaders/07_sprite_vertex_effect/index.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,21 @@ The `SpriteVertexShader` looks different from our pixel shaders in a few importa
3333

3434
### Input Semantics
3535

36-
The inputs to the vertex shader mirror the information that the []`SpriteBatch`](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch) class bundles up for each vertex. If you look at the `SpriteBatchItem`, you will see that each sprite is made up of 4 `VertexPositionColorTexture` instances:
36+
The inputs to the vertex shader mirror the information that the [`SpriteBatchItem`](https://github.com/MonoGame/MonoGame/blob/develop/MonoGame.Framework/Graphics/SpriteBatchItem.cs) class bundles up for each vertex. If you look at the `SpriteBatchItem`, you will see that each sprite is made up of 4 `VertexPositionColorTexture` instances:
3737

3838
[!code-csharp[](./snippets/snippet-7-02.cs)]
3939

40+
> [!note]
41+
> The `SpriteBatchItem` is part of the implementation of `SpriteBatch`, but `SpriteBatchItem` is not part of the public MonoGame API.
4042
41-
The [`VertexPositionColorTexture`](xref:Microsoft.Xna.Framework.Graphics.VertexPositionColorTexture) class is a standard MonoGame implementation of the `IVertexType`, and it defines a `Position`, a `Color`, and a `TextureCoordinate` for each vertex. Those should look familiar, because they align with the inputs to the vertex shader function. The alignment is not happenstance, it is enforced by "semantics" that are applied to each field in the vertex. This snippet from the `VertexPositionColorTexture` class defines the semantics for each field in the vertex by specifying the `VertexElementUsage`:
43+
The [`VertexPositionColorTexture`](xhref:Microsoft.Xna.Framework.Graphics.VertexPositionColorTexture) class is a standard MonoGame implementation of the `IVertexType`, and it defines a `Position`, a `Color`, and a `TextureCoordinate` for each vertex. Those should look familiar, because they align with the inputs to the vertex shader function. The alignment is not happenstance, it is enforced by "semantics" that are applied to each field in the vertex.
4244

43-
[!code-csharp[](./snippets/snippet-7-03.cs)]
45+
This snippet from the `VertexPositionColorTexture` class defines the semantics for each field in the vertex by specifying the `VertexElementUsage`:
4446

45-
> [!note]
46-
> MonoGame is open source, so you can go read the full code for [SpriteBatchItem](https://github.com/MonoGame/MonoGame/blob/develop/MonoGame.Framework/Graphics/SpriteBatchItem.cs) and[`VertexPositionColorTexture`](https://github.com/MonoGame/MonoGame/blob/develop/MonoGame.Framework/Graphics/Vertices/VertexPositionColorTexture.cs)
47+
[!code-csharp[](./snippets/snippet-7-03.cs)]
4748

49+
> ![tip]
50+
> MonoGame is free and open source, so you can always go read the full source-code for the [`VertexPositionColorTexture`](https://github.com/MonoGame/MonoGame/blob/develop/MonoGame.Framework/Graphics/Vertices/VertexPositionColorTexture.cs))
4851
4952
The vertex shader declares a semantic for each input using the `:` syntax:
5053

@@ -65,6 +68,18 @@ This is the _input_ struct for the standard pixel shaders from previous chapters
6568

6669
[!code-hlsl[](./snippets/snippet-7-06.hlsl)]
6770

71+
> [!tip]
72+
> What is the difference between `SV_Position` and `POSITION0` ?
73+
>
74+
> In various places in the shader code, you may notice semantics using `SV_Position` and `POSITION` interchangeably. The `SV_Position` semantic is actually specific to [Direct3D 10's System-Value Semantics](https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-semantics?redirectedfrom=MSDN#system-value-semantics). In fact, `SV_Position` is _not_ a valid semantic in DesktopGL targets, so _how_ can it be used interchangeably with `POSITION`?
75+
>
76+
> MonoGame's default shader has a trick to re-map `SV_Position` to `POSITION` only when the target is `OPENGL`:
77+
> [!code-hlsl[](./snippets/snippet-7-sv.hlsl?highlight=2)]
78+
>
79+
> The `#define` line tells the shader parser to replace any instance of `SV_POSITION` with `POSITION`.
80+
> This implies that `SV_POSITION` is converted to `POSITION` when you are targetting `OPENGL` platforms, and left "as is" when targeting DirectX.
81+
82+
6883
### Matrix Transform
6984

7085
The default sprite vertex shader uses this line:
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#if OPENGL
2+
#define SV_POSITION POSITION
3+
#define VS_SHADERMODEL vs_3_0
4+
#define PS_SHADERMODEL ps_3_0
5+
#else
6+
#define VS_SHADERMODEL vs_4_0_level_9_1
7+
#define PS_SHADERMODEL ps_4_0_level_9_1
8+
#endif

0 commit comments

Comments
 (0)