diff --git a/WickedEngine/ConstantBufferMapping.h b/WickedEngine/ConstantBufferMapping.h
index 36e19f160..1d427b4e4 100644
--- a/WickedEngine/ConstantBufferMapping.h
+++ b/WickedEngine/ConstantBufferMapping.h
@@ -27,7 +27,8 @@
#define CBSLOT_RENDERER_DISPATCHPARAMS 8
#define CBSLOT_RENDERER_VOXELIZER 8
#define CBSLOT_RENDERER_TRACED 8
-#define CBSLOT_RENDERER_BVH 8
+#define CBSLOT_RENDERER_BVH 8
+#define CBSLOT_RENDERER_UTILITY 8
#define CBSLOT_OTHER_EMITTEDPARTICLE 8
#define CBSLOT_OTHER_HAIRPARTICLE 8
diff --git a/WickedEngine/ShaderInterop_Utility.h b/WickedEngine/ShaderInterop_Utility.h
new file mode 100644
index 000000000..c5acc6f0b
--- /dev/null
+++ b/WickedEngine/ShaderInterop_Utility.h
@@ -0,0 +1,11 @@
+#ifndef _SHADERINTEROP_UTILITY_H_
+#define _SHADERINTEROP_UTILITY_H_
+#include "ShaderInterop.h"
+
+CBUFFER(CopyTextureCB, CBSLOT_RENDERER_UTILITY)
+{
+ uint2 xCopyDest;
+ uint2 padding0;
+};
+
+#endif //_SHADERINTEROP_UTILITY_H_
diff --git a/WickedEngine/WickedEngine_SHADERS.vcxproj b/WickedEngine/WickedEngine_SHADERS.vcxproj
index 1e44e59b3..71aee08bb 100644
--- a/WickedEngine/WickedEngine_SHADERS.vcxproj
+++ b/WickedEngine/WickedEngine_SHADERS.vcxproj
@@ -70,6 +70,10 @@
Pixel
+
+ Compute
+ 5.0
+
Pixel
diff --git a/WickedEngine/WickedEngine_SHADERS.vcxproj.filters b/WickedEngine/WickedEngine_SHADERS.vcxproj.filters
index cc66c50c4..9bfebabe4 100644
--- a/WickedEngine/WickedEngine_SHADERS.vcxproj.filters
+++ b/WickedEngine/WickedEngine_SHADERS.vcxproj.filters
@@ -756,6 +756,9 @@
CS
+
+ CS
+
diff --git a/WickedEngine/WickedEngine_SHARED.vcxitems b/WickedEngine/WickedEngine_SHARED.vcxitems
index f64307d6a..1bb2be231 100644
--- a/WickedEngine/WickedEngine_SHARED.vcxitems
+++ b/WickedEngine/WickedEngine_SHARED.vcxitems
@@ -246,6 +246,7 @@
+
diff --git a/WickedEngine/WickedEngine_SHARED.vcxitems.filters b/WickedEngine/WickedEngine_SHARED.vcxitems.filters
index 3e93cf3de..22b43b1f3 100644
--- a/WickedEngine/WickedEngine_SHARED.vcxitems.filters
+++ b/WickedEngine/WickedEngine_SHARED.vcxitems.filters
@@ -1185,6 +1185,9 @@
ENGINE\Components
+
+ ENGINE\Graphics\GPUMapping
+
diff --git a/WickedEngine/copytexture2Dregion_unorm4CS.hlsl b/WickedEngine/copytexture2Dregion_unorm4CS.hlsl
new file mode 100644
index 000000000..adcd9f414
--- /dev/null
+++ b/WickedEngine/copytexture2Dregion_unorm4CS.hlsl
@@ -0,0 +1,12 @@
+#include "globals.hlsli"
+#include "ShaderInterop_Utility.h"
+
+TEXTURE2D(input, float4, TEXSLOT_ONDEMAND0);
+
+RWTEXTURE2D(output, unorm float4, 0);
+
+[numthreads(8, 8, 1)]
+void main( uint3 DTid : SV_DispatchThreadID )
+{
+ output[DTid.xy + xCopyDest] = input[DTid.xy];
+}
diff --git a/WickedEngine/wiEnums.h b/WickedEngine/wiEnums.h
index e54b2adb5..0672c0017 100644
--- a/WickedEngine/wiEnums.h
+++ b/WickedEngine/wiEnums.h
@@ -83,6 +83,7 @@ enum CBTYPES
CBTYPE_CLOUDGENERATOR,
CBTYPE_BVH,
CBTYPE_RAYTRACE,
+ CBTYPE_COPYTEXTURE,
CBTYPE_LAST
};
@@ -269,6 +270,7 @@ enum CSTYPES
CSTYPE_GENERATEMIPCHAIN2D_GAUSSIAN,
CSTYPE_GENERATEMIPCHAIN3D_SIMPLEFILTER,
CSTYPE_GENERATEMIPCHAIN3D_GAUSSIAN,
+ CSTYPE_COPYTEXTURE2DREGION_UNORM4,
CSTYPE_SKINNING,
CSTYPE_SKINNING_LDS,
CSTYPE_CLOUDGENERATOR,
diff --git a/WickedEngine/wiRenderer.cpp b/WickedEngine/wiRenderer.cpp
index fc6ec1115..74f73b52b 100644
--- a/WickedEngine/wiRenderer.cpp
+++ b/WickedEngine/wiRenderer.cpp
@@ -28,6 +28,7 @@
#include "ShaderInterop_Skinning.h"
#include "ShaderInterop_TracedRendering.h"
#include "ShaderInterop_BVH.h"
+#include "ShaderInterop_Utility.h"
#include "wiWidget.h"
#include "wiGPUSortLib.h"
@@ -603,6 +604,9 @@ void wiRenderer::LoadBuffers()
bd.ByteWidth = sizeof(BVHCB);
GetDevice()->CreateBuffer(&bd, nullptr, constantBuffers[CBTYPE_BVH]);
+ bd.ByteWidth = sizeof(CopyTextureCB);
+ GetDevice()->CreateBuffer(&bd, nullptr, constantBuffers[CBTYPE_COPYTEXTURE]);
+
@@ -1472,6 +1476,7 @@ void wiRenderer::LoadShaders()
computeShaders[CSTYPE_GENERATEMIPCHAIN2D_GAUSSIAN] = static_cast(wiResourceManager::GetShaderManager()->add(SHADERPATH + "generateMIPChain2D_GaussianCS.cso", wiResourceManager::COMPUTESHADER));
computeShaders[CSTYPE_GENERATEMIPCHAIN3D_SIMPLEFILTER] = static_cast(wiResourceManager::GetShaderManager()->add(SHADERPATH + "generateMIPChain3D_SimpleFilterCS.cso", wiResourceManager::COMPUTESHADER));
computeShaders[CSTYPE_GENERATEMIPCHAIN3D_GAUSSIAN] = static_cast(wiResourceManager::GetShaderManager()->add(SHADERPATH + "generateMIPChain3D_GaussianCS.cso", wiResourceManager::COMPUTESHADER));
+ computeShaders[CSTYPE_COPYTEXTURE2DREGION_UNORM4] = static_cast(wiResourceManager::GetShaderManager()->add(SHADERPATH + "copytexture2Dregion_unorm4CS.cso", wiResourceManager::COMPUTESHADER));
computeShaders[CSTYPE_SKINNING] = static_cast(wiResourceManager::GetShaderManager()->add(SHADERPATH + "skinningCS.cso", wiResourceManager::COMPUTESHADER));
computeShaders[CSTYPE_SKINNING_LDS] = static_cast(wiResourceManager::GetShaderManager()->add(SHADERPATH + "skinningCS_LDS.cso", wiResourceManager::COMPUTESHADER));
computeShaders[CSTYPE_CLOUDGENERATOR] = static_cast(wiResourceManager::GetShaderManager()->add(SHADERPATH + "cloudGeneratorCS.cso", wiResourceManager::COMPUTESHADER));
@@ -1761,6 +1766,7 @@ void wiRenderer::LoadShaders()
desc.rs = rasterizers[RSTYPE_FRONT];
desc.bs = blendStates[BSTYPE_DECAL];
desc.dss = depthStencils[DSSTYPE_DECAL];
+ desc.pt = TRIANGLESTRIP;
desc.numRTs = 1;
desc.RTFormats[0] = RTFormat_gbuffer_0;
@@ -6330,6 +6336,56 @@ void wiRenderer::GenerateMipChain(Texture3D* texture, MIPGENFILTER filter, GRAPH
GetDevice()->EventEnd(threadID);
}
+void wiRenderer::CopyTexture2D_Region(Texture2D* dst, UINT DstMIP, UINT DstX, UINT DstY, Texture2D* src, UINT SrcMIP, GRAPHICSTHREAD threadID)
+{
+ GraphicsDevice* device = GetDevice();
+
+ const TextureDesc& desc_dst = dst->GetDesc();
+ const TextureDesc& desc_src = src->GetDesc();
+
+ assert(desc_dst.BindFlags & BIND_UNORDERED_ACCESS);
+ assert(desc_src.BindFlags & BIND_SHADER_RESOURCE);
+
+ device->EventBegin("CopyTexture2D_Region_UNORM4", threadID);
+
+ device->BindComputePSO(CPSO[CSTYPE_COPYTEXTURE2DREGION_UNORM4], threadID);
+
+ CopyTextureCB cb;
+ cb.xCopyDest.x = DstX;
+ cb.xCopyDest.y = DstY;
+ device->UpdateBuffer(constantBuffers[CBTYPE_COPYTEXTURE], &cb, threadID);
+
+ device->BindConstantBuffer(CS, constantBuffers[CBTYPE_COPYTEXTURE], CB_GETBINDSLOT(CopyTextureCB), threadID);
+
+ //if (SrcMIP > 0)
+ //{
+ // assert(desc_src.MipLevels > SrcMIP);
+ // device->BindResource(CS, src, TEXSLOT_ONDEMAND0, threadID, SrcMIP);
+ //}
+ //else
+ {
+ device->BindResource(CS, src, TEXSLOT_ONDEMAND0, threadID);
+ }
+
+ if (DstMIP > 0)
+ {
+ assert(desc_dst.MipLevels > DstMIP);
+ device->BindUnorderedAccessResourceCS(dst, 0, threadID, DstMIP);
+ }
+ else
+ {
+ device->BindUnorderedAccessResourceCS(dst, 0, threadID);
+ }
+
+ device->Dispatch((UINT)ceilf((float)desc_src.Width / 8.0f), (UINT)ceilf((float)desc_src.Height / 8.0f), 1, threadID);
+
+ device->UnBindUnorderedAccessResources(0, 1, threadID);
+
+ device->EventEnd(threadID);
+}
+
+
+
// Should I care that usually buffers are not declared here?
static GPUBuffer* bvhNodeBuffer = nullptr;
static GPUBuffer* bvhAABBBuffer = nullptr;
@@ -6851,25 +6907,19 @@ void wiRenderer::DrawTracedScene(Camera* camera, wiGraphicsTypes::Texture2D* res
desc.Height = (UINT)bins[0].size.h;
desc.MipLevels = 1;
desc.ArraySize = 1;
- desc.Format = FORMAT_B8G8R8A8_UNORM;
+ desc.Format = FORMAT_R8G8B8A8_UNORM;
desc.SampleDesc.Count = 1;
desc.SampleDesc.Quality = 0;
desc.Usage = USAGE_DEFAULT;
- desc.BindFlags = BIND_SHADER_RESOURCE;
+ desc.BindFlags = BIND_SHADER_RESOURCE | BIND_UNORDERED_ACCESS;
desc.CPUAccessFlags = 0;
desc.MiscFlags = 0;
device->CreateTexture2D(&desc, nullptr, &atlasTexture);
- for (UINT mip = 0; mip < atlasTexture->GetDesc().MipLevels; ++mip)
+ for (auto& it : storedTextures)
{
- for (auto& it : storedTextures)
- {
- if (mip < it.first->GetDesc().MipLevels)
- {
- device->CopyTexture2D_Region(atlasTexture, mip, it.second.x >> mip, it.second.y >> mip, it.first, mip, threadID);
- }
- }
+ CopyTexture2D_Region(atlasTexture, 0, it.second.x, it.second.y, it.first, 0, threadID);
}
}
else
@@ -7225,14 +7275,18 @@ void wiRenderer::ManageDecalAtlas(GRAPHICSTHREAD threadID)
desc.Height = (UINT)bins[0].size.h;
desc.MipLevels = 6;
desc.ArraySize = 1;
- desc.Format = FORMAT_B8G8R8A8_UNORM; // png decals are loaded into this format! todo: DXT!
+ //desc.Format = FORMAT_R8G8B8A8_UNORM;
+ desc.Format = FORMAT_B8G8R8A8_UNORM; // png decals are loaded into this format! todo: Utility copytexture2d srcMIP!!!
desc.SampleDesc.Count = 1;
desc.SampleDesc.Quality = 0;
desc.Usage = USAGE_DEFAULT;
- desc.BindFlags = BIND_SHADER_RESOURCE;
+ desc.BindFlags = BIND_SHADER_RESOURCE /*| BIND_UNORDERED_ACCESS*/;
desc.CPUAccessFlags = 0;
desc.MiscFlags = 0;
+ atlasTexture = new Texture2D;
+ atlasTexture->RequestIndependentUnorderedAccessResourcesForMIPs(true);
+
device->CreateTexture2D(&desc, nullptr, &atlasTexture);
for (UINT mip = 0; mip < atlasTexture->GetDesc().MipLevels; ++mip)
@@ -7242,6 +7296,7 @@ void wiRenderer::ManageDecalAtlas(GRAPHICSTHREAD threadID)
if (mip < it.first->GetDesc().MipLevels)
{
device->CopyTexture2D_Region(atlasTexture, mip, it.second.x >> mip, it.second.y >> mip, it.first, mip, threadID);
+ //CopyTexture2D_Region(atlasTexture, mip, it.second.x >> mip, it.second.y >> mip, it.first, mip, threadID);
}
}
}
diff --git a/WickedEngine/wiRenderer.h b/WickedEngine/wiRenderer.h
index 64597f5be..a8b18dd76 100644
--- a/WickedEngine/wiRenderer.h
+++ b/WickedEngine/wiRenderer.h
@@ -512,6 +512,9 @@ public:
static void GenerateMipChain(wiGraphicsTypes::Texture2D* texture, MIPGENFILTER filter, GRAPHICSTHREAD threadID);
static void GenerateMipChain(wiGraphicsTypes::Texture3D* texture, MIPGENFILTER filter, GRAPHICSTHREAD threadID);
+ // Performs copy operation even between different texture formats
+ static void CopyTexture2D_Region(wiGraphicsTypes::Texture2D* dst, UINT DstMIP, UINT DstX, UINT DstY, wiGraphicsTypes::Texture2D* src, UINT SrcMIP, GRAPHICSTHREAD threadID);
+
// dst: Texture2D with unordered access, the output will be written to this
// refinementCount: 0: auto select, 1: perfect noise, greater numbers: smoother clouds, slower processing
// randomness: random seed
diff --git a/WickedEngine/wiTextureHelper.cpp b/WickedEngine/wiTextureHelper.cpp
index a2c5933aa..c7cf225e4 100644
--- a/WickedEngine/wiTextureHelper.cpp
+++ b/WickedEngine/wiTextureHelper.cpp
@@ -186,14 +186,14 @@ Texture2D* wiTextureHelper::wiTextureHelperInstance::getColor(const wiColor& col
unsigned char* data = new unsigned char[dataLength];
for (int i = 0; i < dataLength; i += 4)
{
- data[i] = color.b;
+ data[i] = color.r;
data[i + 1] = color.g;
- data[i + 2] = color.r;
+ data[i + 2] = color.b;
data[i + 3] = color.a;
}
Texture2D* texture = nullptr;
- if (FAILED(CreateTexture(texture, data, 1, 1, 4, FORMAT_B8G8R8A8_UNORM)))
+ if (FAILED(CreateTexture(texture, data, 1, 1, 4)))
{
delete[] data;
return nullptr;