Skip to content

Commit

Permalink
Texturing
Browse files Browse the repository at this point in the history
+ Added Textures
+ Added Texturing
+ Added wic conversion code
+ Singel WIC conversion entry (for texture)
- Removes reusing of previously compiled shaders (commented out)
  • Loading branch information
Ohjurot committed Apr 21, 2021
1 parent 1b4855c commit 68558c9
Show file tree
Hide file tree
Showing 16 changed files with 241 additions and 69 deletions.
4 changes: 2 additions & 2 deletions bin/source/pstates/demo.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
"class" : "D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA"
},
{
"name" : "COLOR",
"format" : "DXGI_FORMAT_R32G32B32_FLOAT",
"name" : "TEXTCORDS",
"format" : "DXGI_FORMAT_R32G32_FLOAT",
"offset" : 8,
"class" : "D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA"
}
Expand Down
4 changes: 2 additions & 2 deletions bin/source/shader/demo.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
/// </summary>
struct VS_VertexData {
float2 pos : POSITION;
float3 color : COLOR;
float2 uv : TEXTCORDS;
};

/// <summary>
/// Pixel shader input
/// </summary>
struct PS_VertexData {
float4 pos : SV_POSITION;
float3 color : COLOR;
float2 uv : TEXTCORDS;
};

/// <summary>
Expand Down
9 changes: 8 additions & 1 deletion bin/source/shader/ps_demo.hlsl
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
#include "demo.hlsli"
#include "rs_demo.hlsl"

// RS Constant
const float4 myCFloat : register(b0);

// Texture and sampler
Texture2D<float4> texture : register(t0);
sampler texSampler : register(s0);

/// <summary>
/// Pixel shader main function
/// </summary>
[RootSignature(ROOTSIG)]
OM_PixelData main(in PS_VertexData vertexIp){
OM_PixelData psOut;

psOut.color = float4(vertexIp.color, 1.0f);
psOut.color = float4(texture.Sample(texSampler, vertexIp.uv).rgb * myCFloat.rgb , 1.0f);

return psOut;
}
10 changes: 8 additions & 2 deletions bin/source/shader/rs_demo.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@
*
* Flags:
* Content:
* - NONE
* 0: float4 constant
* 1: RootTable
* X: Static sampler
*/

#define ROOTSIG \
"RootFlags(ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT),"\
"RootConstants(num32BitConstants=4, b0)"
"RootConstants(num32BitConstants=4, b0),"\
"DescriptorTable("\
" SRV(t0, numDescriptors = 1)"\
", visibility = SHADER_VISIBILITY_PIXEL),"\
"StaticSampler(s0, filter=FILTER_ANISOTROPIC)"
5 changes: 1 addition & 4 deletions bin/source/shader/vs_demo.hlsl
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#include "demo.hlsli"
#include "rs_demo.hlsl"

// RS Constant
const float4 myCFloat : register(b0);

/// <summary>
/// Vertex shader main function
/// </summary>
Expand All @@ -12,7 +9,7 @@ PS_VertexData main(in VS_VertexData vertex){
PS_VertexData vertexOut;

vertexOut.pos = float4(vertex.pos, 0.0f, 1.0f);
vertexOut.color = vertex.color * myCFloat.rgb;
vertexOut.uv = vertex.uv;

return vertexOut;
}
15 changes: 15 additions & 0 deletions bin/source/textures/auge/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Subject files:
- auge_512_512_BGR_24BPP.png
- auge_1024_1024_BGR_24BPP.png
- auge_2048_2048_BGR_24BPP.png

WARNING: The textures are based on a part of the US-Doller bill.
You have to make sure to comply all US laws regarding the
Currency Image Use!

Licensed under CC0
======================================================================
CC0 1.0 Universell (CC0 1.0)
Public Domain Dedication
https://creativecommons.org/publicdomain/zero/1.0/
https://creativecommons.org/publicdomain/zero/1.0/legalcode
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
97 changes: 84 additions & 13 deletions src/application/MainJob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
#include <dx/descriptors/RootBindings.h>
#include <engine/rendering/buffer/FixedBuffer.h>
#include <engine/resources/copyProviders/GpuUploadRingbuffer.h>
#include <engine/resources/copyProviders/GpuUploadTexturePool.h>
#include <common/Image/WICMeta.h>
#include <common/Image/WICImageProcessor.h>

