RenderTexture2DExample
parent
1568924f55
commit
d538c2c935
|
@ -22,6 +22,8 @@ class RenderTexture2DArrayExample : Example
|
||||||
Window = window;
|
Window = window;
|
||||||
GraphicsDevice = graphicsDevice;
|
GraphicsDevice = graphicsDevice;
|
||||||
|
|
||||||
|
Window.SetTitle("RenderTexture2DArray");
|
||||||
|
|
||||||
RenderTarget = Texture.CreateTexture2DArray(
|
RenderTarget = Texture.CreateTexture2DArray(
|
||||||
GraphicsDevice,
|
GraphicsDevice,
|
||||||
16,
|
16,
|
||||||
|
|
|
@ -0,0 +1,118 @@
|
||||||
|
using System;
|
||||||
|
using MoonWorks;
|
||||||
|
using MoonWorks.Graphics;
|
||||||
|
using MoonWorks.Input;
|
||||||
|
|
||||||
|
namespace MoonWorksGraphicsTests;
|
||||||
|
|
||||||
|
class RenderTexture2DExample : Example
|
||||||
|
{
|
||||||
|
private Texture[] textures = new Texture[4];
|
||||||
|
|
||||||
|
public override void Init(Window window, GraphicsDevice graphicsDevice, Inputs inputs)
|
||||||
|
{
|
||||||
|
Window = window;
|
||||||
|
GraphicsDevice = graphicsDevice;
|
||||||
|
|
||||||
|
Window.SetTitle("RenderTexture2D");
|
||||||
|
|
||||||
|
for (int i = 0; i < textures.Length; i += 1)
|
||||||
|
{
|
||||||
|
textures[i] = Texture.CreateTexture2D(
|
||||||
|
GraphicsDevice,
|
||||||
|
Window.Width / 4,
|
||||||
|
Window.Height / 4,
|
||||||
|
TextureFormat.R8G8B8A8,
|
||||||
|
TextureUsageFlags.ColorTarget | TextureUsageFlags.Sampler
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Update(System.TimeSpan delta) { }
|
||||||
|
|
||||||
|
public override void Draw(double alpha)
|
||||||
|
{
|
||||||
|
CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer();
|
||||||
|
Texture swapchainTexture = cmdbuf.AcquireSwapchainTexture(Window);
|
||||||
|
if (swapchainTexture != null)
|
||||||
|
{
|
||||||
|
var renderPass = cmdbuf.BeginRenderPass(new ColorAttachmentInfo(textures[0], false, Color.Red));
|
||||||
|
cmdbuf.EndRenderPass(renderPass);
|
||||||
|
|
||||||
|
renderPass = cmdbuf.BeginRenderPass(new ColorAttachmentInfo(textures[1], false, Color.Blue));
|
||||||
|
cmdbuf.EndRenderPass(renderPass);
|
||||||
|
|
||||||
|
renderPass = cmdbuf.BeginRenderPass(new ColorAttachmentInfo(textures[2], false, Color.Green));
|
||||||
|
cmdbuf.EndRenderPass(renderPass);
|
||||||
|
|
||||||
|
renderPass = cmdbuf.BeginRenderPass(new ColorAttachmentInfo(textures[3], false, Color.Yellow));
|
||||||
|
cmdbuf.EndRenderPass(renderPass);
|
||||||
|
|
||||||
|
cmdbuf.Blit(
|
||||||
|
textures[0],
|
||||||
|
new TextureRegion
|
||||||
|
{
|
||||||
|
TextureSlice = swapchainTexture,
|
||||||
|
Width = swapchainTexture.Width / 2,
|
||||||
|
Height = swapchainTexture.Height / 2,
|
||||||
|
Depth = 1
|
||||||
|
},
|
||||||
|
Filter.Nearest,
|
||||||
|
false
|
||||||
|
);
|
||||||
|
|
||||||
|
cmdbuf.Blit(
|
||||||
|
textures[1],
|
||||||
|
new TextureRegion
|
||||||
|
{
|
||||||
|
TextureSlice = swapchainTexture,
|
||||||
|
X = swapchainTexture.Width / 2,
|
||||||
|
Width = swapchainTexture.Width / 2,
|
||||||
|
Height = swapchainTexture.Height / 2,
|
||||||
|
Depth = 1
|
||||||
|
},
|
||||||
|
Filter.Nearest,
|
||||||
|
false
|
||||||
|
);
|
||||||
|
|
||||||
|
cmdbuf.Blit(
|
||||||
|
textures[2],
|
||||||
|
new TextureRegion
|
||||||
|
{
|
||||||
|
TextureSlice = swapchainTexture,
|
||||||
|
Y = swapchainTexture.Height / 2,
|
||||||
|
Width = swapchainTexture.Width / 2,
|
||||||
|
Height = swapchainTexture.Height / 2,
|
||||||
|
Depth = 1
|
||||||
|
},
|
||||||
|
Filter.Nearest,
|
||||||
|
false
|
||||||
|
);
|
||||||
|
|
||||||
|
cmdbuf.Blit(
|
||||||
|
textures[3],
|
||||||
|
new TextureRegion
|
||||||
|
{
|
||||||
|
TextureSlice = swapchainTexture,
|
||||||
|
X = swapchainTexture.Width / 2,
|
||||||
|
Y = swapchainTexture.Height / 2,
|
||||||
|
Width = swapchainTexture.Width / 2,
|
||||||
|
Height = swapchainTexture.Height / 2,
|
||||||
|
Depth = 1
|
||||||
|
},
|
||||||
|
Filter.Nearest,
|
||||||
|
false
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
GraphicsDevice.Submit(cmdbuf);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Destroy()
|
||||||
|
{
|
||||||
|
for (var i = 0; i < 4; i += 1)
|
||||||
|
{
|
||||||
|
textures[i].Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,129 +0,0 @@
|
||||||
using MoonWorks;
|
|
||||||
using MoonWorks.Graphics;
|
|
||||||
using MoonWorks.Math.Float;
|
|
||||||
|
|
||||||
namespace MoonWorks.Test
|
|
||||||
{
|
|
||||||
class RenderTexture2DGame : Game
|
|
||||||
{
|
|
||||||
private GraphicsPipeline pipeline;
|
|
||||||
private GpuBuffer vertexBuffer;
|
|
||||||
private GpuBuffer indexBuffer;
|
|
||||||
private Texture[] textures = new Texture[4];
|
|
||||||
private Sampler sampler;
|
|
||||||
|
|
||||||
public RenderTexture2DGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), TestUtils.PreferredBackends, 60, true)
|
|
||||||
{
|
|
||||||
// Load the shaders
|
|
||||||
ShaderModule vertShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("TexturedQuad.vert"));
|
|
||||||
ShaderModule fragShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("TexturedQuad.frag"));
|
|
||||||
|
|
||||||
// Create the graphics pipeline
|
|
||||||
GraphicsPipelineCreateInfo pipelineCreateInfo = TestUtils.GetStandardGraphicsPipelineCreateInfo(
|
|
||||||
MainWindow.SwapchainFormat,
|
|
||||||
vertShaderModule,
|
|
||||||
fragShaderModule
|
|
||||||
);
|
|
||||||
pipelineCreateInfo.VertexInputState = VertexInputState.CreateSingleBinding<PositionTextureVertex>();
|
|
||||||
pipelineCreateInfo.VertexShaderInfo = GraphicsShaderInfo.Create(vertShaderModule, "main", 0);
|
|
||||||
pipelineCreateInfo.FragmentShaderInfo.SamplerBindingCount = 1;
|
|
||||||
pipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo);
|
|
||||||
|
|
||||||
// Create sampler
|
|
||||||
SamplerCreateInfo samplerCreateInfo = SamplerCreateInfo.PointClamp;
|
|
||||||
sampler = new Sampler(GraphicsDevice, samplerCreateInfo);
|
|
||||||
|
|
||||||
// Create and populate the GPU resources
|
|
||||||
var resourceUploader = new ResourceUploader(GraphicsDevice);
|
|
||||||
|
|
||||||
vertexBuffer = resourceUploader.CreateBuffer(
|
|
||||||
[
|
|
||||||
new PositionTextureVertex(new Vector3(-1, -1, 0), new Vector2(0, 0)),
|
|
||||||
new PositionTextureVertex(new Vector3(0, -1, 0), new Vector2(1, 0)),
|
|
||||||
new PositionTextureVertex(new Vector3(0, 0, 0), new Vector2(1, 1)),
|
|
||||||
new PositionTextureVertex(new Vector3(-1, 0, 0), new Vector2(0, 1)),
|
|
||||||
|
|
||||||
new PositionTextureVertex(new Vector3(0, -1, 0), new Vector2(0, 0)),
|
|
||||||
new PositionTextureVertex(new Vector3(1, -1, 0), new Vector2(1, 0)),
|
|
||||||
new PositionTextureVertex(new Vector3(1, 0, 0), new Vector2(1, 1)),
|
|
||||||
new PositionTextureVertex(new Vector3(0, 0, 0), new Vector2(0, 1)),
|
|
||||||
|
|
||||||
new PositionTextureVertex(new Vector3(-1, 0, 0), new Vector2(0, 0)),
|
|
||||||
new PositionTextureVertex(new Vector3(0, 0, 0), new Vector2(1, 0)),
|
|
||||||
new PositionTextureVertex(new Vector3(0, 1, 0), new Vector2(1, 1)),
|
|
||||||
new PositionTextureVertex(new Vector3(-1, 1, 0), new Vector2(0, 1)),
|
|
||||||
|
|
||||||
new PositionTextureVertex(new Vector3(0, 0, 0), new Vector2(0, 0)),
|
|
||||||
new PositionTextureVertex(new Vector3(1, 0, 0), new Vector2(1, 0)),
|
|
||||||
new PositionTextureVertex(new Vector3(1, 1, 0), new Vector2(1, 1)),
|
|
||||||
new PositionTextureVertex(new Vector3(0, 1, 0), new Vector2(0, 1)),
|
|
||||||
],
|
|
||||||
BufferUsageFlags.Vertex
|
|
||||||
);
|
|
||||||
|
|
||||||
indexBuffer = resourceUploader.CreateBuffer<ushort>(
|
|
||||||
[
|
|
||||||
0, 1, 2,
|
|
||||||
0, 2, 3,
|
|
||||||
],
|
|
||||||
BufferUsageFlags.Index
|
|
||||||
);
|
|
||||||
|
|
||||||
resourceUploader.Upload();
|
|
||||||
resourceUploader.Dispose();
|
|
||||||
|
|
||||||
for (int i = 0; i < textures.Length; i += 1)
|
|
||||||
{
|
|
||||||
textures[i] = Texture.CreateTexture2D(
|
|
||||||
GraphicsDevice,
|
|
||||||
MainWindow.Width / 4,
|
|
||||||
MainWindow.Height / 4,
|
|
||||||
TextureFormat.R8G8B8A8,
|
|
||||||
TextureUsageFlags.ColorTarget | TextureUsageFlags.Sampler
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void Update(System.TimeSpan delta) { }
|
|
||||||
|
|
||||||
protected override void Draw(double alpha)
|
|
||||||
{
|
|
||||||
CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer();
|
|
||||||
Texture? backbuffer = cmdbuf.AcquireSwapchainTexture(MainWindow);
|
|
||||||
if (backbuffer != null)
|
|
||||||
{
|
|
||||||
cmdbuf.BeginRenderPass(new ColorAttachmentInfo(textures[0], WriteOptions.Cycle, Color.Red));
|
|
||||||
cmdbuf.EndRenderPass();
|
|
||||||
|
|
||||||
cmdbuf.BeginRenderPass(new ColorAttachmentInfo(textures[1], WriteOptions.Cycle, Color.Blue));
|
|
||||||
cmdbuf.EndRenderPass();
|
|
||||||
|
|
||||||
cmdbuf.BeginRenderPass(new ColorAttachmentInfo(textures[2], WriteOptions.Cycle, Color.Green));
|
|
||||||
cmdbuf.EndRenderPass();
|
|
||||||
|
|
||||||
cmdbuf.BeginRenderPass(new ColorAttachmentInfo(textures[3], WriteOptions.Cycle, Color.Yellow));
|
|
||||||
cmdbuf.EndRenderPass();
|
|
||||||
|
|
||||||
cmdbuf.BeginRenderPass(new ColorAttachmentInfo(backbuffer, WriteOptions.Cycle, Color.Black));
|
|
||||||
cmdbuf.BindGraphicsPipeline(pipeline);
|
|
||||||
cmdbuf.BindVertexBuffers(vertexBuffer);
|
|
||||||
cmdbuf.BindIndexBuffer(indexBuffer, IndexElementSize.Sixteen);
|
|
||||||
|
|
||||||
for (uint i = 0; i < textures.Length; i += 1)
|
|
||||||
{
|
|
||||||
cmdbuf.BindFragmentSamplers(new TextureSamplerBinding(textures[i], sampler));
|
|
||||||
cmdbuf.DrawIndexedPrimitives(4 * i, 0, 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
cmdbuf.EndRenderPass();
|
|
||||||
}
|
|
||||||
GraphicsDevice.Submit(cmdbuf);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void Main(string[] args)
|
|
||||||
{
|
|
||||||
RenderTexture2DGame game = new RenderTexture2DGame();
|
|
||||||
game.Run();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -41,6 +41,7 @@
|
||||||
<Compile Include="Examples\MSAACubeExample.cs" />
|
<Compile Include="Examples\MSAACubeExample.cs" />
|
||||||
<Compile Include="Examples\MSAAExample.cs" />
|
<Compile Include="Examples\MSAAExample.cs" />
|
||||||
<Compile Include="Examples\RenderTexture2DArrayExample.cs" />
|
<Compile Include="Examples\RenderTexture2DArrayExample.cs" />
|
||||||
|
<Compile Include="Examples\RenderTexture2DExample.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<Import Project=".\CopyMoonlibs.targets" />
|
<Import Project=".\CopyMoonlibs.targets" />
|
||||||
|
|
|
@ -24,7 +24,8 @@ class Program : Game
|
||||||
new InstancingAndOffsetsExample(),
|
new InstancingAndOffsetsExample(),
|
||||||
new MSAACubeExample(),
|
new MSAACubeExample(),
|
||||||
new MSAAExample(),
|
new MSAAExample(),
|
||||||
new RenderTexture2DArrayExample()
|
new RenderTexture2DArrayExample(),
|
||||||
|
new RenderTexture2DExample()
|
||||||
];
|
];
|
||||||
|
|
||||||
int ExampleIndex = 0;
|
int ExampleIndex = 0;
|
||||||
|
|
Loading…
Reference in New Issue