Skip to content

Commit

Permalink
Framework: Add Mipmap generation using ComputeShaders on D3D12.
Browse files Browse the repository at this point in the history
  • Loading branch information
amerkoleci committed Jun 28, 2024
1 parent e6c2333 commit 52847cc
Show file tree
Hide file tree
Showing 7 changed files with 480 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<ItemGroup>
<Content Include="Shaders\**" CopyToOutputDirectory="PreserveNewest" />
<Content Include="Textures\**" CopyToOutputDirectory="PreserveNewest" />

<ProjectReference Include="..\..\Vortice.Framework\Vortice.Framework.csproj" />

Expand Down
59 changes: 36 additions & 23 deletions src/Direct3D12/07_DrawTexturedCube/DrawTexturedCubeApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Text;
using Vortice.Direct3D12;
using Vortice.Dxc;
using Vortice.DXGI;
Expand Down Expand Up @@ -79,7 +80,7 @@ protected override void Initialize()
// Create pipeline
ReadOnlyMemory<byte> vertexShaderByteCode = CompileBytecode(DxcShaderStage.Vertex, "TexturedCube.hlsl", "VSMain");
ReadOnlyMemory<byte> pixelShaderByteCode = CompileBytecode(DxcShaderStage.Pixel, "TexturedCube.hlsl", "PSMain");

GraphicsPipelineStateDescription psoDesc = new()
{
RootSignature = _rootSignature,
Expand All @@ -97,29 +98,41 @@ protected override void Initialize()
_pipelineState = Device.CreateGraphicsPipelineState<ID3D12PipelineState>(psoDesc);
}

private void CreateTexture()
private void CreateTexture(bool fromFile = true)
{
Span<Color> pixels = [
0xFFFFFFFF,
0x00000000,
0xFFFFFFFF,
0x00000000,
0x00000000,
0xFFFFFFFF,
0x00000000,
0xFFFFFFFF,
0xFFFFFFFF,
0x00000000,
0xFFFFFFFF,
0x00000000,
0x00000000,
0xFFFFFFFF,
0x00000000,
0xFFFFFFFF,
];

_texture = D3D12ResourceUtils.CreateTexture2D(Device, UploadBatch, 4, 4,
Format.R8G8B8A8_UNorm, pixels);
if (fromFile)
{
string assetsPath = Path.Combine(AppContext.BaseDirectory, "Textures");
string textureFile = Path.Combine(assetsPath, "10points.png");

Image image = Image.FromFile(textureFile)!;
_texture = D3D12ResourceUtils.CreateTexture2D(Device, UploadBatch,
image.Width, image.Height, image.Format, image.Data.Span,
generateMips: true);
}
else
{
Span<Color> pixels = [
0xFFFFFFFF,
0x00000000,
0xFFFFFFFF,
0x00000000,
0x00000000,
0xFFFFFFFF,
0x00000000,
0xFFFFFFFF,
0xFFFFFFFF,
0x00000000,
0xFFFFFFFF,
0x00000000,
0x00000000,
0xFFFFFFFF,
0x00000000,
0xFFFFFFFF,
];

_texture = D3D12ResourceUtils.CreateTexture2D(Device, UploadBatch, 4, 4, Format.R8G8B8A8_UNorm, pixels);
}
}

protected override void OnDestroy()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ PSInput VSMain(in VSInput input)
PSInput result;
result.Position = mul(worldViewProjection, float4(input.Position, 1.0f));
result.Normal = input.Normal;
result.Texcoord = input.Texcoord * 5.0f;
result.Texcoord = input.Texcoord;
return result;
}

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 52847cc

Please sign in to comment.