/
GraphicsDeviceExtensions.cs
102 lines (90 loc) · 4.92 KB
/
GraphicsDeviceExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright (c) Stride contributors (https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using System.Collections.Generic;
using Stride.Core.Mathematics;
using Stride.Rendering;
namespace Stride.Graphics
{
/// <summary>
/// Extensions for the <see cref="GraphicsDevice"/>
/// </summary>
public static class GraphicsDeviceExtensions
{
/// <summary>
/// Draws a fullscreen quad with the specified effect and parameters.
/// </summary>
/// <param name="device">The device.</param>
/// <param name="effectInstance">The effect instance.</param>
/// <exception cref="System.ArgumentNullException">effect</exception>
public static void DrawQuad(this GraphicsContext graphicsContext, EffectInstance effectInstance)
{
if (effectInstance == null) throw new ArgumentNullException("effectInstance");
// Draw a full screen quad
graphicsContext.CommandList.GraphicsDevice.PrimitiveQuad.Draw(graphicsContext, effectInstance);
}
#region DrawQuad/DrawTexture Helpers
/// <summary>
/// Draws a full screen quad. An <see cref="Effect"/> must be applied before calling this method.
/// </summary>
public static void DrawQuad(this CommandList commandList)
{
commandList.GraphicsDevice.PrimitiveQuad.Draw(commandList);
}
/// <summary>
/// Draws a fullscreen texture using a <see cref="SamplerStateFactory.LinearClamp"/> sampler. See <see cref="Draw+a+texture"/> to learn how to use it.
/// </summary>
/// <param name="texture">The texture. Expecting an instance of <see cref="Texture"/>.</param>
/// <param name="applyEffectStates">The flag to apply effect states.</param>
public static void DrawTexture(this GraphicsContext graphicsContext, Texture texture, BlendStateDescription? blendState = null)
{
graphicsContext.DrawTexture(texture, null, Color4.White, blendState);
}
/// <summary>
/// Draws a fullscreen texture using the specified sampler. See <see cref="Draw+a+texture"/> to learn how to use it.
/// </summary>
/// <param name="texture">The texture. Expecting an instance of <see cref="Texture"/>.</param>
/// <param name="sampler">The sampler.</param>
/// <param name="applyEffectStates">The flag to apply effect states.</param>
public static void DrawTexture(this GraphicsContext graphicsContext, Texture texture, SamplerState sampler, BlendStateDescription? blendState = null)
{
graphicsContext.DrawTexture(texture, sampler, Color4.White, blendState);
}
/// <summary>
/// Draws a fullscreen texture using a <see cref="SamplerStateFactory.LinearClamp"/> sampler
/// and the texture color multiplied by a custom color. See <see cref="Draw+a+texture"/> to learn how to use it.
/// </summary>
/// <param name="texture">The texture. Expecting an instance of <see cref="Texture"/>.</param>
/// <param name="color">The color.</param>
/// <param name="applyEffectStates">The flag to apply effect states.</param>
public static void DrawTexture(this GraphicsContext graphicsContext, Texture texture, Color4 color, BlendStateDescription? blendState = null)
{
graphicsContext.DrawTexture(texture, null, color, blendState);
}
/// <summary>
/// Draws a fullscreen texture using the specified sampler
/// and the texture color multiplied by a custom color. See <see cref="Draw+a+texture"/> to learn how to use it.
/// </summary>
/// <param name="texture">The texture. Expecting an instance of <see cref="Texture"/>.</param>
/// <param name="sampler">The sampler.</param>
/// <param name="color">The color.</param>
/// <param name="applyEffectStates">The flag to apply effect states.</param>
public static void DrawTexture(this GraphicsContext graphicsContext, Texture texture, SamplerState sampler, Color4 color, BlendStateDescription? blendState = null)
{
graphicsContext.CommandList.GraphicsDevice.PrimitiveQuad.Draw(graphicsContext, texture, sampler, color, blendState);
}
#endregion
public static Texture GetSharedWhiteTexture(this GraphicsDevice device)
{
return device.GetOrCreateSharedData("WhiteTexture", CreateWhiteTexture);
}
private static Texture CreateWhiteTexture(GraphicsDevice device)
{
const int Size = 2;
var whiteData = new Color[Size * Size];
for (int i = 0; i < Size * Size; i++)
whiteData[i] = Color.White;
return Texture.New2D(device, Size, Size, PixelFormat.R8G8B8A8_UNorm, whiteData);
}
}
}