struct Vertex {
float pos[2];
float color[3];
float tex[2];
};

MAIN_JOB(ytDirectXMain) {
Expand Down Expand Up @@ -62,26 +65,74 @@ MAIN_JOB(ytDirectXMain) {
// Heap
engine::GpuStackHeap stackHeap(xDevice, MEM_MiB(32));

// Texture
common::image::WIC_META fileMeta;
EXPP_ASSERT(common::image::WicIO::open(L"./source/textures/auge/auge_2048_2048_BGR_24BPP.png", fileMeta), "Failed to open texture");

// CPU Buffer
size_t imageMemorySize = fileMeta.width * fileMeta.height * ((fileMeta.bpp + 7) / 8);
void* memoryImage = malloc(imageMemorySize);
EXPP_ASSERT(memoryImage, "Memory allocation for image failed!");

// Load texture
// @mem(memoryImage, UINT8, 4, 2048, 2048, 2048 * 4)
EXPP_ASSERT(common::image::WICImageProcessor::wicToMemory(fileMeta, memoryImage, imageMemorySize), "Failed to load texture");

// Create CPU Resource
D3D12_RESOURCE_DESC texDesc;
ZeroMemory(&texDesc, sizeof(D3D12_RESOURCE_DESC));
texDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
texDesc.Width = fileMeta.width;
texDesc.Height = fileMeta.height;
texDesc.DepthOrArraySize = 1;
texDesc.MipLevels = 1;
texDesc.Format = fileMeta.targetGiFormat;
texDesc.SampleDesc.Count = 1;
texDesc.SampleDesc.Quality = 0;
texDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
texDesc.Flags = D3D12_RESOURCE_FLAG_NONE;

// Create resource
HEAP_ALLOCATION textureAllocation;
EXPP_ASSERT(stackHeap.alloc(textureAllocation, DX::XResource::size(xDevice, &texDesc)), "Texture memory allocation failed!");
DX::XResource texture(xDevice, textureAllocation, &texDesc, nullptr, D3D12_RESOURCE_STATE_COPY_DEST);

// Desriptor heap for texture
DX::XDescHeap srvHeap(xDevice, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 1);

// Textrue descriptor
D3D12_SHADER_RESOURCE_VIEW_DESC textureView;
ZeroMemory(&textureView, sizeof(D3D12_SHADER_RESOURCE_VIEW_DESC));
textureView.Format = fileMeta.targetGiFormat;
textureView.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
textureView.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
textureView.Texture2D.MipLevels = 1;
textureView.Texture2D.MostDetailedMip = 0;
textureView.Texture2D.PlaneSlice = 0;
textureView.Texture2D.ResourceMinLODClamp = 0.0f;

xDevice->CreateShaderResourceView(texture, &textureView, srvHeap->GetCPUDescriptorHandleForHeapStart());

// Close Texture File
common::image::WicIO::close(fileMeta);

// Vertex & Index buffer
engine::rendering::FixedBuffer<Vertex, 3> vertexBuffer(xDevice, &stackHeap);

vertexBuffer[0].pos[0] = -1.0f;
vertexBuffer[0].pos[1] = -1.0f;
vertexBuffer[0].color[0] = 1.0f;
vertexBuffer[0].color[1] = 0.0f;
vertexBuffer[0].color[2] = 0.0f;
vertexBuffer[0].tex[0] = 0.0f;
vertexBuffer[0].tex[1] = 1.0f;

vertexBuffer[1].pos[0] = 0.0f;
vertexBuffer[1].pos[1] = 1.0f;
vertexBuffer[1].color[0] = 0.0f;
vertexBuffer[1].color[1] = 1.0f;
vertexBuffer[1].color[2] = 0.0f;
vertexBuffer[1].tex[0] = 0.5f;
vertexBuffer[1].tex[1] = 0.0f;

vertexBuffer[2].pos[0] = 1.0f;
vertexBuffer[2].pos[1] = -1.0f;
vertexBuffer[2].color[0] = 0.0f;
vertexBuffer[2].color[1] = 0.0f;
vertexBuffer[2].color[2] = 1.0f;
vertexBuffer[2].tex[0] = 1.0f;
vertexBuffer[2].tex[1] = 1.0f;

engine::rendering::FixedBuffer<UINT32, 3> indexBuffer(xDevice, &stackHeap);
indexBuffer[0] = 0;
Expand All @@ -90,12 +141,19 @@ MAIN_JOB(ytDirectXMain) {

// Upload
engine::GpuStackHeap stackHeapUpl(xDevice, MEM_MiB(32), D3D12_HEAP_TYPE_UPLOAD);

// Buffer
HEAP_ALLOCATION allocUpload;
EXPP_ASSERT(stackHeapUpl.alloc(allocUpload, MEM_MiB(1)), "Allocation failed");
engine::GpuUploadRingbuffer rb(xDevice, allocUpload, MEM_MiB(1));

// Texture
HEAP_ALLOCATION allocUploadTexture;
EXPP_ASSERT(stackHeapUpl.alloc(allocUploadTexture, MEM_MiB(4)), "Allocation failed");
engine::GpuUploadTexturePool texUploader(xDevice, allocUploadTexture, 32, 256);

// Upload State
DX::XCommandList::WaitObject woWaitStateS, woWaitStateR, woCopy;
DX::XCommandList::WaitObject woWaitStateS, woWaitStateR, woCopy, woCopy2;
vertexBuffer.res().resourceTransition(lao, D3D12_RESOURCE_STATE_COPY_DEST);
indexBuffer.res().resourceTransition(lao, D3D12_RESOURCE_STATE_COPY_DEST);
woWaitStateS = lao.executeExchange();
Expand All @@ -104,15 +162,23 @@ MAIN_JOB(ytDirectXMain) {
rb.queueUpload(vertexBuffer.res(), vertexBuffer.ptr(), 0, vertexBuffer.size(), woCopy, woWaitStateS);
rb.queueUpload(indexBuffer.res(), indexBuffer.ptr(), 0, indexBuffer.size(), woCopy, woWaitStateS);
rb.kickoff();

texUploader.queueUploadTexture(texture, memoryImage, texDesc.Height, texDesc.Width, texDesc.Format, woCopy2, woWaitStateS);
texUploader.kickoff();

// Set state
lao.addDependency(woCopy);
lao.addDependency(woCopy2);

//
vertexBuffer.res().resourceTransition(lao, D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER);
indexBuffer.res().resourceTransition(lao, D3D12_RESOURCE_STATE_INDEX_BUFFER);
texture.resourceTransition(lao, D3D12_RESOURCE_STATE_GENERIC_READ);
lao.executeExchange().wait();

// Delete local memory
free(memoryImage);

// Views
D3D12_VERTEX_BUFFER_VIEW* ptrVBView = vertexBuffer.createVertexBufferView();
D3D12_INDEX_BUFFER_VIEW* ptrIBView = indexBuffer.createIndexBufferView();
Expand Down Expand Up @@ -142,8 +208,9 @@ MAIN_JOB(ytDirectXMain) {
EXPP_ASSERT(state.compile(xDevice), "Failed to compile PSO");

float flt[] = {1.0f, 1.0f, 1.0f, 1.0f};
dx::RootBindings<1> bd = {
dx::ROOT_ELEMENT<DX_ROOT_TYPE_CONSTANT>(sizeof(float) * 4, flt)
dx::RootBindings<2> bd = {
dx::ROOT_ELEMENT<DX_ROOT_TYPE_CONSTANT>(sizeof(float) * 4, flt),
dx::ROOT_ELEMENT<DX_ROOT_TYPE_TABLE>(srvHeap->GetGPUDescriptorHandleForHeapStart())
};

// TEMP
Expand All @@ -156,6 +223,7 @@ MAIN_JOB(ytDirectXMain) {
// TEMP
// Bind PSO
state.bind(lao);
lao->SetDescriptorHeaps(1, srvHeap.to());
bd.bind(lao);

lao->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
Expand All @@ -177,10 +245,13 @@ MAIN_JOB(ytDirectXMain) {
lao.release();

// TEMP
srvHeap.release();
state.release();
indexBuffer.release();
vertexBuffer.release();
texture.release();
rb.release();
texUploader.release();
stackHeapUpl.release();
stackHeap.release();
// TEMP
Expand Down
29 changes: 27 additions & 2 deletions src/common/Image/WICImageProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,34 @@ bool common::image::WICImageProcessor::wicToMemory(const WIC_META& inputMeta, vo
return false;
}
}
// Conversion required
else {
// TODO: Conversion
return false;
// Create converter
ScopedComPointer<IWICFormatConverter> ptrFormatConverter;
if (FAILED(ptrFactory->CreateFormatConverter(ptrFormatConverter.to()))) {
return false;
}

// Check if conversion is right
BOOL conversionAvailible = FALSE;
if (FAILED(ptrFormatConverter->CanConvert(inputMeta.nativePixelFormat, inputMeta.targetPixelFormat, &conversionAvailible)) || !conversionAvailible) {
return false;
}

// Init converter
if (FAILED(ptrFormatConverter->Initialize(ptrFrame, inputMeta.targetPixelFormat, WICBitmapDitherTypeNone, NULL, 0.0f, WICBitmapPaletteTypeCustom))) {
return false;
}

// Copy output
WICRect rcDest;
rcDest.X = 0;
rcDest.Y = 0;
rcDest.Width = inputMeta.width;
rcDest.Height = inputMeta.height;
if (FAILED(ptrFormatConverter->CopyPixels(&rcDest, targetSize / inputMeta.height, targetSize, (BYTE*)targetMemory))) {
return false;
}
}

// Passed --> true
Expand Down
37 changes: 34 additions & 3 deletions src/common/Image/WICMeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
const GUID common::image::WicIO::guid_to_dxgi_SOURCE[CLS_COMMONT_IMAGE_WICMETA__NUM_GUID_DXGI] = {
// 64 BPP
GUID_WICPixelFormat64bppRGBA, // R16G16B16A16

// 32 BPP
GUID_WICPixelFormat32bppBGR, // B8G8R8X8
GUID_WICPixelFormat32bppBGRA, // B8G8R8A8
Expand All @@ -21,6 +21,16 @@ const DXGI_FORMAT common::image::WicIO::guid_to_dxgi_TARGET[CLS_COMMONT_IMAGE_WI
DXGI_FORMAT_B8G8R8A8_UNORM,
DXGI_FORMAT_R8G8B8A8_UNORM,
};
// [GUID] --> GUID
const GUID common::image::WicIO::guid_to_guid_SOURCE[CLS_COMMONT_IMAGE_WICMETA__NUM_GUID_GUID] = {
// 24 BPP
GUID_WICPixelFormat24bppBGR,
};
// GUID --> [GUID]
const GUID common::image::WicIO::guid_to_guid_TARGET[CLS_COMMONT_IMAGE_WICMETA__NUM_GUID_GUID] = {
// 32 BPP
GUID_WICPixelFormat32bppBGR,
};

bool common::image::WicIO::open(LPCWSTR path, WIC_META& refMeta) noexcept {
// Open file
Expand Down Expand Up @@ -108,8 +118,15 @@ bool common::image::WicIO::open(IWICImagingFactory* ptrFactory, IWICBitmapDecode
refMeta.targetPixelFormat = refMeta.nativePixelFormat;
}
else {
// TODO: Try wic converter search
return false;
// Try wic converter search
if (!convert_GUID_to_GUID(refMeta.nativePixelFormat, &refMeta.targetPixelFormat)) {
return false;
}

// Find DXGI Format
if (!convert_GUID_to_DXGI(refMeta.targetPixelFormat, &refMeta.targetGiFormat)) {
return false;
}
}

// Get component info
Expand Down Expand Up @@ -151,3 +168,17 @@ bool common::image::WicIO::convert_GUID_to_DXGI(GUID inputGuid, DXGI_FORMAT* ptr
// Fallback false
return false;
}

bool common::image::WicIO::convert_GUID_to_GUID(GUID inputGuid, GUID* ptrGuid) noexcept {
// Find matching GUID
for (unsigned int i = 0; i < CLS_COMMONT_IMAGE_WICMETA__NUM_GUID_GUID; i++) {
if (guid_to_guid_SOURCE[i] == inputGuid) {
// Set and return
*ptrGuid = guid_to_guid_TARGET[i];
return true;
}
}

// Fallback false
return false;
}
Loading

0 comments on commit 68558c9

Please sign in to comment.