From e1aaab09a33ac3a672083e3a8edef856de4f636b Mon Sep 17 00:00:00 2001 From: turanszkij Date: Tue, 25 Sep 2018 17:56:07 +0100 Subject: [PATCH] optimize, refactor --- Editor/RendererWindow.cpp | 1 + WickedEngine/ConstantBufferMapping.h | 1 - WickedEngine/ShaderInterop_Renderer.h | 105 +- .../WickedEngine_SHARED.vcxitems.filters | 16 +- WickedEngine/cullingShaderHF.hlsli | 2 +- WickedEngine/emittedparticle_simulateCS.hlsl | 2 +- WickedEngine/environmentalLightPS.hlsl | 2 +- WickedEngine/fogHF.hlsli | 2 +- WickedEngine/globals.hlsli | 22 +- WickedEngine/lightCullingCS.hlsl | 6 +- WickedEngine/lightingHF.hlsli | 26 +- WickedEngine/objectGS_voxelizer.hlsl | 4 +- WickedEngine/objectHF.hlsli | 29 +- WickedEngine/objectPS_voxelizer.hlsl | 6 +- WickedEngine/raytrace_launchCS.hlsl | 2 +- WickedEngine/raytrace_lightsamplingCS.hlsl | 2 +- WickedEngine/raytrace_primaryCS.hlsl | 2 +- WickedEngine/skyHF.hlsli | 6 +- WickedEngine/stereogramPS.hlsl | 2 +- WickedEngine/temporalAAResolvePS.hlsl | 2 +- WickedEngine/voxelConeTracingHF.hlsli | 22 +- WickedEngine/voxelGS.hlsl | 6 +- .../voxelRadianceSecondaryBounceCS.hlsl | 10 +- WickedEngine/voxelSceneCopyClearCS.hlsl | 2 +- WickedEngine/voxelVS.hlsl | 8 +- WickedEngine/wiAllocator.h | 6 + WickedEngine/wiEnums.h | 1 - WickedEngine/wiImage.cpp | 4 +- WickedEngine/wiInitializer.cpp | 2 +- WickedEngine/wiPhysicsEngine.h | 2 +- WickedEngine/wiPhysicsEngine_Bullet.cpp | 37 +- WickedEngine/wiRenderer.cpp | 1611 +++++++---------- WickedEngine/wiRenderer.h | 1 - WickedEngine/wiResourceManager.cpp | 5 +- WickedEngine/wiSceneSystem.cpp | 5 +- 35 files changed, 852 insertions(+), 1110 deletions(-) diff --git a/Editor/RendererWindow.cpp b/Editor/RendererWindow.cpp index 791872e64..625901ec6 100644 --- a/Editor/RendererWindow.cpp +++ b/Editor/RendererWindow.cpp @@ -410,6 +410,7 @@ RendererWindow::RendererWindow(wiGUI* gui, Renderable3DComponent* component) : G wiRenderer::SetToDrawDebugPartitionTree(args.bValue); }); partitionBoxesCheckBox->SetCheck(wiRenderer::GetToDrawDebugPartitionTree()); + partitionBoxesCheckBox->SetEnabled(false); // SP tree is not implemented at the moment anymore rendererWindow->AddWidget(partitionBoxesCheckBox); boneLinesCheckBox = new wiCheckBox("Bone line visualizer: "); diff --git a/WickedEngine/ConstantBufferMapping.h b/WickedEngine/ConstantBufferMapping.h index e8bf583d5..86b52e64b 100644 --- a/WickedEngine/ConstantBufferMapping.h +++ b/WickedEngine/ConstantBufferMapping.h @@ -6,7 +6,6 @@ // Persistent buffers: // These are bound once and are alive forever -#define CBSLOT_RENDERER_WORLD 0 #define CBSLOT_RENDERER_FRAME 1 #define CBSLOT_RENDERER_CAMERA 2 #define CBSLOT_RENDERER_MISC 3 diff --git a/WickedEngine/ShaderInterop_Renderer.h b/WickedEngine/ShaderInterop_Renderer.h index fe466126b..0b18fdaad 100644 --- a/WickedEngine/ShaderInterop_Renderer.h +++ b/WickedEngine/ShaderInterop_Renderer.h @@ -4,54 +4,51 @@ // ---------- Persistent: ----------------- -CBUFFER(WorldCB, CBSLOT_RENDERER_WORLD) -{ - float2 g_xWorld_ScreenWidthHeight; - float2 g_xWorld_ScreenWidthHeight_Inverse; - - float2 g_xWorld_InternalResolution; - float2 g_xWorld_InternalResolution_Inverse; - - float g_xWorld_Gamma; - float3 g_xWorld_SunColor; - - float3 g_xWorld_SunDirection; float pad0_WorldCB; - - float3 g_xWorld_Horizon; float pad1_WorldCB; - - float3 g_xWorld_Zenith; - float g_xWorld_CloudScale; - - float3 g_xWorld_Ambient; - float g_xWorld_Cloudiness; - - float3 g_xWorld_Fog; // Fog Start,End,Height - float g_xWorld_SpecularAA; - - float g_xWorld_VoxelRadianceDataSize; // voxel half-extent in world space units - float g_xWorld_VoxelRadianceDataSize_Inverse; // 1.0 / voxel-half extent - uint g_xWorld_VoxelRadianceDataRes; // voxel grid resolution - float g_xWorld_VoxelRadianceDataRes_Inverse; // 1.0 / voxel grid resolution - - uint g_xWorld_VoxelRadianceDataMIPs; // voxel grid mipmap count - uint g_xWorld_VoxelRadianceNumCones; // number of diffuse cones to trace - float g_xWorld_VoxelRadianceNumCones_Inverse; // 1.0 / number of diffuse cones to trace - float g_xWorld_VoxelRadianceRayStepSize; // raymarch step size in voxel space units - - uint g_xWorld_VoxelRadianceReflectionsEnabled; // are voxel gi reflections enabled or not - float3 g_xWorld_VoxelRadianceDataCenter; // center of the voxel grid in world space units - - uint g_xWorld_AdvancedRefractions; - uint3 g_xWorld_EntityCullingTileCount; - - uint g_xWorld_TransparentShadowsEnabled; - int g_xWorld_GlobalEnvProbeIndex; - uint g_xWorld_EnvProbeMipCount; - float g_xWorld_EnvProbeMipCount_Inverse; -}; - CBUFFER(FrameCB, CBSLOT_RENDERER_FRAME) { + float2 g_xFrame_ScreenWidthHeight; + float2 g_xFrame_ScreenWidthHeight_Inverse; + + float2 g_xFrame_InternalResolution; + float2 g_xFrame_InternalResolution_Inverse; + + float g_xFrame_Gamma; + float3 g_xFrame_SunColor; + + float3 g_xFrame_SunDirection; float pad0_WorldCB; + + float3 g_xFrame_Horizon; float pad1_WorldCB; + + float3 g_xFrame_Zenith; + float g_xFrame_CloudScale; + + float3 g_xFrame_Ambient; + float g_xFrame_Cloudiness; + + float3 g_xFrame_Fog; // Fog Start,End,Height + float g_xFrame_SpecularAA; + + float g_xFrame_VoxelRadianceDataSize; // voxel half-extent in world space units + float g_xFrame_VoxelRadianceDataSize_Inverse; // 1.0 / voxel-half extent + uint g_xFrame_VoxelRadianceDataRes; // voxel grid resolution + float g_xFrame_VoxelRadianceDataRes_Inverse; // 1.0 / voxel grid resolution + + uint g_xFrame_VoxelRadianceDataMIPs; // voxel grid mipmap count + uint g_xFrame_VoxelRadianceNumCones; // number of diffuse cones to trace + float g_xFrame_VoxelRadianceNumCones_Inverse; // 1.0 / number of diffuse cones to trace + float g_xFrame_VoxelRadianceRayStepSize; // raymarch step size in voxel space units + + uint g_xFrame_VoxelRadianceReflectionsEnabled; // are voxel gi reflections enabled or not + float3 g_xFrame_VoxelRadianceDataCenter; // center of the voxel grid in world space units + + uint g_xFrame_AdvancedRefractions; + uint3 g_xFrame_EntityCullingTileCount; + + uint g_xFrame_TransparentShadowsEnabled; + int g_xFrame_GlobalEnvProbeIndex; + uint g_xFrame_EnvProbeMipCount; + float g_xFrame_EnvProbeMipCount_Inverse; + float g_xFrame_Time; float g_xFrame_TimePrev; float g_xFrame_DeltaTime; @@ -113,6 +110,7 @@ CBUFFER(FrameCB, CBSLOT_RENDERER_FRAME) float3 g_xFrame_WorldBoundsExtents; float pad2_frameCB; // world enclosing AABB abs(max - min) float3 g_xFrame_WorldBoundsExtents_Inverse; float pad3_frameCB; // world enclosing AABB 1.0f / abs(max - min) }; + // The following buffer contains properties for a temporary camera (eg. main camera, reflection camera, shadow camera...) CBUFFER(CameraCB, CBSLOT_RENDERER_CAMERA) { @@ -121,6 +119,21 @@ CBUFFER(CameraCB, CBSLOT_RENDERER_CAMERA) float4x4 g_xCamera_Proj; float3 g_xCamera_CamPos; float xPadding0_Camera_CommonCB; }; + +CBUFFER(MaterialCB, CBSLOT_RENDERER_MATERIAL) +{ + float4 g_xMat_baseColor; + float4 g_xMat_texMulAdd; + float g_xMat_roughness; + float g_xMat_reflectance; + float g_xMat_metalness; + float g_xMat_emissive; + float g_xMat_refractionIndex; + float g_xMat_subsurfaceScattering; + float g_xMat_normalMapStrength; + float g_xMat_parallaxOcclusionMapping; +}; + CBUFFER(MiscCB, CBSLOT_RENDERER_MISC) { float4x4 g_xTransform; diff --git a/WickedEngine/WickedEngine_SHARED.vcxitems.filters b/WickedEngine/WickedEngine_SHARED.vcxitems.filters index 6431b0e01..875d14be8 100644 --- a/WickedEngine/WickedEngine_SHARED.vcxitems.filters +++ b/WickedEngine/WickedEngine_SHARED.vcxitems.filters @@ -1128,9 +1128,6 @@ ENGINE\Graphics\GPUMapping - - ENGINE\Scripting\LuaBindings - ENGINE\Graphics\GPUMapping @@ -1140,9 +1137,12 @@ ENGINE\Graphics\GPUMapping - + ENGINE\Physics + + ENGINE\Scripting\LuaBindings + @@ -1928,12 +1928,12 @@ ENGINE\System - - ENGINE\Scripting\LuaBindings - - + ENGINE\Physics + + ENGINE\Scripting\LuaBindings + diff --git a/WickedEngine/cullingShaderHF.hlsli b/WickedEngine/cullingShaderHF.hlsli index dc0cc4ebf..311e71641 100644 --- a/WickedEngine/cullingShaderHF.hlsli +++ b/WickedEngine/cullingShaderHF.hlsli @@ -50,7 +50,7 @@ float4 ClipToView(float4 clip) float4 ScreenToView(float4 screen) { // Convert to normalized texture coordinates - float2 texCoord = screen.xy * g_xWorld_InternalResolution_Inverse; + float2 texCoord = screen.xy * g_xFrame_InternalResolution_Inverse; // Convert to clip space float4 clip = float4(float2(texCoord.x, 1.0f - texCoord.y) * 2.0f - 1.0f, screen.z, screen.w); diff --git a/WickedEngine/emittedparticle_simulateCS.hlsl b/WickedEngine/emittedparticle_simulateCS.hlsl index dd62f61a8..7670fc923 100644 --- a/WickedEngine/emittedparticle_simulateCS.hlsl +++ b/WickedEngine/emittedparticle_simulateCS.hlsl @@ -89,7 +89,7 @@ void main(uint3 DTid : SV_DispatchThreadID, uint Gid : SV_GroupIndex) if (pos2D.x > -1 && pos2D.x < 1 && pos2D.y > -1 && pos2D.y < 1) { float2 uv = pos2D.xy * float2(0.5f, -0.5f) + 0.5f; - uint2 pixel = uv * g_xWorld_InternalResolution; + uint2 pixel = uv * g_xFrame_InternalResolution; float depth0 = texture_depth[pixel + uint2(0, 0)]; float surfaceLinearDepth = getLinearDepth(depth0); diff --git a/WickedEngine/environmentalLightPS.hlsl b/WickedEngine/environmentalLightPS.hlsl index d0ce549f6..28bf4f36b 100644 --- a/WickedEngine/environmentalLightPS.hlsl +++ b/WickedEngine/environmentalLightPS.hlsl @@ -8,7 +8,7 @@ LightOutputType main(VertexToPixel PSIn) DEFERREDLIGHT_MAKEPARAMS diffuse = 0; - float envMapMIP = roughness * g_xWorld_EnvProbeMipCount; + float envMapMIP = roughness * g_xFrame_EnvProbeMipCount; specular = max(0, EnvironmentReflection_Global(surface, envMapMIP)); VoxelGI(surface, diffuse, specular, ao); diff --git a/WickedEngine/fogHF.hlsli b/WickedEngine/fogHF.hlsli index 562d84587..95365337b 100644 --- a/WickedEngine/fogHF.hlsli +++ b/WickedEngine/fogHF.hlsli @@ -4,7 +4,7 @@ inline float GetFog(float dist) { - return saturate((dist - g_xWorld_Fog.x) / (g_xWorld_Fog.y - g_xWorld_Fog.x)); + return saturate((dist - g_xFrame_Fog.x) / (g_xFrame_Fog.y - g_xFrame_Fog.x)); } #endif // _FOGHF_ diff --git a/WickedEngine/globals.hlsli b/WickedEngine/globals.hlsli index e3e5da95e..86cc6378d 100644 --- a/WickedEngine/globals.hlsli +++ b/WickedEngine/globals.hlsli @@ -56,19 +56,19 @@ static const float SQRT2 = 1.41421356237309504880; #define ALPHATEST(x) clip((x) - (1.0f - g_xAlphaRef)); #endif -#define DEGAMMA(x) pow(abs(x),g_xWorld_Gamma) -#define GAMMA(x) pow(abs(x),1.0/g_xWorld_Gamma) +#define DEGAMMA(x) pow(abs(x),g_xFrame_Gamma) +#define GAMMA(x) pow(abs(x),1.0/g_xFrame_Gamma) -inline float3 GetSunColor() { return g_xWorld_SunColor; } -inline float3 GetSunDirection() { return g_xWorld_SunDirection; } -inline float3 GetHorizonColor() { return g_xWorld_Horizon.rgb; } -inline float3 GetZenithColor() { return g_xWorld_Zenith.rgb; } -inline float3 GetAmbientColor() { return g_xWorld_Ambient.rgb; } +inline float3 GetSunColor() { return g_xFrame_SunColor; } +inline float3 GetSunDirection() { return g_xFrame_SunDirection; } +inline float3 GetHorizonColor() { return g_xFrame_Horizon.rgb; } +inline float3 GetZenithColor() { return g_xFrame_Zenith.rgb; } +inline float3 GetAmbientColor() { return g_xFrame_Ambient.rgb; } inline float3 GetAmbient(in float3 N) { return lerp(GetHorizonColor(), GetZenithColor(), saturate(N.y * 0.5f + 0.5f)) + GetAmbientColor(); } -inline float2 GetScreenResolution() { return g_xWorld_ScreenWidthHeight; } -inline float GetScreenWidth() { return g_xWorld_ScreenWidthHeight.x; } -inline float GetScreenHeight() { return g_xWorld_ScreenWidthHeight.y; } -inline float2 GetInternalResolution() { return g_xWorld_InternalResolution; } +inline float2 GetScreenResolution() { return g_xFrame_ScreenWidthHeight; } +inline float GetScreenWidth() { return g_xFrame_ScreenWidthHeight.x; } +inline float GetScreenHeight() { return g_xFrame_ScreenWidthHeight.y; } +inline float2 GetInternalResolution() { return g_xFrame_InternalResolution; } inline float GetTime() { return g_xFrame_Time; } inline float GetEmissive(float emissive) { return emissive * 10.0f; } inline uint2 GetTemporalAASampleRotation() { return float2((g_xFrame_TemporalAASampleRotation >> 0) & 0x000000FF, (g_xFrame_TemporalAASampleRotation >> 8) & 0x000000FF); } diff --git a/WickedEngine/lightCullingCS.hlsl b/WickedEngine/lightCullingCS.hlsl index cca92165b..dd8da37a2 100644 --- a/WickedEngine/lightCullingCS.hlsl +++ b/WickedEngine/lightCullingCS.hlsl @@ -463,7 +463,7 @@ void main(ComputeShaderInput IN) float reflectance = g3.y; float metalness = g3.z; float ao = g3.w; - float3 P = getPosition((float2)pixel * g_xWorld_InternalResolution_Inverse, depth); + float3 P = getPosition((float2)pixel * g_xFrame_InternalResolution_Inverse, depth); float3 V = normalize(g_xFrame_MainCamera_CamPos - P); Surface surface = CreateSurface(P, N, V, baseColor, roughness, reflectance, metalness); @@ -476,7 +476,7 @@ void main(ComputeShaderInput IN) // Apply environment maps: float4 envmapAccumulation = 0; - float envMapMIP = surface.roughness * g_xWorld_EnvProbeMipCount; + float envMapMIP = surface.roughness * g_xFrame_EnvProbeMipCount; #ifdef DISABLE_LOCALENVPMAPS // local envmaps are disabled, set iterator to skip: @@ -572,7 +572,7 @@ void main(ComputeShaderInput IN) VoxelGI(surface, diffuse, reflection, ao); - float2 ScreenCoord = (float2)pixel * g_xWorld_ScreenWidthHeight_Inverse; + float2 ScreenCoord = (float2)pixel * g_xFrame_ScreenWidthHeight_Inverse; float2 velocity = g1.zw; float2 ReprojectedScreenCoord = ScreenCoord + velocity; float4 ssr = xSSR.SampleLevel(sampler_linear_clamp, ReprojectedScreenCoord, 0); diff --git a/WickedEngine/lightingHF.hlsli b/WickedEngine/lightingHF.hlsli index 2c72b3a3f..3bc6b02a9 100644 --- a/WickedEngine/lightingHF.hlsli +++ b/WickedEngine/lightingHF.hlsli @@ -33,7 +33,7 @@ inline float3 shadowCascade(float4 shadowPos, float2 ShTex, float shadowKernel, #endif #ifndef DISABLE_TRANSPARENT_SHADOWMAP - if (g_xWorld_TransparentShadowsEnabled) + if (g_xFrame_TransparentShadowsEnabled) { // unfortunately transparents will not receive transparent shadow map // because we cannot distinguish without using secondary depth buffer for transparents @@ -399,7 +399,7 @@ inline LightingResult SphereLight(in ShaderEntityType light, in Surface surface) #ifndef DISABLE_SHADOWMAPS [branch] if (light.additionalData_index >= 0) { - fLight *= texture_shadowarray_cube.SampleCmpLevelZero(sampler_cmp_depth, float4(-L, light.additionalData_index), 1 - dist / (light.GetRadius() * 100) * (1 - light.shadowBias)).r; + fLight *= texture_shadowarray_cube.SampleCmpLevelZero(sampler_cmp_depth, float4(-L, light.additionalData_index), 1 - dist / light.range * (1 - light.shadowBias)).r; } #endif @@ -450,7 +450,7 @@ inline LightingResult DiscLight(in ShaderEntityType light, in Surface surface) #ifndef DISABLE_SHADOWMAPS [branch] if (light.additionalData_index >= 0) { - fLight *= texture_shadowarray_cube.SampleCmpLevelZero(sampler_cmp_depth, float4(-L, light.additionalData_index), 1 - dist / (light.GetRadius() * 100) * (1 - light.shadowBias)).r; + fLight *= texture_shadowarray_cube.SampleCmpLevelZero(sampler_cmp_depth, float4(-L, light.additionalData_index), 1 - dist / light.range * (1 - light.shadowBias)).r; } #endif @@ -521,7 +521,7 @@ inline LightingResult RectangleLight(in ShaderEntityType light, in Surface surfa #ifndef DISABLE_SHADOWMAPS [branch] if (light.additionalData_index >= 0) { - fLight *= texture_shadowarray_cube.SampleCmpLevelZero(sampler_cmp_depth, float4(-L, light.additionalData_index), 1 - dist / (max(light.GetWidth(),light.GetHeight()) * 100) * (1 - light.shadowBias)).r; + fLight *= texture_shadowarray_cube.SampleCmpLevelZero(sampler_cmp_depth, float4(-L, light.additionalData_index), 1 - dist / light.range * (1 - light.shadowBias)).r; } #endif @@ -645,7 +645,7 @@ inline LightingResult TubeLight(in ShaderEntityType light, in Surface surface) #ifndef DISABLE_SHADOWMAPS [branch] if (light.additionalData_index >= 0) { - fLight *= texture_shadowarray_cube.SampleCmpLevelZero(sampler_cmp_depth, float4(-L, light.additionalData_index), 1 - dist / (max(light.GetRadius(),light.GetWidth())*100) * (1 - light.shadowBias)).r; + fLight *= texture_shadowarray_cube.SampleCmpLevelZero(sampler_cmp_depth, float4(-L, light.additionalData_index), 1 - dist / light.range * (1 - light.shadowBias)).r; } #endif @@ -689,12 +689,12 @@ inline LightingResult TubeLight(in ShaderEntityType light, in Surface surface) inline void VoxelGI(in Surface surface, inout float3 diffuse, inout float3 specular, inout float ao) { - [branch]if (g_xWorld_VoxelRadianceDataRes != 0) + [branch]if (g_xFrame_VoxelRadianceDataRes != 0) { // determine blending factor (we will blend out voxel GI on grid edges): - float3 voxelSpacePos = surface.P - g_xWorld_VoxelRadianceDataCenter; - voxelSpacePos *= g_xWorld_VoxelRadianceDataSize_Inverse; - voxelSpacePos *= g_xWorld_VoxelRadianceDataRes_Inverse; + float3 voxelSpacePos = surface.P - g_xFrame_VoxelRadianceDataCenter; + voxelSpacePos *= g_xFrame_VoxelRadianceDataSize_Inverse; + voxelSpacePos *= g_xFrame_VoxelRadianceDataRes_Inverse; voxelSpacePos = saturate(abs(voxelSpacePos)); float blend = 1 - pow(max(voxelSpacePos.x, max(voxelSpacePos.y, voxelSpacePos.z)), 4); @@ -703,7 +703,7 @@ inline void VoxelGI(in Surface surface, inout float3 diffuse, inout float3 specu ao *= 1 - lerp(0, radiance.a, blend); [branch] - if (g_xWorld_VoxelRadianceReflectionsEnabled) + if (g_xFrame_VoxelRadianceReflectionsEnabled) { float4 reflection = ConeTraceReflection(texture_voxelradiance, surface.P, surface.N, surface.V, surface.roughness); specular = lerp(specular, reflection.rgb, reflection.a * blend); @@ -724,10 +724,10 @@ inline float3 EnvironmentReflection_Global(in Surface surface, in float MIP) #ifndef ENVMAPRENDERING [branch] - if (g_xWorld_GlobalEnvProbeIndex >= 0) + if (g_xFrame_GlobalEnvProbeIndex >= 0) { // We have envmap information in a texture: - envColor = texture_envmaparray.SampleLevel(sampler_linear_clamp, float4(surface.R, g_xWorld_GlobalEnvProbeIndex), MIP).rgb; + envColor = texture_envmaparray.SampleLevel(sampler_linear_clamp, float4(surface.R, g_xFrame_GlobalEnvProbeIndex), MIP).rgb; } else #endif // ENVMAPRENDERING @@ -735,7 +735,7 @@ inline float3 EnvironmentReflection_Global(in Surface surface, in float MIP) // There are no envmaps, approximate sky color: float3 realSkyColor = lerp(GetHorizonColor(), GetZenithColor(), pow(saturate(surface.R.y), 0.25f)); float3 roughSkyColor = (GetHorizonColor() + GetZenithColor()) * 0.5f; - float blendSkyByRoughness = saturate(MIP * g_xWorld_EnvProbeMipCount_Inverse); + float blendSkyByRoughness = saturate(MIP * g_xFrame_EnvProbeMipCount_Inverse); envColor = lerp(realSkyColor, roughSkyColor, blendSkyByRoughness); } diff --git a/WickedEngine/objectGS_voxelizer.hlsl b/WickedEngine/objectGS_voxelizer.hlsl index fb67faca3..80558c4f2 100644 --- a/WickedEngine/objectGS_voxelizer.hlsl +++ b/WickedEngine/objectGS_voxelizer.hlsl @@ -33,7 +33,7 @@ void main( for (uint i = 0; i < 3; ++i) { // World space -> Voxel grid space: - output[i].pos.xyz = (input[i].pos.xyz - g_xWorld_VoxelRadianceDataCenter) * g_xWorld_VoxelRadianceDataSize_Inverse; + output[i].pos.xyz = (input[i].pos.xyz - g_xFrame_VoxelRadianceDataCenter) * g_xFrame_VoxelRadianceDataSize_Inverse; // Project onto dominant axis: [flatten] @@ -61,7 +61,7 @@ void main( for (uint j = 0; j < 3; j++) { // Voxel grid space -> Clip space - output[j].pos.xy *= g_xWorld_VoxelRadianceDataRes_Inverse; + output[j].pos.xy *= g_xFrame_VoxelRadianceDataRes_Inverse; output[j].pos.zw = 1; // Append the rest of the parameters as is: diff --git a/WickedEngine/objectHF.hlsli b/WickedEngine/objectHF.hlsli index a45c98f4f..ac373f6e2 100644 --- a/WickedEngine/objectHF.hlsli +++ b/WickedEngine/objectHF.hlsli @@ -26,23 +26,6 @@ #include "packHF.hlsli" #include "lightingHF.hlsli" -// UNIFORMS -////////////////// - -CBUFFER(MaterialCB, CBSLOT_RENDERER_MATERIAL) -{ - float4 g_xMat_baseColor; - float4 g_xMat_texMulAdd; - float g_xMat_roughness; - float g_xMat_reflectance; - float g_xMat_metalness; - float g_xMat_emissive; - float g_xMat_refractionIndex; - float g_xMat_subsurfaceScattering; - float g_xMat_normalMapStrength; - float g_xMat_parallaxOcclusionMapping; -}; - // DEFINITIONS ////////////////// @@ -129,11 +112,11 @@ inline void NormalMapping(in float2 UV, in float3 V, inout float3 N, in float3x3 inline void SpecularAA(in float3 N, inout float roughness) { [branch] - if (g_xWorld_SpecularAA > 0) + if (g_xFrame_SpecularAA > 0) { float3 ddxN = ddx_coarse(N); float3 ddyN = ddy_coarse(N); - float curve = pow(max(dot(ddxN, ddxN), dot(ddyN, ddyN)), 1 - g_xWorld_SpecularAA); + float curve = pow(max(dot(ddxN, ddxN), dot(ddyN, ddyN)), 1 - g_xFrame_SpecularAA); roughness = max(roughness, curve); } } @@ -177,7 +160,7 @@ inline void Refraction(in float2 ScreenCoord, in float2 normal2D, in float3 bump float mipLevels; xRefraction.GetDimensions(0, size.x, size.y, mipLevels); float2 perturbatedRefrTexCoords = ScreenCoord.xy + (normal2D + bumpColor.rg) * g_xMat_refractionIndex; - float4 refractiveColor = xRefraction.SampleLevel(sampler_linear_clamp, perturbatedRefrTexCoords, (g_xWorld_AdvancedRefractions ? surface.roughness * mipLevels : 0)); + float4 refractiveColor = xRefraction.SampleLevel(sampler_linear_clamp, perturbatedRefrTexCoords, (g_xFrame_AdvancedRefractions ? surface.roughness * mipLevels : 0)); surface.albedo.rgb *= lerp(refractiveColor.rgb, 1, color.a); color.a = 1; } @@ -192,7 +175,7 @@ inline void ForwardLighting(inout Surface surface, inout float3 diffuse, out flo specular += surface.baseColor.rgb * GetEmissive(surface.emissive); #ifndef DISABLE_ENVMAPS - float envMapMIP = surface.roughness * g_xWorld_EnvProbeMipCount; + float envMapMIP = surface.roughness * g_xFrame_EnvProbeMipCount; reflection = max(0, EnvironmentReflection_Global(surface, envMapMIP)); #endif // DISABLE_ENVMAPS @@ -251,7 +234,7 @@ inline void ForwardLighting(inout Surface surface, inout float3 diffuse, out flo inline void TiledLighting(in float2 pixel, inout Surface surface, inout float3 diffuse, out float3 specular, out float3 reflection) { uint2 tileIndex = uint2(floor(pixel / TILED_CULLING_BLOCKSIZE)); - uint startOffset = flatten2D(tileIndex, g_xWorld_EntityCullingTileCount.xy) * MAX_SHADER_ENTITY_COUNT_PER_TILE; + uint startOffset = flatten2D(tileIndex, g_xFrame_EntityCullingTileCount.xy) * MAX_SHADER_ENTITY_COUNT_PER_TILE; uint arrayProperties = EntityIndexList[startOffset]; uint arrayLength = arrayProperties & 0x000FFFFF; // count of every element in the tile uint decalCount = (arrayProperties & 0xFF000000) >> 24; // count of just the decals in the tile @@ -312,7 +295,7 @@ inline void TiledLighting(in float2 pixel, inout Surface surface, inout float3 d // Apply environment maps: float4 envmapAccumulation = 0; - float envMapMIP = surface.roughness * g_xWorld_EnvProbeMipCount; + float envMapMIP = surface.roughness * g_xFrame_EnvProbeMipCount; #ifdef DISABLE_LOCALENVPMAPS // local envmaps are disabled, set iterator to skip: diff --git a/WickedEngine/objectPS_voxelizer.hlsl b/WickedEngine/objectPS_voxelizer.hlsl index 63599eaaa..6b7994582 100644 --- a/WickedEngine/objectPS_voxelizer.hlsl +++ b/WickedEngine/objectPS_voxelizer.hlsl @@ -5,7 +5,7 @@ RWSTRUCTUREDBUFFER(output, VoxelType, 0); void main(float4 pos : SV_POSITION, float3 N : NORMAL, float2 tex : TEXCOORD, float3 P : POSITION3D, nointerpolation float3 instanceColor : COLOR) { - float3 diff = (P - g_xWorld_VoxelRadianceDataCenter) * g_xWorld_VoxelRadianceDataRes_Inverse * g_xWorld_VoxelRadianceDataSize_Inverse; + float3 diff = (P - g_xFrame_VoxelRadianceDataCenter) * g_xFrame_VoxelRadianceDataRes_Inverse * g_xFrame_VoxelRadianceDataSize_Inverse; float3 uvw = diff * float3(0.5f, -0.5f, 0.5f) + 0.5f; [branch] @@ -128,8 +128,8 @@ void main(float4 pos : SV_POSITION, float3 N : NORMAL, float2 tex : TEXCOORD, fl uint normal_encoded = EncodeNormal(N); // output: - uint3 writecoord = floor(uvw * g_xWorld_VoxelRadianceDataRes); - uint id = flatten3D(writecoord, g_xWorld_VoxelRadianceDataRes); + uint3 writecoord = floor(uvw * g_xFrame_VoxelRadianceDataRes); + uint id = flatten3D(writecoord, g_xFrame_VoxelRadianceDataRes); InterlockedMax(output[id].colorMask, color_encoded); InterlockedMax(output[id].normalMask, normal_encoded); } diff --git a/WickedEngine/raytrace_launchCS.hlsl b/WickedEngine/raytrace_launchCS.hlsl index 9a657d709..ec9c483e3 100644 --- a/WickedEngine/raytrace_launchCS.hlsl +++ b/WickedEngine/raytrace_launchCS.hlsl @@ -10,7 +10,7 @@ void main( uint3 DTid : SV_DispatchThreadID ) if (DTid.x < (uint)GetInternalResolution().x && DTid.y < (uint)GetInternalResolution().y) { // Compute screen coordinates: - float2 uv = float2((DTid.xy + xTracePixelOffset) * g_xWorld_InternalResolution_Inverse * 2.0f - 1.0f) * float2(1, -1); + float2 uv = float2((DTid.xy + xTracePixelOffset) * g_xFrame_InternalResolution_Inverse * 2.0f - 1.0f) * float2(1, -1); // Target pixel: uint pixelID = flatten2D(DTid.xy, GetInternalResolution()); diff --git a/WickedEngine/raytrace_lightsamplingCS.hlsl b/WickedEngine/raytrace_lightsamplingCS.hlsl index 1b0f83dca..9b242f268 100644 --- a/WickedEngine/raytrace_lightsamplingCS.hlsl +++ b/WickedEngine/raytrace_lightsamplingCS.hlsl @@ -125,7 +125,7 @@ void main( uint3 DTid : SV_DispatchThreadID, uint groupIndex : SV_GroupIndex) uint2 coords2D = unflatten2D(pixelID, GetInternalResolution()); // Compute screen coordinates: - float2 uv = float2((coords2D + xTracePixelOffset) * g_xWorld_InternalResolution_Inverse * 2.0f - 1.0f) * float2(1, -1); + float2 uv = float2((coords2D + xTracePixelOffset) * g_xFrame_InternalResolution_Inverse * 2.0f - 1.0f) * float2(1, -1); float seed = xTraceRandomSeed; diff --git a/WickedEngine/raytrace_primaryCS.hlsl b/WickedEngine/raytrace_primaryCS.hlsl index 5421acd0d..0aa25fadd 100644 --- a/WickedEngine/raytrace_primaryCS.hlsl +++ b/WickedEngine/raytrace_primaryCS.hlsl @@ -227,7 +227,7 @@ void main( uint3 DTid : SV_DispatchThreadID, uint groupIndex : SV_GroupIndex ) uint2 coords2D = unflatten2D(pixelID, GetInternalResolution()); // Compute screen coordinates: - float2 uv = float2((coords2D + xTracePixelOffset) * g_xWorld_InternalResolution_Inverse * 2.0f - 1.0f) * float2(1, -1); + float2 uv = float2((coords2D + xTracePixelOffset) * g_xFrame_InternalResolution_Inverse * 2.0f - 1.0f) * float2(1, -1); float seed = xTraceRandomSeed; diff --git a/WickedEngine/skyHF.hlsli b/WickedEngine/skyHF.hlsli index c97851af4..21338b79a 100644 --- a/WickedEngine/skyHF.hlsli +++ b/WickedEngine/skyHF.hlsli @@ -5,7 +5,7 @@ float3 GetDynamicSkyColor(in float3 normal) { - float aboveHorizon = saturate(pow(saturate(normal.y), 0.25f + g_xWorld_Fog.z) / (g_xWorld_Fog.z + 1)); + float aboveHorizon = saturate(pow(saturate(normal.y), 0.25f + g_xFrame_Fog.z) / (g_xFrame_Fog.z + 1)); float3 sky = lerp(GetHorizonColor(), GetZenithColor(), aboveHorizon); #ifdef NOSUN @@ -45,10 +45,10 @@ void AddCloudLayer(inout float4 color, in float3 normal, bool dark) float3 cloudPos = o + d * t; float2 cloudUV = planeOrigin.xz - cloudPos.xz; - cloudUV *= g_xWorld_CloudScale; + cloudUV *= g_xFrame_CloudScale; float clouds1 = texture_0.SampleLevel(sampler_linear_mirror, cloudUV, 0).r; - clouds1 = saturate(clouds1 - (1 - g_xWorld_Cloudiness)) /** pow(saturate(normal.y), 0.5)*/; + clouds1 = saturate(clouds1 - (1 - g_xFrame_Cloudiness)) /** pow(saturate(normal.y), 0.5)*/; float clouds2 = texture_0.SampleLevel(sampler_linear_clamp, normal.xz * 0.5 + 0.5, 0).g; clouds2 *= pow(saturate(normal.y), 0.25); diff --git a/WickedEngine/stereogramPS.hlsl b/WickedEngine/stereogramPS.hlsl index b2cbfc828..63d961efc 100644 --- a/WickedEngine/stereogramPS.hlsl +++ b/WickedEngine/stereogramPS.hlsl @@ -14,7 +14,7 @@ float4 main(VertexToPixelPostProcess PSIn) : SV_TARGET if (uv.x < pWid) break; - float d = 1.0 - saturate(texture_lineardepth.SampleLevel(sampler_linear_clamp, uv / g_xWorld_ScreenWidthHeight.xy, 0).r * 100); + float d = 1.0 - saturate(texture_lineardepth.SampleLevel(sampler_linear_clamp, uv / g_xFrame_ScreenWidthHeight.xy, 0).r * 100); uv.x -= pWid - (d * maxStep); } diff --git a/WickedEngine/temporalAAResolvePS.hlsl b/WickedEngine/temporalAAResolvePS.hlsl index f0ed29dc9..17163c29e 100644 --- a/WickedEngine/temporalAAResolvePS.hlsl +++ b/WickedEngine/temporalAAResolvePS.hlsl @@ -42,7 +42,7 @@ float4 main(VertexToPixelPostProcess PSIn) : SV_TARGET float4 current = neighborhood[4]; // the linear filtering can cause blurry image, try to account for that: - float subpixelCorrection = frac(max(abs(velocity.x)*g_xWorld_InternalResolution.x, abs(velocity.y)*g_xWorld_InternalResolution.y)) * 0.5f; + float subpixelCorrection = frac(max(abs(velocity.x)*g_xFrame_InternalResolution.x, abs(velocity.y)*g_xFrame_InternalResolution.y)) * 0.5f; // compute a nice blend factor: float blendfactor = saturate(lerp(0.05f, 0.8f, subpixelCorrection)); diff --git a/WickedEngine/voxelConeTracingHF.hlsli b/WickedEngine/voxelConeTracingHF.hlsli index 716ab8aeb..593f1dd62 100644 --- a/WickedEngine/voxelConeTracingHF.hlsli +++ b/WickedEngine/voxelConeTracingHF.hlsli @@ -36,26 +36,26 @@ inline float4 ConeTrace(in Texture3D voxels, in float3 P, in float3 N, i // We need to offset the cone start position to avoid sampling its own voxel (self-occlusion): // Unfortunately, it will result in disconnection between nearby surfaces :( - float dist = g_xWorld_VoxelRadianceDataSize; // offset by cone dir so that first sample of all cones are not the same - float3 startPos = P + N * g_xWorld_VoxelRadianceDataSize * 2 * SQRT2; // sqrt2 is diagonal voxel half-extent + float dist = g_xFrame_VoxelRadianceDataSize; // offset by cone dir so that first sample of all cones are not the same + float3 startPos = P + N * g_xFrame_VoxelRadianceDataSize * 2 * SQRT2; // sqrt2 is diagonal voxel half-extent // We will break off the loop if the sampling distance is too far for performance reasons: - const float maxDistance = MAX_DIST * g_xWorld_VoxelRadianceDataSize; + const float maxDistance = MAX_DIST * g_xFrame_VoxelRadianceDataSize; while (dist < maxDistance && alpha < 1) { - float diameter = max(g_xWorld_VoxelRadianceDataSize, 2 * coneAperture * dist); - float mip = log2(diameter * g_xWorld_VoxelRadianceDataSize_Inverse); + float diameter = max(g_xFrame_VoxelRadianceDataSize, 2 * coneAperture * dist); + float mip = log2(diameter * g_xFrame_VoxelRadianceDataSize_Inverse); // Because we do the ray-marching in world space, we need to remap into 3d texture space before sampling: // todo: optimization could be doing ray-marching in texture space float3 tc = startPos + coneDirection * dist; - tc = (tc - g_xWorld_VoxelRadianceDataCenter) * g_xWorld_VoxelRadianceDataSize_Inverse; - tc *= g_xWorld_VoxelRadianceDataRes_Inverse; + tc = (tc - g_xFrame_VoxelRadianceDataCenter) * g_xFrame_VoxelRadianceDataSize_Inverse; + tc *= g_xFrame_VoxelRadianceDataRes_Inverse; tc = tc * float3(0.5f, -0.5f, 0.5f) + 0.5f; // break if the ray exits the voxel grid, or we sample from the last mip: - if (any(tc - saturate(tc)) || mip >= (float)g_xWorld_VoxelRadianceDataMIPs) + if (any(tc - saturate(tc)) || mip >= (float)g_xFrame_VoxelRadianceDataMIPs) break; float4 sam = voxels.SampleLevel(sampler_linear_clamp, tc, mip); @@ -66,7 +66,7 @@ inline float4 ConeTrace(in Texture3D voxels, in float3 P, in float3 N, i alpha += a * sam.a; // step along ray: - dist += diameter * g_xWorld_VoxelRadianceRayStepSize; + dist += diameter * g_xFrame_VoxelRadianceRayStepSize; } return float4(color, alpha); @@ -79,7 +79,7 @@ inline float4 ConeTraceRadiance(in Texture3D voxels, in float3 P, in flo { float4 radiance = 0; - for (uint cone = 0; cone < g_xWorld_VoxelRadianceNumCones; ++cone) // quality is between 1 and 16 cones + for (uint cone = 0; cone < g_xFrame_VoxelRadianceNumCones; ++cone) // quality is between 1 and 16 cones { // approximate a hemisphere from random points inside a sphere: // (and modulate cone with surface normal, no banding this way) @@ -91,7 +91,7 @@ inline float4 ConeTraceRadiance(in Texture3D voxels, in float3 P, in flo } // final radiance is average of all the cones radiances - radiance *= g_xWorld_VoxelRadianceNumCones_Inverse; + radiance *= g_xFrame_VoxelRadianceNumCones_Inverse; radiance.a = saturate(radiance.a); return max(0, radiance); diff --git a/WickedEngine/voxelGS.hlsl b/WickedEngine/voxelGS.hlsl index f798bb7aa..5eceedad7 100644 --- a/WickedEngine/voxelGS.hlsl +++ b/WickedEngine/voxelGS.hlsl @@ -21,11 +21,11 @@ void main( element.pos = input[0].pos; element.col = input[0].col; - element.pos.xyz = element.pos.xyz / g_xWorld_VoxelRadianceDataRes * 2 - 1; + element.pos.xyz = element.pos.xyz / g_xFrame_VoxelRadianceDataRes * 2 - 1; element.pos.y = -element.pos.y; - element.pos.xyz *= g_xWorld_VoxelRadianceDataRes; + element.pos.xyz *= g_xFrame_VoxelRadianceDataRes; element.pos.xyz += (CreateCube(i) - float3(0, 1, 0)) * 2; - element.pos.xyz *= g_xWorld_VoxelRadianceDataRes * g_xWorld_VoxelRadianceDataSize / g_xWorld_VoxelRadianceDataRes; + element.pos.xyz *= g_xFrame_VoxelRadianceDataRes * g_xFrame_VoxelRadianceDataSize / g_xFrame_VoxelRadianceDataRes; element.pos = mul(float4(element.pos.xyz, 1), g_xTransform); element.col *= g_xColor; diff --git a/WickedEngine/voxelRadianceSecondaryBounceCS.hlsl b/WickedEngine/voxelRadianceSecondaryBounceCS.hlsl index 35ca61e53..7399e7575 100644 --- a/WickedEngine/voxelRadianceSecondaryBounceCS.hlsl +++ b/WickedEngine/voxelRadianceSecondaryBounceCS.hlsl @@ -11,7 +11,7 @@ RWTEXTURE3D(output, float4, 0); [numthreads(64, 1, 1)] void main( uint3 DTid : SV_DispatchThreadID ) { - const uint3 writecoord = unflatten3D(DTid.x, g_xWorld_VoxelRadianceDataRes); + const uint3 writecoord = unflatten3D(DTid.x, g_xFrame_VoxelRadianceDataRes); float4 emission = input_emission[writecoord]; @@ -19,12 +19,12 @@ void main( uint3 DTid : SV_DispatchThreadID ) { float3 N = DecodeNormal(input_voxelscene[DTid.x].normalMask); - float3 P = ((float3)writecoord + 0.5f) * g_xWorld_VoxelRadianceDataRes_Inverse; + float3 P = ((float3)writecoord + 0.5f) * g_xFrame_VoxelRadianceDataRes_Inverse; P = P * 2 - 1; P.y *= -1; - P *= g_xWorld_VoxelRadianceDataSize; - P *= g_xWorld_VoxelRadianceDataRes; - P += g_xWorld_VoxelRadianceDataCenter; + P *= g_xFrame_VoxelRadianceDataSize; + P *= g_xFrame_VoxelRadianceDataRes; + P += g_xFrame_VoxelRadianceDataCenter; float4 radiance = ConeTraceRadiance(input_emission, P, N); diff --git a/WickedEngine/voxelSceneCopyClearCS.hlsl b/WickedEngine/voxelSceneCopyClearCS.hlsl index 847a6feee..ff6aed6a4 100644 --- a/WickedEngine/voxelSceneCopyClearCS.hlsl +++ b/WickedEngine/voxelSceneCopyClearCS.hlsl @@ -11,7 +11,7 @@ void main( uint3 DTid : SV_DispatchThreadID ) const float4 color = DecodeColor(voxel.colorMask); - const uint3 writecoord = unflatten3D(DTid.x, g_xWorld_VoxelRadianceDataRes); + const uint3 writecoord = unflatten3D(DTid.x, g_xFrame_VoxelRadianceDataRes); [branch] if (color.a > 0) diff --git a/WickedEngine/voxelVS.hlsl b/WickedEngine/voxelVS.hlsl index 0755d93bf..9a85552e3 100644 --- a/WickedEngine/voxelVS.hlsl +++ b/WickedEngine/voxelVS.hlsl @@ -12,18 +12,18 @@ VSOut main( uint vertexID : SV_VERTEXID ) { VSOut o; - uint3 coord = unflatten3D(vertexID, g_xWorld_VoxelRadianceDataRes); + uint3 coord = unflatten3D(vertexID, g_xFrame_VoxelRadianceDataRes); o.pos = float4(coord, 1); o.col = texture_voxelradiance[coord]; //[branch] //if (o.col.a > 0) //{ - // float3 pos = (float3)coord / g_xWorld_VoxelRadianceDataRes * 2 - 1; + // float3 pos = (float3)coord / g_xFrame_VoxelRadianceDataRes * 2 - 1; // pos.y = -pos.y; - // pos *= g_xWorld_VoxelRadianceDataRes; + // pos *= g_xFrame_VoxelRadianceDataRes; // pos += /*CUBE[vertexID].xyz*/CreateCube(vertexID) * 2 /*+ float3(1,-1,1)*/; - // pos *= g_xWorld_VoxelRadianceDataRes * g_xWorld_VoxelRadianceDataSize / g_xWorld_VoxelRadianceDataRes; + // pos *= g_xFrame_VoxelRadianceDataRes * g_xFrame_VoxelRadianceDataSize / g_xFrame_VoxelRadianceDataRes; // o.pos = mul(float4(pos, 1), g_xTransform); // o.col *= g_xColor; diff --git a/WickedEngine/wiAllocator.h b/WickedEngine/wiAllocator.h index 99a3d7e87..53d3dfdad 100644 --- a/WickedEngine/wiAllocator.h +++ b/WickedEngine/wiAllocator.h @@ -35,6 +35,12 @@ namespace wiAllocators return nullptr; } + inline void free(size_t size) + { + assert(offset >= size); + offset -= size; + } + inline void reset() { offset = 0; diff --git a/WickedEngine/wiEnums.h b/WickedEngine/wiEnums.h index 24da765da..47fc4f3fe 100644 --- a/WickedEngine/wiEnums.h +++ b/WickedEngine/wiEnums.h @@ -51,7 +51,6 @@ enum STENCILREF // constant buffers enum CBTYPES { - CBTYPE_WORLD, CBTYPE_FRAME, CBTYPE_CAMERA, CBTYPE_MISC, diff --git a/WickedEngine/wiImage.cpp b/WickedEngine/wiImage.cpp index c6229a370..6ac532c58 100644 --- a/WickedEngine/wiImage.cpp +++ b/WickedEngine/wiImage.cpp @@ -621,12 +621,12 @@ void wiImage::Draw(Texture2D* texture, const wiImageEffects& effects,GRAPHICSTHR if(effects.blurDir==0) { device->BindGraphicsPSO(&postprocessPSO[POSTPROCESS_BLUR_H], threadID); - prcb.xPPParams1.z = 1.0f / wiRenderer::GetInternalResolution().x; + prcb.xPPParams1.w = 1.0f / wiRenderer::GetInternalResolution().x; } else { device->BindGraphicsPSO(&postprocessPSO[POSTPROCESS_BLUR_V], threadID); - prcb.xPPParams1.z = 1.0f / wiRenderer::GetInternalResolution().y; + prcb.xPPParams1.w = 1.0f / wiRenderer::GetInternalResolution().y; } static float weight0 = 1.0f; diff --git a/WickedEngine/wiInitializer.cpp b/WickedEngine/wiInitializer.cpp index 260af5fee..120ba55d2 100644 --- a/WickedEngine/wiInitializer.cpp +++ b/WickedEngine/wiInitializer.cpp @@ -33,7 +33,7 @@ namespace wiInitializer wiWidget::LoadShaders(); wiGPUSortLib::LoadShaders(); - wiPhysics::Initialize(); + wiPhysicsEngine::Initialize(); if (FAILED(wiSoundEffect::Initialize()) || FAILED(wiMusic::Initialize())) { diff --git a/WickedEngine/wiPhysicsEngine.h b/WickedEngine/wiPhysicsEngine.h index d934820b9..77f67c37e 100644 --- a/WickedEngine/wiPhysicsEngine.h +++ b/WickedEngine/wiPhysicsEngine.h @@ -2,7 +2,7 @@ #include "wiECS.h" #include "wiSceneSystem_Decl.h" -namespace wiPhysics +namespace wiPhysicsEngine { void Initialize(); void CleanUp(); diff --git a/WickedEngine/wiPhysicsEngine_Bullet.cpp b/WickedEngine/wiPhysicsEngine_Bullet.cpp index 49adc38aa..c7d26d79e 100644 --- a/WickedEngine/wiPhysicsEngine_Bullet.cpp +++ b/WickedEngine/wiPhysicsEngine_Bullet.cpp @@ -19,7 +19,7 @@ using namespace std; using namespace wiECS; using namespace wiSceneSystem; -namespace wiPhysics +namespace wiPhysicsEngine { btVector3 gravity(0, -110, 0); int softbodyIterationCount = 5; @@ -85,35 +85,24 @@ namespace wiPhysics int id = it->second; lookup.erase(it); - btCollisionObject* obj = dynamicsWorld->getCollisionObjectArray()[id]; - btRigidBody* body = btRigidBody::upcast(obj); + btCollisionObject* collisionobject = dynamicsWorld->getCollisionObjectArray()[id]; - if (body && body->getMotionState()) + btRigidBody* rigidbody = btRigidBody::upcast(collisionobject); + if (rigidbody != nullptr) { - delete body->getMotionState(); - } - while (dynamicsWorld->getNumConstraints()) - { - btTypedConstraint* pc = dynamicsWorld->getConstraint(0); - dynamicsWorld->removeConstraint(pc); - delete pc; - } - - btSoftBody* softBody = btSoftBody::upcast(obj); - if (softBody) - { - ((btSoftRigidDynamicsWorld*)dynamicsWorld)->removeSoftBody(softBody); + dynamicsWorld->removeRigidBody(rigidbody); } else { - btRigidBody* body = btRigidBody::upcast(obj); - if (body) - dynamicsWorld->removeRigidBody(body); - else - dynamicsWorld->removeCollisionObject(obj); + btSoftBody* softbody = btSoftBody::upcast(collisionobject); + + if (softbody != nullptr) + { + ((btSoftRigidDynamicsWorld*)dynamicsWorld)->removeSoftBody(softbody); + } } - delete obj; + dynamicsWorld->removeCollisionObject(collisionobject); } } int AddRigidBody(Entity entity, wiSceneSystem::RigidBodyPhysicsComponent& physicscomponent, const wiSceneSystem::MeshComponent& mesh, const wiSceneSystem::TransformComponent& transform) @@ -187,7 +176,7 @@ namespace wiPhysics if (shape != nullptr) { - shape->setMargin(btScalar(0.05)); + shape->setMargin(btScalar(0.01)); btScalar mass(physicscomponent.mass); diff --git a/WickedEngine/wiRenderer.cpp b/WickedEngine/wiRenderer.cpp index c2d20b91b..2530515f2 100644 --- a/WickedEngine/wiRenderer.cpp +++ b/WickedEngine/wiRenderer.cpp @@ -62,11 +62,11 @@ GPUBuffer *constantBuffers[CBTYPE_LAST] = {}; GPUBuffer *resourceBuffers[RBTYPE_LAST] = {}; Texture *textures[TEXTYPE_LAST] = {}; Sampler *customsamplers[SSTYPE_LAST] = {}; -GPURingBuffer *dynamicVertexBufferPool = nullptr; string SHADERPATH = "shaders/"; LinearAllocator frameAllocators[GRAPHICSTHREAD_COUNT]; +GPURingBuffer dynamicVertexBufferPools[GRAPHICSTHREAD_COUNT] = {}; float GAMMA = 2.2f; int SHADOWRES_2D = 1024; @@ -154,7 +154,7 @@ GraphicsDevice* GetDevice() return graphicsDevice; } - +// Direct reference to a renderable instance: struct RenderBatch { uint32_t hash; @@ -171,11 +171,11 @@ struct RenderBatch instance = (uint32_t)instanceIndex; } - inline size_t GetMeshIndex() const + inline uint32_t GetMeshIndex() const { - return (size_t)((hash >> 8) & 0x00FFFFFF); + return (hash >> 8) & 0x00FFFFFF; } - inline size_t GetInstanceIndex() const + inline uint32_t GetInstanceIndex() const { return instance; } @@ -184,10 +184,12 @@ struct RenderBatch return (float)(hash & 0xFF); } }; + +// This is just a utility that points to a linear array of render batches: struct RenderQueue { RenderBatch* batchArray = nullptr; - size_t batchCount = 0; + uint32_t batchCount = 0; enum RenderQueueSortType { @@ -232,13 +234,15 @@ struct RenderQueue } } }; + +// This is a storage for component indices inside the camera frustum. These can directly index the corresponding ComponentManagers: struct FrameCulling { Frustum frustum; - vector culledObjects; - vector culledLights; - vector culledDecals; - vector culledEnvProbes; + vector culledObjects; + vector culledLights; + vector culledDecals; + vector culledEnvProbes; void Clear() { @@ -317,33 +321,6 @@ GFX_STRUCT InstancePrev ALIGN_16 }; -CBUFFER(MaterialCB, CBSLOT_RENDERER_MATERIAL) -{ - XMFLOAT4 baseColor; // + alpha (.w) - XMFLOAT4 texMulAdd; - float roughness; - float reflectance; - float metalness; - float emissive; - float refractionIndex; - float subsurfaceScattering; - float normalMapStrength; - float parallaxOcclusionMapping; - - void Create(const MaterialComponent& mat) - { - baseColor = mat.baseColor; - texMulAdd = mat.texMulAdd; - roughness = mat.roughness; - reflectance = mat.reflectance; - metalness = mat.metalness; - emissive = mat.emissive; - refractionIndex = mat.refractionIndex; - subsurfaceScattering = mat.subsurfaceScattering; - normalMapStrength = (mat.normalMap == nullptr ? 0 : mat.normalMapStrength); - parallaxOcclusionMapping = mat.parallaxOcclusionMapping; - } -}; GPUBuffer impostorMaterialCB; GPUBuffer impostorVB_POS; @@ -601,8 +578,6 @@ void CleanUp() SAFE_DELETE(customsamplers[i]); } - SAFE_DELETE(dynamicVertexBufferPool); - SAFE_DELETE(graphicsDevice); } void ClearWorld() @@ -1198,7 +1173,7 @@ ComputePSO* CPSO_tiledlighting[TILEDLIGHTING_TYPE_COUNT][TILEDLIGHTING_CULLING_C ComputePSO* CPSO[CSTYPE_LAST] = {}; void RenderMeshes(const XMFLOAT3& eye, const RenderQueue& renderQueue, SHADERTYPE shaderType, UINT renderTypeFlags, GRAPHICSTHREAD threadID, - bool tessellation = false, bool occlusionCulling = false) + bool tessellation = false) { // Intensive section, refactor and optimize! @@ -1232,162 +1207,31 @@ void RenderMeshes(const XMFLOAT3& eye, const RenderQueue& renderQueue, SHADERTYP shaderType == SHADERTYPE_DEPTHONLY || shaderType == SHADERTYPE_VOXELIZE; - GraphicsPSO* impostorRequest = GetImpostorPSO(shaderType); - - //// Render impostors: - //if (impostorRequest != nullptr) - //{ - // bool impostorGraphicsStateComplete = false; - - // for (size_t batchID = 0; batchID < renderQueue.batchCount; ++batchID) - // { - // const RenderBatch& batch = renderQueue.batchArray[batchID]; - - // const size_t meshIndex = batch.GetMeshIndex(); - // const MeshComponent& mesh = scene.meshes[meshIndex]; - // if (!mesh.IsRenderable() || !mesh.HasImpostor()) - // { - // continue; - // } - - // //const auto& visibleInstances = iter->second; - - // UINT instancesOffset; - // size_t alloc_size = /*visibleInstances.size()*/ 1; - // alloc_size *= advancedVBRequest ? sizeof(InstBuf) : sizeof(Instance); - // void* instances = device->AllocateFromRingBuffer(dynamicVertexBufferPool, alloc_size, instancesOffset, threadID); - - // int k = 0; - // for (size_t instanceID = 0; instanceID < 1; ++instanceID) - // { - // size_t instanceIndex = batch.GetInstanceIndex(); - // const ObjectComponent& instance = scene.objects[instanceIndex]; - // if (occlusionCulling && instance.IsOccluded()) - // continue; - - // const AABB& aabb = scene.aabb_objects[instanceIndex]; - - // const float impostorThreshold = aabb.getRadius(); - // float dist = wiMath::Distance(eye, aabb.getCenter()); - // float dither = instance.GetTransparency(); - // dither = wiMath::SmoothStep(1.0f, dither, wiMath::Clamp((dist - mesh.impostorDistance) / impostorThreshold, 0, 1)); - // if (dither > 1.0f - FLT_EPSILON) - // continue; - - // XMMATRIX boxMat = mesh.aabb.getAsBoxMatrix(); - - // Entity objectEntity = scene.objects.GetEntity(instanceIndex); - // const TransformComponent& transform = *scene.transforms.GetComponent(objectEntity); - - // XMFLOAT4X4 tempMat; - // XMStoreFloat4x4(&tempMat, boxMat*XMLoadFloat4x4(&transform.world)); - - // if (advancedVBRequest) - // { - // ((volatile InstBuf*)instances)[k].instance.Create(tempMat, instance.color, dither); - - // const PreviousFrameTransformComponent& prev_transform = *scene.prev_transforms.GetComponent(objectEntity); - // XMStoreFloat4x4(&tempMat, boxMat*XMLoadFloat4x4(&prev_transform.world_prev)); - // ((volatile InstBuf*)instances)[k].instancePrev.Create(tempMat); - // } - // else - // { - // ((volatile Instance*)instances)[k].Create(tempMat, instance.color, dither); - // } - - // ++k; - // } - - // device->InvalidateBufferAccess(dynamicVertexBufferPool, threadID); - - // if (k < 1) - // continue; - - // if (!advancedVBRequest || IsWireRender()) - // { - // GPUBuffer* vbs[] = { - // &impostorVB_POS, - // &impostorVB_TEX, - // dynamicVertexBufferPool - // }; - // UINT strides[] = { - // sizeof(MeshComponent::Vertex_POS), - // sizeof(MeshComponent::Vertex_TEX), - // sizeof(Instance) - // }; - // UINT offsets[] = { - // 0, - // 0, - // instancesOffset - // }; - // device->BindVertexBuffers(vbs, 0, ARRAYSIZE(vbs), strides, offsets, threadID); - // } - // else - // { - // GPUBuffer* vbs[] = { - // &impostorVB_POS, - // &impostorVB_TEX, - // &impostorVB_POS, - // dynamicVertexBufferPool - // }; - // UINT strides[] = { - // sizeof(MeshComponent::Vertex_POS), - // sizeof(MeshComponent::Vertex_TEX), - // sizeof(MeshComponent::Vertex_POS), - // sizeof(InstBuf) - // }; - // UINT offsets[] = { - // 0, - // 0, - // 0, - // instancesOffset - // }; - // device->BindVertexBuffers(vbs, 0, ARRAYSIZE(vbs), strides, offsets, threadID); - // } - - // GPUResource* res[] = { - // mesh.impostorTarget.GetTexture(0), - // mesh.impostorTarget.GetTexture(1), - // mesh.impostorTarget.GetTexture(2) - // }; - // device->BindResources(PS, res, TEXSLOT_ONDEMAND0, (easyTextureBind ? 1 : ARRAYSIZE(res)), threadID); - - // if (!impostorGraphicsStateComplete) - // { - // device->BindGraphicsPSO(impostorRequest, threadID); - // device->BindConstantBuffer(PS, &impostorMaterialCB, CB_GETBINDSLOT(MaterialCB), threadID); - // SetAlphaRef(0.75f, threadID); - // impostorGraphicsStateComplete = true; - // } - - // device->DrawInstanced(6 * 6, k, 0, 0, threadID); // 6 * 6: see MeshComponent::CreateImpostorVB function - - // } - //} - // Pre-allocate space for all the instances in GPU-buffer: const UINT instanceDataSize = advancedVBRequest ? sizeof(InstBuf) : sizeof(Instance); UINT instancesOffset; const size_t alloc_size = renderQueue.batchCount * instanceDataSize; - void* instances = device->AllocateFromRingBuffer(dynamicVertexBufferPool, alloc_size, instancesOffset, threadID); + void* instances = device->AllocateFromRingBuffer(&dynamicVertexBufferPools[threadID], alloc_size, instancesOffset, threadID); + // Purpose of InstancedBatch: + // The RenderQueue is sorted by meshIndex. There can be multiple instances for a single meshIndex, + // and the InstancedBatchArray contains this information. The array size will be the unique mesh count here. struct InstancedBatch { - size_t meshIndex; + uint32_t meshIndex; int instanceCount; - UINT dataOffset; + uint32_t dataOffset; }; InstancedBatch* instancedBatchArray = nullptr; int instancedBatchCount = 0; - int k = 0; // global instance counter size_t prevMeshIndex = ~0; - for (size_t batchID = 0; batchID < renderQueue.batchCount; ++batchID) + for (uint32_t batchID = 0; batchID < renderQueue.batchCount; ++batchID) // Do not break out of this loop! { const RenderBatch& batch = renderQueue.batchArray[batchID]; - const size_t meshIndex = batch.GetMeshIndex(); - const size_t instanceIndex = batch.GetInstanceIndex(); + const uint32_t meshIndex = batch.GetMeshIndex(); + const uint32_t instanceIndex = batch.GetInstanceIndex(); // When we encounter a new mesh inside the global instance array, we begin a new InstancedBatch: if (meshIndex != prevMeshIndex) @@ -1397,7 +1241,7 @@ void RenderMeshes(const XMFLOAT3& eye, const RenderQueue& renderQueue, SHADERTYP InstancedBatch* instancedBatch = (InstancedBatch*)frameAllocators[threadID].allocate(sizeof(InstancedBatch)); instancedBatch->meshIndex = meshIndex; instancedBatch->instanceCount = 0; - instancedBatch->dataOffset = instancesOffset + k * instanceDataSize; + instancedBatch->dataOffset = instancesOffset + batchID * instanceDataSize; if (instancedBatchArray == nullptr) { instancedBatchArray = instancedBatch; @@ -1405,8 +1249,6 @@ void RenderMeshes(const XMFLOAT3& eye, const RenderQueue& renderQueue, SHADERTYP } const ObjectComponent& instance = scene.objects[instanceIndex]; - if (occlusionCulling && instance.IsOccluded()) - continue; float dither = instance.GetTransparency(); @@ -1416,27 +1258,26 @@ void RenderMeshes(const XMFLOAT3& eye, const RenderQueue& renderQueue, SHADERTYP // Write into actual GPU-buffer: if (advancedVBRequest) { - ((volatile InstBuf*)instances)[k].instance.Create(transform.world, instance.color); + ((volatile InstBuf*)instances)[batchID].instance.Create(transform.world, instance.color); const PreviousFrameTransformComponent& prev_transform = *scene.prev_transforms.GetComponent(objectEntity); - ((volatile InstBuf*)instances)[k].instancePrev.Create(prev_transform.world_prev); + ((volatile InstBuf*)instances)[batchID].instancePrev.Create(prev_transform.world_prev); } else { - ((volatile Instance*)instances)[k].Create(transform.world, instance.color, dither); + ((volatile Instance*)instances)[batchID].Create(transform.world, instance.color, dither); } - k++; // next global instance instancedBatchArray[instancedBatchCount - 1].instanceCount++; // next instance in current InstancedBatch } - device->InvalidateBufferAccess(dynamicVertexBufferPool, threadID); // closes instance GPU-buffer, ready to draw! + device->InvalidateBufferAccess(&dynamicVertexBufferPools[threadID], threadID); // closes instance GPU-buffer, ready to draw! // Render instanced batches: PRIMITIVETOPOLOGY prevTOPOLOGY = TRIANGLELIST; - for (int instancedBatchIdx = 0; instancedBatchIdx < instancedBatchCount; ++instancedBatchIdx) + for (int instancedBatchID = 0; instancedBatchID < instancedBatchCount; ++instancedBatchID) { - const InstancedBatch& instancedBatch = instancedBatchArray[instancedBatchIdx]; + const InstancedBatch& instancedBatch = instancedBatchArray[instancedBatchID]; const MeshComponent& mesh = scene.meshes[instancedBatch.meshIndex]; @@ -1473,7 +1314,7 @@ void RenderMeshes(const XMFLOAT3& eye, const RenderQueue& renderQueue, SHADERTYP } const MaterialComponent& material = *scene.materials.GetComponent(subset.materialID); - GraphicsPSO* pso = GetObjectPSO(shaderType, mesh.IsDoubleSided(), tessellatorRequested, material, forceAlphaTestForDithering) /*: material.customShader->passes[shaderType].pso*/; + GraphicsPSO* pso = GetObjectPSO(shaderType, mesh.IsDoubleSided(), tessellatorRequested, material, forceAlphaTestForDithering); if (pso == nullptr) { continue; @@ -1549,7 +1390,7 @@ void RenderMeshes(const XMFLOAT3& eye, const RenderQueue& renderQueue, SHADERTYP { GPUBuffer* vbs[] = { mesh.streamoutBuffer_POS.get() != nullptr ? mesh.streamoutBuffer_POS.get() : mesh.vertexBuffer_POS.get(), - dynamicVertexBufferPool + &dynamicVertexBufferPools[threadID] }; UINT strides[] = { sizeof(MeshComponent::Vertex_POS), @@ -1567,7 +1408,7 @@ void RenderMeshes(const XMFLOAT3& eye, const RenderQueue& renderQueue, SHADERTYP GPUBuffer* vbs[] = { mesh.streamoutBuffer_POS.get() != nullptr ? mesh.streamoutBuffer_POS.get() : mesh.vertexBuffer_POS.get(), mesh.vertexBuffer_TEX.get(), - dynamicVertexBufferPool + &dynamicVertexBufferPools[threadID] }; UINT strides[] = { sizeof(MeshComponent::Vertex_POS), @@ -1588,7 +1429,7 @@ void RenderMeshes(const XMFLOAT3& eye, const RenderQueue& renderQueue, SHADERTYP mesh.streamoutBuffer_POS.get() != nullptr ? mesh.streamoutBuffer_POS.get() : mesh.vertexBuffer_POS.get(), mesh.vertexBuffer_TEX.get(), mesh.streamoutBuffer_PRE.get() != nullptr ? mesh.streamoutBuffer_PRE.get() : mesh.vertexBuffer_POS.get(), - dynamicVertexBufferPool + &dynamicVertexBufferPools[threadID] }; UINT strides[] = { sizeof(MeshComponent::Vertex_POS), @@ -1632,266 +1473,174 @@ void RenderMeshes(const XMFLOAT3& eye, const RenderQueue& renderQueue, SHADERTYP } } - - - //// Render meshes: - //for (size_t batchID = 0; batchID < renderQueue.batchCount; ++batchID) - //{ - // const RenderBatch& batch = renderQueue.batchArray[batchID]; - - // const size_t meshIndex = batch.GetMeshIndex(); - // const MeshComponent& mesh = scene.meshes[meshIndex]; - - // const float tessF = mesh.GetTessellationFactor(); - // const bool tessellatorRequested = tessF > 0 && tessellation; - - // if (tessellatorRequested) - // { - // TessellationCB tessCB; - // tessCB.g_f4TessFactors = XMFLOAT4(tessF, tessF, tessF, tessF); - // device->UpdateBuffer(constantBuffers[CBTYPE_TESSELLATION], &tessCB, threadID); - // device->BindConstantBuffer(HS, constantBuffers[CBTYPE_TESSELLATION], CBSLOT_RENDERER_TESSELLATION, threadID); - // } - - // bool forceAlphaTestForDithering = false; - - // UINT instancesOffset; - // size_t alloc_size = /*visibleInstances.size()*/1; - // alloc_size *= advancedVBRequest ? sizeof(InstBuf) : sizeof(Instance); - // void* instances = device->AllocateFromRingBuffer(dynamicVertexBufferPool, alloc_size, instancesOffset, threadID); - - // int k = 0; - // for (size_t instanceID = 0; instanceID < 1; ++instanceID) - // { - // size_t instanceIndex = batch.GetInstanceIndex(); - // const ObjectComponent& instance = scene.objects[instanceIndex]; - // if (occlusionCulling && instance.IsOccluded()) - // continue; - - // float dither = instance.GetTransparency(); - // if (impostorRequest != nullptr) - // { - // // fade out to impostor... - // const AABB& aabb = scene.aabb_objects[instanceIndex]; - // const float impostorThreshold = aabb.getRadius(); - // float dist = wiMath::Distance(eye, aabb.getCenter()); - // if (mesh.HasImpostor()) - // dither = wiMath::SmoothStep(dither, 1.0f, wiMath::Clamp((dist - impostorThreshold - mesh.impostorDistance) / impostorThreshold, 0, 1)); - // } - // if (dither > 1.0f - FLT_EPSILON) - // continue; - - // forceAlphaTestForDithering = forceAlphaTestForDithering || (dither > 0); - - // Entity objectEntity = scene.objects.GetEntity(instanceIndex); - // const TransformComponent& transform = *scene.transforms.GetComponent(objectEntity); - - // if (advancedVBRequest || tessellatorRequested) - // { - // ((volatile InstBuf*)instances)[k].instance.Create(transform.world, instance.color); - - // const PreviousFrameTransformComponent& prev_transform = *scene.prev_transforms.GetComponent(objectEntity); - // ((volatile InstBuf*)instances)[k].instancePrev.Create(prev_transform.world_prev); - // } - // else - // { - // ((volatile Instance*)instances)[k].Create(transform.world, instance.color, dither); - // } - - // ++k; - - // } - - // device->InvalidateBufferAccess(dynamicVertexBufferPool, threadID); - - // if (k < 1) - // continue; - - // device->BindIndexBuffer(mesh.indexBuffer.get(), mesh.GetIndexFormat(), 0, threadID); - - // enum class BOUNDVERTEXBUFFERTYPE - // { - // NOTHING, - // POSITION, - // POSITION_TEXCOORD, - // EVERYTHING, - // }; - // BOUNDVERTEXBUFFERTYPE boundVBType_Prev = BOUNDVERTEXBUFFERTYPE::NOTHING; - - // for (const MeshComponent::MeshSubset& subset : mesh.subsets) - // { - // if (subset.indexCount == 0) - // { - // continue; - // } - // const MaterialComponent& material = *scene.materials.GetComponent(subset.materialID); - - // GraphicsPSO* pso = GetObjectPSO(shaderType, mesh.IsDoubleSided(), tessellatorRequested, material, forceAlphaTestForDithering) /*: material.customShader->passes[shaderType].pso*/; - // if (pso == nullptr) - // { - // continue; - // } - - // bool subsetRenderable = false; - - // if (renderTypeFlags & RENDERTYPE_OPAQUE) - // { - // subsetRenderable = subsetRenderable || (!material.IsTransparent() && !material.IsWater()); - // } - // if (renderTypeFlags & RENDERTYPE_TRANSPARENT) - // { - // subsetRenderable = subsetRenderable || material.IsTransparent(); - // } - // if (renderTypeFlags & RENDERTYPE_WATER) - // { - // subsetRenderable = subsetRenderable || material.IsWater(); - // } - // if (shaderType == SHADERTYPE_SHADOW || shaderType == SHADERTYPE_SHADOWCUBE) - // { - // subsetRenderable = subsetRenderable && material.IsCastingShadow(); - // } - - // if (!subsetRenderable) - // { - // continue; - // } - - // BOUNDVERTEXBUFFERTYPE boundVBType; - // if (advancedVBRequest || tessellatorRequested) - // { - // boundVBType = BOUNDVERTEXBUFFERTYPE::EVERYTHING; - // } - // else - // { - // // simple vertex buffers are used in some passes (note: tessellator requires more attributes) - // if ((shaderType == SHADERTYPE_DEPTHONLY || shaderType == SHADERTYPE_SHADOW || shaderType == SHADERTYPE_SHADOWCUBE) && !material.IsAlphaTestEnabled() && !forceAlphaTestForDithering) - // { - // if (shaderType == SHADERTYPE_SHADOW && material.IsTransparent()) - // { - // boundVBType = BOUNDVERTEXBUFFERTYPE::POSITION_TEXCOORD; - // } - // else - // { - // // bypass texcoord stream for non alphatested shadows and zprepass - // boundVBType = BOUNDVERTEXBUFFERTYPE::POSITION; - // } - // } - // else - // { - // boundVBType = BOUNDVERTEXBUFFERTYPE::POSITION_TEXCOORD; - // } - // } - - // if (material.IsWater()) - // { - // boundVBType = BOUNDVERTEXBUFFERTYPE::POSITION_TEXCOORD; - // } - - // if (IsWireRender()) - // { - // boundVBType = BOUNDVERTEXBUFFERTYPE::POSITION_TEXCOORD; - // } - - // // Only bind vertex buffers when the layout changes - // if (boundVBType != boundVBType_Prev) - // { - // // Assemble the required vertex buffer: - // switch (boundVBType) - // { - // case BOUNDVERTEXBUFFERTYPE::POSITION: - // { - // GPUBuffer* vbs[] = { - // mesh.streamoutBuffer_POS.get() != nullptr ? mesh.streamoutBuffer_POS.get() : mesh.vertexBuffer_POS.get(), - // dynamicVertexBufferPool - // }; - // UINT strides[] = { - // sizeof(MeshComponent::Vertex_POS), - // sizeof(Instance) - // }; - // UINT offsets[] = { - // 0, - // instancesOffset - // }; - // device->BindVertexBuffers(vbs, 0, ARRAYSIZE(vbs), strides, offsets, threadID); - // } - // break; - // case BOUNDVERTEXBUFFERTYPE::POSITION_TEXCOORD: - // { - // GPUBuffer* vbs[] = { - // mesh.streamoutBuffer_POS.get() != nullptr ? mesh.streamoutBuffer_POS.get() : mesh.vertexBuffer_POS.get(), - // mesh.vertexBuffer_TEX.get(), - // dynamicVertexBufferPool - // }; - // UINT strides[] = { - // sizeof(MeshComponent::Vertex_POS), - // sizeof(MeshComponent::Vertex_TEX), - // sizeof(Instance) - // }; - // UINT offsets[] = { - // 0, - // 0, - // instancesOffset - // }; - // device->BindVertexBuffers(vbs, 0, ARRAYSIZE(vbs), strides, offsets, threadID); - // } - // break; - // case BOUNDVERTEXBUFFERTYPE::EVERYTHING: - // { - // GPUBuffer* vbs[] = { - // mesh.streamoutBuffer_POS.get() != nullptr ? mesh.streamoutBuffer_POS.get() : mesh.vertexBuffer_POS.get(), - // mesh.vertexBuffer_TEX.get(), - // mesh.streamoutBuffer_PRE.get() != nullptr ? mesh.streamoutBuffer_PRE.get() : mesh.vertexBuffer_POS.get(), - // dynamicVertexBufferPool - // }; - // UINT strides[] = { - // sizeof(MeshComponent::Vertex_POS), - // sizeof(MeshComponent::Vertex_TEX), - // sizeof(MeshComponent::Vertex_POS), - // sizeof(InstBuf) - // }; - // UINT offsets[] = { - // 0, - // 0, - // 0, - // instancesOffset - // }; - // device->BindVertexBuffers(vbs, 0, ARRAYSIZE(vbs), strides, offsets, threadID); - // } - // break; - // default: - // assert(0); - // break; - // } - // } - // boundVBType_Prev = boundVBType; - - // device->BindConstantBuffer(PS, material.constantBuffer.get(), CB_GETBINDSLOT(MaterialCB), threadID); - - // device->BindStencilRef(material.GetStencilRef(), threadID); - - // device->BindGraphicsPSO(pso, threadID); - - // GPUResource* res[] = { - // material.GetBaseColorMap(), - // material.GetNormalMap(), - // material.GetSurfaceMap(), - // material.GetDisplacementMap(), - // }; - // device->BindResources(PS, res, TEXSLOT_ONDEMAND0, (easyTextureBind ? 2 : ARRAYSIZE(res)), threadID); - - // SetAlphaRef(material.alphaRef, threadID); - - // device->DrawIndexedInstanced((int)subset.indexCount, k, subset.indexOffset, 0, 0, threadID); - // } - - //} - ResetAlphaRef(threadID); + frameAllocators[threadID].free(sizeof(InstancedBatch) * instancedBatchCount); + device->EventEnd(threadID); } } +void RenderImpostors(const XMFLOAT3& eye, const RenderQueue& renderQueue, SHADERTYPE shaderType, UINT renderTypeFlags, GRAPHICSTHREAD threadID) +{ + //GraphicsPSO* impostorRequest = GetImpostorPSO(shaderType); + + //if (!renderQueue.empty() && impostorRequest != nullptr) + //{ + // GraphicsDevice* device = GetDevice(); + // Scene& scene = GetScene(); + + // device->EventBegin("RenderImpostors", threadID); + + // const bool easyTextureBind = + // shaderType == SHADERTYPE_TEXTURE || + // shaderType == SHADERTYPE_SHADOW || + // shaderType == SHADERTYPE_SHADOWCUBE || + // shaderType == SHADERTYPE_DEPTHONLY || + // shaderType == SHADERTYPE_VOXELIZE; + + + // // Pre-allocate space for all the instances in GPU-buffer: + // const UINT instanceDataSize = sizeof(Instance); + // UINT instancesOffset; + // const size_t alloc_size = renderQueue.batchCount * instanceDataSize; + // void* instances = device->AllocateFromRingBuffer(dynamicVertexBufferPool, alloc_size, instancesOffset, threadID); + + // // Render impostors: + // if (impostorRequest != nullptr) + // { + // bool impostorGraphicsStateComplete = false; + + // for (size_t batchID = 0; batchID < renderQueue.batchCount; ++batchID) + // { + // const RenderBatch& batch = renderQueue.batchArray[batchID]; + + // const size_t meshIndex = batch.GetMeshIndex(); + // const MeshComponent& mesh = scene.meshes[meshIndex]; + // if (!mesh.IsRenderable() || !mesh.HasImpostor()) + // { + // continue; + // } + + // //const auto& visibleInstances = iter->second; + + // UINT instancesOffset; + // size_t alloc_size = /*visibleInstances.size()*/ 1; + // alloc_size *= advancedVBRequest ? sizeof(InstBuf) : sizeof(Instance); + // void* instances = device->AllocateFromRingBuffer(dynamicVertexBufferPool, alloc_size, instancesOffset, threadID); + + // int k = 0; + // for (size_t instanceID = 0; instanceID < 1; ++instanceID) + // { + // size_t instanceIndex = batch.GetInstanceIndex(); + // const ObjectComponent& instance = scene.objects[instanceIndex]; + // if (occlusionCulling && instance.IsOccluded()) + // continue; + + // const AABB& aabb = scene.aabb_objects[instanceIndex]; + + // const float impostorThreshold = aabb.getRadius(); + // float dist = wiMath::Distance(eye, aabb.getCenter()); + // float dither = instance.GetTransparency(); + // dither = wiMath::SmoothStep(1.0f, dither, wiMath::Clamp((dist - mesh.impostorDistance) / impostorThreshold, 0, 1)); + // if (dither > 1.0f - FLT_EPSILON) + // continue; + + // XMMATRIX boxMat = mesh.aabb.getAsBoxMatrix(); + + // Entity objectEntity = scene.objects.GetEntity(instanceIndex); + // const TransformComponent& transform = *scene.transforms.GetComponent(objectEntity); + + // XMFLOAT4X4 tempMat; + // XMStoreFloat4x4(&tempMat, boxMat*XMLoadFloat4x4(&transform.world)); + + // if (advancedVBRequest) + // { + // ((volatile InstBuf*)instances)[k].instance.Create(tempMat, instance.color, dither); + + // const PreviousFrameTransformComponent& prev_transform = *scene.prev_transforms.GetComponent(objectEntity); + // XMStoreFloat4x4(&tempMat, boxMat*XMLoadFloat4x4(&prev_transform.world_prev)); + // ((volatile InstBuf*)instances)[k].instancePrev.Create(tempMat); + // } + // else + // { + // ((volatile Instance*)instances)[k].Create(tempMat, instance.color, dither); + // } + + // ++k; + // } + + // device->InvalidateBufferAccess(dynamicVertexBufferPool, threadID); + + // if (k < 1) + // continue; + + // if (!advancedVBRequest || IsWireRender()) + // { + // GPUBuffer* vbs[] = { + // &impostorVB_POS, + // &impostorVB_TEX, + // dynamicVertexBufferPool + // }; + // UINT strides[] = { + // sizeof(MeshComponent::Vertex_POS), + // sizeof(MeshComponent::Vertex_TEX), + // sizeof(Instance) + // }; + // UINT offsets[] = { + // 0, + // 0, + // instancesOffset + // }; + // device->BindVertexBuffers(vbs, 0, ARRAYSIZE(vbs), strides, offsets, threadID); + // } + // else + // { + // GPUBuffer* vbs[] = { + // &impostorVB_POS, + // &impostorVB_TEX, + // &impostorVB_POS, + // dynamicVertexBufferPool + // }; + // UINT strides[] = { + // sizeof(MeshComponent::Vertex_POS), + // sizeof(MeshComponent::Vertex_TEX), + // sizeof(MeshComponent::Vertex_POS), + // sizeof(InstBuf) + // }; + // UINT offsets[] = { + // 0, + // 0, + // 0, + // instancesOffset + // }; + // device->BindVertexBuffers(vbs, 0, ARRAYSIZE(vbs), strides, offsets, threadID); + // } + + // GPUResource* res[] = { + // mesh.impostorTarget.GetTexture(0), + // mesh.impostorTarget.GetTexture(1), + // mesh.impostorTarget.GetTexture(2) + // }; + // device->BindResources(PS, res, TEXSLOT_ONDEMAND0, (easyTextureBind ? 1 : ARRAYSIZE(res)), threadID); + + // if (!impostorGraphicsStateComplete) + // { + // device->BindGraphicsPSO(impostorRequest, threadID); + // device->BindConstantBuffer(PS, &impostorMaterialCB, CB_GETBINDSLOT(MaterialCB), threadID); + // SetAlphaRef(0.75f, threadID); + // impostorGraphicsStateComplete = true; + // } + + // device->DrawInstanced(6 * 6, k, 0, 0, threadID); // 6 * 6: see MeshComponent::CreateImpostorVB function + + // } + // } + + + // device->EventEnd(threadID); + //} +} void LoadShaders() @@ -2890,14 +2639,16 @@ void LoadBuffers() GPUBufferDesc bd; // Ring buffer allows fast allocation of dynamic buffers for one frame: - dynamicVertexBufferPool = new GPURingBuffer; - bd.BindFlags = BIND_VERTEX_BUFFER; - bd.ByteWidth = 1024 * 1024 * 64; - bd.Usage = USAGE_DYNAMIC; - bd.CPUAccessFlags = CPU_ACCESS_WRITE; - bd.MiscFlags = 0; - GetDevice()->CreateBuffer(&bd, nullptr, dynamicVertexBufferPool); - GetDevice()->SetName(dynamicVertexBufferPool, "DynamicVertexBufferPool"); + for (int threadID = 0; threadID < GRAPHICSTHREAD_COUNT; ++threadID) + { + bd.BindFlags = BIND_VERTEX_BUFFER; + bd.ByteWidth = 1024 * 1024 * 64; + bd.Usage = USAGE_DYNAMIC; + bd.CPUAccessFlags = CPU_ACCESS_WRITE; + bd.MiscFlags = 0; + GetDevice()->CreateBuffer(&bd, nullptr, &dynamicVertexBufferPools[threadID]); + GetDevice()->SetName(&dynamicVertexBufferPools[threadID], "DynamicVertexBufferPool"); + } for (int i = 0; i < CBTYPE_LAST; ++i) @@ -2913,10 +2664,6 @@ void LoadBuffers() // Per World Constant buffer will be updated occasionally, so it should reside in DEFAULT GPU memory! bd.CPUAccessFlags = 0; bd.Usage = USAGE_DEFAULT; - bd.ByteWidth = sizeof(WorldCB); - GetDevice()->CreateBuffer(&bd, nullptr, constantBuffers[CBTYPE_WORLD]); - GetDevice()->SetName(constantBuffers[CBTYPE_WORLD], "PerWorldConstantBuffer"); - bd.ByteWidth = sizeof(FrameCB); GetDevice()->CreateBuffer(&bd, nullptr, constantBuffers[CBTYPE_FRAME]); GetDevice()->SetName(constantBuffers[CBTYPE_FRAME], "PerFrameConstantBuffer"); @@ -3547,13 +3294,16 @@ void UpdatePerFrameData(float dt) if (culling.frustum.CheckBox(aabb)) { - const ObjectComponent& object = scene.objects[i]; - Entity entity = scene.objects.GetEntity(i); + culling.culledObjects.push_back((uint32_t)i); - culling.culledObjects.push_back(entity); - if (object.GetRenderTypes() & RENDERTYPE_WATER) + // Main camera can request reflection rendering: + if (camera == &GetCamera()) { - requestReflectionRendering = true; + const ObjectComponent& object = scene.objects[i]; + if (object.GetRenderTypes() & RENDERTYPE_WATER) + { + requestReflectionRendering = true; + } } } } @@ -3568,8 +3318,7 @@ void UpdatePerFrameData(float dt) if (culling.frustum.CheckBox(aabb)) { - Entity entity = scene.decals.GetEntity(i); - culling.culledDecals.push_back(entity); + culling.culledDecals.push_back((uint32_t)i); } } @@ -3580,8 +3329,7 @@ void UpdatePerFrameData(float dt) if (culling.frustum.CheckBox(aabb)) { - Entity entity = scene.probes.GetEntity(i); - culling.culledEnvProbes.push_back(entity); + culling.culledEnvProbes.push_back((uint32_t)i); } } @@ -3592,17 +3340,16 @@ void UpdatePerFrameData(float dt) if (culling.frustum.CheckBox(aabb)) { - Entity entity = scene.lights.GetEntity(i); - culling.culledLights.push_back(entity); + culling.culledLights.push_back((uint32_t)i); } } int i = 0; int shadowCounter_2D = 0; int shadowCounter_Cube = 0; - for (Entity entity : culling.culledLights) + for (uint32_t lightIndex : culling.culledLights) { - LightComponent& light = *scene.lights.GetComponent(entity); + LightComponent& light = scene.lights[lightIndex]; light.entityArray_index = i; // Link shadowmaps to lights till there are free slots @@ -3717,7 +3464,16 @@ void UpdateRenderData(GRAPHICSTHREAD threadID) { material.SetClean(); - materialGPUData.Create(material); + materialGPUData.g_xMat_baseColor = material.baseColor; + materialGPUData.g_xMat_texMulAdd = material.texMulAdd; + materialGPUData.g_xMat_roughness = material.roughness; + materialGPUData.g_xMat_reflectance = material.reflectance; + materialGPUData.g_xMat_metalness = material.metalness; + materialGPUData.g_xMat_emissive = material.emissive; + materialGPUData.g_xMat_refractionIndex = material.refractionIndex; + materialGPUData.g_xMat_subsurfaceScattering = material.subsurfaceScattering; + materialGPUData.g_xMat_normalMapStrength = (material.normalMap == nullptr ? 0 : material.normalMapStrength); + materialGPUData.g_xMat_parallaxOcclusionMapping = material.parallaxOcclusionMapping; if (material.constantBuffer == nullptr) { @@ -3746,8 +3502,6 @@ void UpdateRenderData(GRAPHICSTHREAD threadID) // Fill Light Array with lights + envprobes + decals in the frustum: { - const auto& culledLights = mainCameraCulling.culledLights; - static ShaderEntityType* entityArray = (ShaderEntityType*)_mm_malloc(sizeof(ShaderEntityType)*MAX_SHADER_ENTITY_COUNT, 16); static XMMATRIX* matrixArray = (XMMATRIX*)_mm_malloc(sizeof(XMMATRIX)*MATRIXARRAY_COUNT, 16); @@ -3766,7 +3520,7 @@ void UpdateRenderData(GRAPHICSTHREAD threadID) entityArrayCount_EnvProbes = 0; entityArrayOffset_Lights = entityCounter; - for (Entity entity : culledLights) + for (uint32_t lightIndex : mainCameraCulling.culledLights) { if (entityCounter == MAX_SHADER_ENTITY_COUNT) { @@ -3775,7 +3529,7 @@ void UpdateRenderData(GRAPHICSTHREAD threadID) break; } - const LightComponent& light = *scene.lights.GetComponent(entity); + const LightComponent& light = scene.lights[lightIndex]; const int shadowIndex = light.shadowMap_index; @@ -3841,7 +3595,7 @@ void UpdateRenderData(GRAPHICSTHREAD threadID) entityArrayCount_Lights = entityCounter - entityArrayOffset_Lights; entityArrayOffset_EnvProbes = entityCounter; - for (Entity entity : mainCameraCulling.culledEnvProbes) + for (uint32_t probeIndex : mainCameraCulling.culledEnvProbes) { if (entityCounter == MAX_SHADER_ENTITY_COUNT) { @@ -3856,7 +3610,7 @@ void UpdateRenderData(GRAPHICSTHREAD threadID) break; } - EnvironmentProbeComponent& probe = *scene.probes.GetComponent(entity); + EnvironmentProbeComponent& probe = scene.probes[probeIndex]; if (probe.textureIndex < 0) { continue; @@ -3877,7 +3631,7 @@ void UpdateRenderData(GRAPHICSTHREAD threadID) entityArrayCount_EnvProbes = entityCounter - entityArrayOffset_EnvProbes; entityArrayOffset_Decals = entityCounter; - for (Entity entity : mainCameraCulling.culledDecals) + for (uint32_t decalIndex : mainCameraCulling.culledDecals) { if (entityCounter == MAX_SHADER_ENTITY_COUNT) { @@ -3891,7 +3645,7 @@ void UpdateRenderData(GRAPHICSTHREAD threadID) matrixCounter--; break; } - const DecalComponent& decal = *scene.decals.GetComponent(entity); + const DecalComponent& decal = scene.decals[decalIndex]; entityArray[entityCounter].type = ENTITY_TYPE_DECAL; entityArray[entityCounter].positionWS = decal.position; @@ -3920,7 +3674,6 @@ void UpdateRenderData(GRAPHICSTHREAD threadID) } const ForceFieldComponent& force = scene.forces[i]; - Entity entity = scene.forces.GetEntity(i); entityArray[entityCounter].type = force.type; entityArray[entityCounter].positionWS = force.position; @@ -3946,8 +3699,6 @@ void UpdateRenderData(GRAPHICSTHREAD threadID) device->BindResources(CS, resources, SBSLOT_ENTITYARRAY, ARRAYSIZE(resources), threadID); } - - UpdateWorldCB(threadID); // only commits when parameters are changed UpdateFrameCB(threadID); BindPersistentState(threadID); @@ -4138,9 +3889,9 @@ void OcclusionCulling_Render(GRAPHICSTHREAD threadID) MiscCB cb; - for (Entity entity : culling.culledObjects) + for (uint32_t instanceIndex : culling.culledObjects) { - ObjectComponent& object = *scene.objects.GetComponent(entity); + ObjectComponent& object = scene.objects[instanceIndex]; if (!object.IsRenderable()) { continue; @@ -4159,7 +3910,7 @@ void OcclusionCulling_Render(GRAPHICSTHREAD threadID) continue; } - const AABB& aabb = *scene.aabb_objects.GetComponent(entity); + const AABB& aabb = scene.aabb_objects[instanceIndex]; if (aabb.intersects(GetCamera().Eye)) { @@ -4205,9 +3956,9 @@ void OcclusionCulling_Read() Scene& scene = GetScene(); - for (Entity entity : culling.culledObjects) + for (uint32_t instanceIndex : culling.culledObjects) { - ObjectComponent& object = *scene.objects.GetComponent(entity); + ObjectComponent& object = scene.objects[instanceIndex]; if (!object.IsRenderable()) { continue; @@ -4406,7 +4157,6 @@ void DrawTrails(GRAPHICSTHREAD threadID, Texture2D* refracRes) void DrawLights(const CameraComponent& camera, GRAPHICSTHREAD threadID) { const FrameCulling& culling = frameCullings[&camera]; - const auto& culledLights = culling.culledLights; Scene& scene = GetScene(); @@ -4423,9 +4173,9 @@ void DrawLights(const CameraComponent& camera, GRAPHICSTHREAD threadID) { GetDevice()->BindGraphicsPSO(PSO_deferredlight[type], threadID); - for (Entity entity : culledLights) + for (uint32_t lightIndex : culling.culledLights) { - const LightComponent& light = *scene.lights.GetComponent(entity); + const LightComponent& light = scene.lights[lightIndex]; if (light.GetType() != type) continue; @@ -4482,125 +4232,128 @@ void DrawLights(const CameraComponent& camera, GRAPHICSTHREAD threadID) } void DrawLightVisualizers(const CameraComponent& camera, GRAPHICSTHREAD threadID) { - Scene& scene = GetScene(); + const FrameCulling& culling = frameCullings[&camera]; - GetDevice()->EventBegin("Light Visualizer Render", threadID); - - GetDevice()->BindConstantBuffer(PS, constantBuffers[CBTYPE_VOLUMELIGHT], CB_GETBINDSLOT(VolumeLightCB), threadID); - GetDevice()->BindConstantBuffer(VS, constantBuffers[CBTYPE_VOLUMELIGHT], CB_GETBINDSLOT(VolumeLightCB), threadID); - - XMMATRIX camrot = XMLoadFloat3x3(&camera.rotationMatrix); - - - for (int type = LightComponent::POINT; type < LightComponent::LIGHTTYPE_COUNT; ++type) + if (!culling.culledLights.empty()) { - GetDevice()->BindGraphicsPSO(PSO_lightvisualizer[type], threadID); + Scene& scene = GetScene(); - for (size_t i = 0; i < scene.lights.GetCount(); ++i) + GetDevice()->EventBegin("Light Visualizer Render", threadID); + + GetDevice()->BindConstantBuffer(PS, constantBuffers[CBTYPE_VOLUMELIGHT], CB_GETBINDSLOT(VolumeLightCB), threadID); + GetDevice()->BindConstantBuffer(VS, constantBuffers[CBTYPE_VOLUMELIGHT], CB_GETBINDSLOT(VolumeLightCB), threadID); + + XMMATRIX camrot = XMLoadFloat3x3(&camera.rotationMatrix); + + + for (int type = LightComponent::POINT; type < LightComponent::LIGHTTYPE_COUNT; ++type) { - LightComponent& light = scene.lights[i]; + GetDevice()->BindGraphicsPSO(PSO_lightvisualizer[type], threadID); - if (light.GetType() == type && light.IsVisualizerEnabled()) + for (uint32_t lightIndex : culling.culledLights) { + LightComponent& light = scene.lights[lightIndex]; - VolumeLightCB lcb; - lcb.lightColor = XMFLOAT4(light.color.x, light.color.y, light.color.z, 1); - lcb.lightEnerdis = XMFLOAT4(light.energy, light.range, light.fov, light.energy); - - if (type == LightComponent::POINT) + if (light.GetType() == type && light.IsVisualizerEnabled()) { - lcb.lightEnerdis.w = light.range*light.energy*0.01f; // scale - XMStoreFloat4x4(&lcb.lightWorld, XMMatrixTranspose( - XMMatrixScaling(lcb.lightEnerdis.w, lcb.lightEnerdis.w, lcb.lightEnerdis.w)* - camrot* - XMMatrixTranslationFromVector(XMLoadFloat3(&light.position)) - )); - GetDevice()->UpdateBuffer(constantBuffers[CBTYPE_VOLUMELIGHT], &lcb, threadID); + VolumeLightCB lcb; + lcb.lightColor = XMFLOAT4(light.color.x, light.color.y, light.color.z, 1); + lcb.lightEnerdis = XMFLOAT4(light.energy, light.range, light.fov, light.energy); - GetDevice()->Draw(108, 0, threadID); // circle - } - else if(type == LightComponent::SPOT) - { - float coneS = (float)(light.fov / 0.7853981852531433); - lcb.lightEnerdis.w = light.range*light.energy*0.03f; // scale - XMStoreFloat4x4(&lcb.lightWorld, XMMatrixTranspose( - XMMatrixScaling(coneS*lcb.lightEnerdis.w, lcb.lightEnerdis.w, coneS*lcb.lightEnerdis.w)* - XMMatrixRotationQuaternion(XMLoadFloat4(&light.rotation))* - XMMatrixTranslationFromVector(XMLoadFloat3(&light.position)) - )); + if (type == LightComponent::POINT) + { + lcb.lightEnerdis.w = light.range*light.energy*0.01f; // scale + XMStoreFloat4x4(&lcb.lightWorld, XMMatrixTranspose( + XMMatrixScaling(lcb.lightEnerdis.w, lcb.lightEnerdis.w, lcb.lightEnerdis.w)* + camrot* + XMMatrixTranslationFromVector(XMLoadFloat3(&light.position)) + )); - GetDevice()->UpdateBuffer(constantBuffers[CBTYPE_VOLUMELIGHT], &lcb, threadID); + GetDevice()->UpdateBuffer(constantBuffers[CBTYPE_VOLUMELIGHT], &lcb, threadID); - GetDevice()->Draw(192, 0, threadID); // cone - } - else if (type == LightComponent::SPHERE) - { - XMStoreFloat4x4(&lcb.lightWorld, XMMatrixTranspose( - XMMatrixScaling(light.radius, light.radius, light.radius)* - XMMatrixRotationQuaternion(XMLoadFloat4(&light.rotation))* - XMMatrixTranslationFromVector(XMLoadFloat3(&light.position))* - camera.GetViewProjection() - )); + GetDevice()->Draw(108, 0, threadID); // circle + } + else if (type == LightComponent::SPOT) + { + float coneS = (float)(light.fov / 0.7853981852531433); + lcb.lightEnerdis.w = light.range*light.energy*0.03f; // scale + XMStoreFloat4x4(&lcb.lightWorld, XMMatrixTranspose( + XMMatrixScaling(coneS*lcb.lightEnerdis.w, lcb.lightEnerdis.w, coneS*lcb.lightEnerdis.w)* + XMMatrixRotationQuaternion(XMLoadFloat4(&light.rotation))* + XMMatrixTranslationFromVector(XMLoadFloat3(&light.position)) + )); - GetDevice()->UpdateBuffer(constantBuffers[CBTYPE_VOLUMELIGHT], &lcb, threadID); + GetDevice()->UpdateBuffer(constantBuffers[CBTYPE_VOLUMELIGHT], &lcb, threadID); - GetDevice()->Draw(2880, 0, threadID); // uv-sphere - } - else if (type == LightComponent::DISC) - { - XMStoreFloat4x4(&lcb.lightWorld, XMMatrixTranspose( - XMMatrixScaling(light.radius, light.radius, light.radius)* - XMMatrixRotationQuaternion(XMLoadFloat4(&light.rotation))* - XMMatrixTranslationFromVector(XMLoadFloat3(&light.position))* - camera.GetViewProjection() - )); + GetDevice()->Draw(192, 0, threadID); // cone + } + else if (type == LightComponent::SPHERE) + { + XMStoreFloat4x4(&lcb.lightWorld, XMMatrixTranspose( + XMMatrixScaling(light.radius, light.radius, light.radius)* + XMMatrixRotationQuaternion(XMLoadFloat4(&light.rotation))* + XMMatrixTranslationFromVector(XMLoadFloat3(&light.position))* + camera.GetViewProjection() + )); - GetDevice()->UpdateBuffer(constantBuffers[CBTYPE_VOLUMELIGHT], &lcb, threadID); + GetDevice()->UpdateBuffer(constantBuffers[CBTYPE_VOLUMELIGHT], &lcb, threadID); - GetDevice()->Draw(108, 0, threadID); // circle - } - else if (type == LightComponent::RECTANGLE) - { - XMStoreFloat4x4(&lcb.lightWorld, XMMatrixTranspose( - XMMatrixScaling(light.width * 0.5f, light.height * 0.5f, 0.5f)* - XMMatrixRotationQuaternion(XMLoadFloat4(&light.rotation))* - XMMatrixTranslationFromVector(XMLoadFloat3(&light.position))* - camera.GetViewProjection() - )); + GetDevice()->Draw(2880, 0, threadID); // uv-sphere + } + else if (type == LightComponent::DISC) + { + XMStoreFloat4x4(&lcb.lightWorld, XMMatrixTranspose( + XMMatrixScaling(light.radius, light.radius, light.radius)* + XMMatrixRotationQuaternion(XMLoadFloat4(&light.rotation))* + XMMatrixTranslationFromVector(XMLoadFloat3(&light.position))* + camera.GetViewProjection() + )); - GetDevice()->UpdateBuffer(constantBuffers[CBTYPE_VOLUMELIGHT], &lcb, threadID); + GetDevice()->UpdateBuffer(constantBuffers[CBTYPE_VOLUMELIGHT], &lcb, threadID); - GetDevice()->Draw(6, 0, threadID); // quad - } - else if (type == LightComponent::TUBE) - { - XMStoreFloat4x4(&lcb.lightWorld, XMMatrixTranspose( - XMMatrixScaling(max(light.width * 0.5f, light.radius), light.radius, light.radius)* - XMMatrixRotationQuaternion(XMLoadFloat4(&light.rotation))* - XMMatrixTranslationFromVector(XMLoadFloat3(&light.position))* - camera.GetViewProjection() - )); + GetDevice()->Draw(108, 0, threadID); // circle + } + else if (type == LightComponent::RECTANGLE) + { + XMStoreFloat4x4(&lcb.lightWorld, XMMatrixTranspose( + XMMatrixScaling(light.width * 0.5f, light.height * 0.5f, 0.5f)* + XMMatrixRotationQuaternion(XMLoadFloat4(&light.rotation))* + XMMatrixTranslationFromVector(XMLoadFloat3(&light.position))* + camera.GetViewProjection() + )); - GetDevice()->UpdateBuffer(constantBuffers[CBTYPE_VOLUMELIGHT], &lcb, threadID); + GetDevice()->UpdateBuffer(constantBuffers[CBTYPE_VOLUMELIGHT], &lcb, threadID); - GetDevice()->Draw(384, 0, threadID); // cylinder + GetDevice()->Draw(6, 0, threadID); // quad + } + else if (type == LightComponent::TUBE) + { + XMStoreFloat4x4(&lcb.lightWorld, XMMatrixTranspose( + XMMatrixScaling(max(light.width * 0.5f, light.radius), light.radius, light.radius)* + XMMatrixRotationQuaternion(XMLoadFloat4(&light.rotation))* + XMMatrixTranslationFromVector(XMLoadFloat3(&light.position))* + camera.GetViewProjection() + )); + + GetDevice()->UpdateBuffer(constantBuffers[CBTYPE_VOLUMELIGHT], &lcb, threadID); + + GetDevice()->Draw(384, 0, threadID); // cylinder + } } } + } + GetDevice()->EventEnd(threadID); + } - - GetDevice()->EventEnd(threadID); - - } void DrawVolumeLights(const CameraComponent& camera, GRAPHICSTHREAD threadID) { const FrameCulling& culling = frameCullings[&camera]; - const auto& culledLights = culling.culledLights; - if (!culledLights.empty()) + if (!culling.culledLights.empty()) { GetDevice()->EventBegin("Volumetric Light Render", threadID); @@ -4617,9 +4370,9 @@ void DrawVolumeLights(const CameraComponent& camera, GRAPHICSTHREAD threadID) GetDevice()->BindGraphicsPSO(pso, threadID); - for (Entity entity : culledLights) + for (uint32_t lightIndex : culling.culledLights) { - const LightComponent& light = *scene.lights.GetComponent(entity); + const LightComponent& light = scene.lights[lightIndex]; if (light.GetType() == type && light.IsVolumetricsEnabled()) { @@ -4682,13 +4435,12 @@ void DrawLensFlares(GRAPHICSTHREAD threadID) const CameraComponent& camera = GetCamera(); const FrameCulling& culling = frameCullings[&camera]; - const auto& culledLights = culling.culledLights; Scene& scene = GetScene(); - for(Entity entity : culledLights) + for(uint32_t lightIndex : culling.culledLights) { - const LightComponent& light = *scene.lights.GetComponent(entity); + const LightComponent& light = scene.lights[lightIndex]; if(!light.lensFlareRimTextures.empty()) { @@ -4800,51 +4552,222 @@ void SetShadowPropsCube(int resolution, int count) } void DrawForShadowMap(GRAPHICSTHREAD threadID, uint32_t layerMask) { - if (wireRender) + if (IsWireRender()) return; - // We need to render shadows even if the gamespeed is 0 for these reasons: - // 1.) Shadow cascades is updated every time according to camera - // 2.) We can move any other light, or object, too + const FrameCulling& culling = frameCullings[&GetCamera()]; - //if (GetGameSpeed() > 0) + if (!culling.culledLights.empty()) { GetDevice()->EventBegin("ShadowMap Render", threadID); wiProfiler::GetInstance().BeginRange("Shadow Rendering", wiProfiler::DOMAIN_GPU, threadID); const bool all_layers = layerMask == 0xFFFFFFFF; - const FrameCulling& culling = frameCullings[&GetCamera()]; - const auto& culledLights = culling.culledLights; ViewPort vp; // RGB: Shadow tint (multiplicative), A: Refraction caustics(additive) const float transparentShadowClearColor[] = { 1,1,1,0 }; - if (!culledLights.empty()) + + Scene& scene = GetScene(); + + GetDevice()->UnbindResources(TEXSLOT_SHADOWARRAY_2D, 2, threadID); + + int shadowCounter_2D = 0; + int shadowCounter_Cube = 0; + for (int type = 0; type < LightComponent::LIGHTTYPE_COUNT; ++type) { - Scene& scene = GetScene(); - - GetDevice()->UnbindResources(TEXSLOT_SHADOWARRAY_2D, 2, threadID); - - int shadowCounter_2D = 0; - int shadowCounter_Cube = 0; - for (int type = 0; type < LightComponent::LIGHTTYPE_COUNT; ++type) + switch (type) { + case LightComponent::DIRECTIONAL: + case LightComponent::SPOT: + { + vp.TopLeftX = 0; + vp.TopLeftY = 0; + vp.Width = (float)SHADOWRES_2D; + vp.Height = (float)SHADOWRES_2D; + vp.MinDepth = 0.0f; + vp.MaxDepth = 1.0f; + GetDevice()->BindViewports(1, &vp, threadID); + break; + } + break; + case LightComponent::POINT: + case LightComponent::SPHERE: + case LightComponent::DISC: + case LightComponent::RECTANGLE: + case LightComponent::TUBE: + { + vp.TopLeftX = 0; + vp.TopLeftY = 0; + vp.Width = (float)SHADOWRES_CUBE; + vp.Height = (float)SHADOWRES_CUBE; + vp.MinDepth = 0.0f; + vp.MaxDepth = 1.0f; + GetDevice()->BindViewports(1, &vp, threadID); + + GetDevice()->BindConstantBuffer(GS, constantBuffers[CBTYPE_CUBEMAPRENDER], CB_GETBINDSLOT(CubemapRenderCB), threadID); + break; + } + break; + default: + break; + } + + for (uint32_t lightIndex : culling.culledLights) + { + const LightComponent& light = scene.lights[lightIndex]; + if (light.GetType() != type || !light.IsCastingShadow()) + { + continue; + } + switch (type) { case LightComponent::DIRECTIONAL: + { + if ((shadowCounter_2D + 2) >= SHADOWCOUNT_2D || light.shadowMap_index < 0 || light.shadowCam_dirLight.empty()) + break; + shadowCounter_2D += 3; // shadow indices are already complete so a shadow slot is consumed here even if no rendering actually happens! + + for (uint32_t cascade = 0; cascade < 3; ++cascade) + { + const float siz = light.shadowCam_dirLight[cascade].size * 0.5f; + const float f = light.shadowCam_dirLight[cascade].farplane * 0.5f; + AABB boundingbox; + boundingbox.createFromHalfWidth(XMFLOAT3(0, 0, 0), XMFLOAT3(siz, siz, f)); + + RenderQueue renderQueue; + bool transparentShadowsRequested = false; + for (size_t i = 0; i < scene.aabb_objects.GetCount(); ++i) + { + const AABB& aabb = scene.aabb_objects[i]; + if (boundingbox.get(XMMatrixInverse(0, XMLoadFloat4x4(&light.shadowCam_dirLight[cascade].View))).intersects(aabb)) + { + const ObjectComponent& object = scene.objects[i]; + if (object.IsRenderable() && cascade >= object.cascadeMask && object.IsCastingShadow()) + { + if (!all_layers) + { + Entity cullable_entity = scene.aabb_objects.GetEntity(i); + const LayerComponent& layer = *scene.layers.GetComponent(cullable_entity); + if (!(layerMask & layer.GetLayerMask())) + { + continue; + } + } + + RenderBatch* batch = (RenderBatch*)frameAllocators[threadID].allocate(sizeof(RenderBatch)); + size_t meshIndex = scene.meshes.GetIndex(object.meshID); + batch->Create(meshIndex, i, 0); + renderQueue.add(batch); + + if (object.GetRenderTypes() & RENDERTYPE_TRANSPARENT || object.GetRenderTypes() & RENDERTYPE_WATER) + { + transparentShadowsRequested = true; + } + } + } + } + if (!renderQueue.empty()) + { + CameraCB cb; + XMStoreFloat4x4(&cb.g_xCamera_VP, light.shadowCam_dirLight[cascade].getVP()); + GetDevice()->UpdateBuffer(constantBuffers[CBTYPE_CAMERA], &cb, threadID); + + GetDevice()->ClearDepthStencil(shadowMapArray_2D, CLEAR_DEPTH, 0.0f, 0, threadID, light.shadowMap_index + cascade); + + // unfortunately we will always have to clear the associated transparent shadowmap to avoid discrepancy with shadowmap indexing changes across frames + GetDevice()->ClearRenderTarget(shadowMapArray_Transparent, transparentShadowClearColor, threadID, light.shadowMap_index + cascade); + + // render opaque shadowmap: + GetDevice()->BindRenderTargets(0, nullptr, shadowMapArray_2D, threadID, light.shadowMap_index + cascade); + RenderMeshes(light.shadowCam_dirLight[cascade].Eye, renderQueue, SHADERTYPE_SHADOW, RENDERTYPE_OPAQUE, threadID); + + if (GetTransparentShadowsEnabled() && transparentShadowsRequested) + { + // render transparent shadowmap: + Texture2D* rts[] = { + shadowMapArray_Transparent + }; + GetDevice()->BindRenderTargets(ARRAYSIZE(rts), rts, shadowMapArray_2D, threadID, light.shadowMap_index + cascade); + RenderMeshes(light.shadowCam_dirLight[cascade].Eye, renderQueue, SHADERTYPE_SHADOW, RENDERTYPE_TRANSPARENT | RENDERTYPE_WATER, threadID); + } + frameAllocators[threadID].free(sizeof(RenderBatch) * renderQueue.batchCount); + } + + } + } + break; case LightComponent::SPOT: { - vp.TopLeftX = 0; - vp.TopLeftY = 0; - vp.Width = (float)SHADOWRES_2D; - vp.Height = (float)SHADOWRES_2D; - vp.MinDepth = 0.0f; - vp.MaxDepth = 1.0f; - GetDevice()->BindViewports(1, &vp, threadID); - break; + if (shadowCounter_2D >= SHADOWCOUNT_2D || light.shadowMap_index < 0 || light.shadowCam_spotLight.empty()) + break; + shadowCounter_2D++; // shadow indices are already complete so a shadow slot is consumed here even if no rendering actually happens! + + Frustum frustum; + frustum.ConstructFrustum(light.shadowCam_spotLight[0].farplane, light.shadowCam_spotLight[0].realProjection, light.shadowCam_spotLight[0].View); + + RenderQueue renderQueue; + bool transparentShadowsRequested = false; + for (size_t i = 0; i < scene.aabb_objects.GetCount(); ++i) + { + const AABB& aabb = scene.aabb_objects[i]; + if (frustum.CheckBox(aabb)) + { + const ObjectComponent& object = scene.objects[i]; + if (object.IsRenderable() && object.IsCastingShadow()) + { + if (!all_layers) + { + Entity cullable_entity = scene.aabb_objects.GetEntity(i); + const LayerComponent& layer = *scene.layers.GetComponent(cullable_entity); + if (!(layerMask & layer.GetLayerMask())) + { + continue; + } + } + RenderBatch* batch = (RenderBatch*)frameAllocators[threadID].allocate(sizeof(RenderBatch)); + size_t meshIndex = scene.meshes.GetIndex(object.meshID); + batch->Create(meshIndex, i, 0); + renderQueue.add(batch); + + if (object.GetRenderTypes() & RENDERTYPE_TRANSPARENT || object.GetRenderTypes() & RENDERTYPE_WATER) + { + transparentShadowsRequested = true; + } + } + } + } + if (!renderQueue.empty()) + { + CameraCB cb; + XMStoreFloat4x4(&cb.g_xCamera_VP, light.shadowCam_spotLight[0].getVP()); + GetDevice()->UpdateBuffer(constantBuffers[CBTYPE_CAMERA], &cb, threadID); + + GetDevice()->ClearDepthStencil(shadowMapArray_2D, CLEAR_DEPTH, 0.0f, 0, threadID, light.shadowMap_index); + + // unfortunately we will always have to clear the associated transparent shadowmap to avoid discrepancy with shadowmap indexing changes across frames + GetDevice()->ClearRenderTarget(shadowMapArray_Transparent, transparentShadowClearColor, threadID, light.shadowMap_index); + + // render opaque shadowmap: + GetDevice()->BindRenderTargets(0, nullptr, shadowMapArray_2D, threadID, light.shadowMap_index); + RenderMeshes(frustum.getCamPos(), renderQueue, SHADERTYPE_SHADOW, RENDERTYPE_OPAQUE, threadID); + + if (GetTransparentShadowsEnabled() && transparentShadowsRequested) + { + // render transparent shadowmap: + Texture2D* rts[] = { + shadowMapArray_Transparent + }; + GetDevice()->BindRenderTargets(ARRAYSIZE(rts), rts, shadowMapArray_2D, threadID, light.shadowMap_index); + RenderMeshes(frustum.getCamPos(), renderQueue, SHADERTYPE_SHADOW, RENDERTYPE_TRANSPARENT | RENDERTYPE_WATER, threadID); + } + frameAllocators[threadID].free(sizeof(RenderBatch) * renderQueue.batchCount); + } + } break; case LightComponent::POINT: @@ -4853,226 +4776,66 @@ void DrawForShadowMap(GRAPHICSTHREAD threadID, uint32_t layerMask) case LightComponent::RECTANGLE: case LightComponent::TUBE: { - vp.TopLeftX = 0; - vp.TopLeftY = 0; - vp.Width = (float)SHADOWRES_CUBE; - vp.Height = (float)SHADOWRES_CUBE; - vp.MinDepth = 0.0f; - vp.MaxDepth = 1.0f; - GetDevice()->BindViewports(1, &vp, threadID); + if (shadowCounter_Cube >= SHADOWCOUNT_CUBE || light.shadowMap_index < 0 || light.shadowCam_pointLight.empty()) + break; + shadowCounter_Cube++; // shadow indices are already complete so a shadow slot is consumed here even if no rendering actually happens! + + RenderQueue renderQueue; + for (size_t i = 0; i < scene.aabb_objects.GetCount(); ++i) + { + const AABB& aabb = scene.aabb_objects[i]; + if (SPHERE(light.position, light.range).intersects(aabb)) + { + const ObjectComponent& object = scene.objects[i]; + if (object.IsRenderable() && object.IsCastingShadow() && object.GetRenderTypes() == RENDERTYPE_OPAQUE) + { + if (!all_layers) + { + Entity cullable_entity = scene.aabb_objects.GetEntity(i); + const LayerComponent& layer = *scene.layers.GetComponent(cullable_entity); + if (!(layerMask & layer.GetLayerMask())) + { + continue; + } + } + + RenderBatch* batch = (RenderBatch*)frameAllocators[threadID].allocate(sizeof(RenderBatch)); + size_t meshIndex = scene.meshes.GetIndex(object.meshID); + batch->Create(meshIndex, i, 0); + renderQueue.add(batch); + } + } + } + if (!renderQueue.empty()) + { + GetDevice()->BindRenderTargets(0, nullptr, shadowMapArray_Cube, threadID, light.shadowMap_index); + GetDevice()->ClearDepthStencil(shadowMapArray_Cube, CLEAR_DEPTH, 0.0f, 0, threadID, light.shadowMap_index); + + MiscCB miscCb; + miscCb.g_xColor = float4(light.position.x, light.position.y, light.position.z, 1.0f / light.GetRange()); // reciprocal range, to avoid division in shader + GetDevice()->UpdateBuffer(constantBuffers[CBTYPE_MISC], &miscCb, threadID); + + CubemapRenderCB cb; + for (size_t shcam = 0; shcam < light.shadowCam_pointLight.size(); ++shcam) + { + XMStoreFloat4x4(&cb.xCubeShadowVP[shcam], light.shadowCam_pointLight[shcam].getVP()); + } + GetDevice()->UpdateBuffer(constantBuffers[CBTYPE_CUBEMAPRENDER], &cb, threadID); + + RenderMeshes(light.position, renderQueue, SHADERTYPE_SHADOWCUBE, RENDERTYPE_OPAQUE, threadID); + + frameAllocators[threadID].free(sizeof(RenderBatch) * renderQueue.batchCount); + } - GetDevice()->BindConstantBuffer(GS, constantBuffers[CBTYPE_CUBEMAPRENDER], CB_GETBINDSLOT(CubemapRenderCB), threadID); - break; } break; - default: - break; - } - - for (Entity entity : culledLights) - { - const LightComponent& light = *scene.lights.GetComponent(entity); - if (light.GetType() != type || !light.IsCastingShadow()) - { - continue; - } - - switch (type) - { - case LightComponent::DIRECTIONAL: - { - if ((shadowCounter_2D + 2) >= SHADOWCOUNT_2D || light.shadowMap_index < 0 || light.shadowCam_dirLight.empty()) - break; - shadowCounter_2D += 3; // shadow indices are already complete so a shadow slot is consumed here even if no rendering actually happens! - - for (uint32_t cascade = 0; cascade < 3; ++cascade) - { - const float siz = light.shadowCam_dirLight[cascade].size * 0.5f; - const float f = light.shadowCam_dirLight[cascade].farplane * 0.5f; - AABB boundingbox; - boundingbox.createFromHalfWidth(XMFLOAT3(0, 0, 0), XMFLOAT3(siz, siz, f)); - - RenderQueue renderQueue; - bool transparentShadowsRequested = false; - for (size_t i = 0; i < scene.aabb_objects.GetCount(); ++i) - { - const AABB& aabb = scene.aabb_objects[i]; - if (boundingbox.get(XMMatrixInverse(0, XMLoadFloat4x4(&light.shadowCam_dirLight[cascade].View))).intersects(aabb)) - { - const ObjectComponent& object = scene.objects[i]; - if (object.IsRenderable() && cascade >= object.cascadeMask && object.IsCastingShadow()) - { - Entity cullable_entity = scene.aabb_objects.GetEntity(i); - const LayerComponent& layer = *scene.layers.GetComponent(cullable_entity); - if (all_layers || (layerMask & layer.GetLayerMask())) - { - RenderBatch* batch = (RenderBatch*)frameAllocators[threadID].allocate(sizeof(RenderBatch)); - size_t meshIndex = scene.meshes.GetIndex(object.meshID); - batch->Create(meshIndex, i, 0); - renderQueue.add(batch); - - if (object.GetRenderTypes() & RENDERTYPE_TRANSPARENT || object.GetRenderTypes() & RENDERTYPE_WATER) - { - transparentShadowsRequested = true; - } - } - } - } - } - if (!renderQueue.empty()) - { - CameraCB cb; - XMStoreFloat4x4(&cb.g_xCamera_VP, light.shadowCam_dirLight[cascade].getVP()); - GetDevice()->UpdateBuffer(constantBuffers[CBTYPE_CAMERA], &cb, threadID); - - GetDevice()->ClearDepthStencil(shadowMapArray_2D, CLEAR_DEPTH, 0.0f, 0, threadID, light.shadowMap_index + cascade); - - // unfortunately we will always have to clear the associated transparent shadowmap to avoid discrepancy with shadowmap indexing changes across frames - GetDevice()->ClearRenderTarget(shadowMapArray_Transparent, transparentShadowClearColor, threadID, light.shadowMap_index + cascade); - - // render opaque shadowmap: - GetDevice()->BindRenderTargets(0, nullptr, shadowMapArray_2D, threadID, light.shadowMap_index + cascade); - RenderMeshes(light.shadowCam_dirLight[cascade].Eye, renderQueue, SHADERTYPE_SHADOW, RENDERTYPE_OPAQUE, threadID); - - if (GetTransparentShadowsEnabled() && transparentShadowsRequested) - { - // render transparent shadowmap: - Texture2D* rts[] = { - shadowMapArray_Transparent - }; - GetDevice()->BindRenderTargets(ARRAYSIZE(rts), rts, shadowMapArray_2D, threadID, light.shadowMap_index + cascade); - RenderMeshes(light.shadowCam_dirLight[cascade].Eye, renderQueue, SHADERTYPE_SHADOW, RENDERTYPE_TRANSPARENT | RENDERTYPE_WATER, threadID); - } - } - - } - } - break; - case LightComponent::SPOT: - { - if (shadowCounter_2D >= SHADOWCOUNT_2D || light.shadowMap_index < 0 || light.shadowCam_spotLight.empty()) - break; - shadowCounter_2D++; // shadow indices are already complete so a shadow slot is consumed here even if no rendering actually happens! - - Frustum frustum; - frustum.ConstructFrustum(light.shadowCam_spotLight[0].farplane, light.shadowCam_spotLight[0].realProjection, light.shadowCam_spotLight[0].View); - - RenderQueue renderQueue; - bool transparentShadowsRequested = false; - for (size_t i = 0; i < scene.aabb_objects.GetCount(); ++i) - { - const AABB& aabb = scene.aabb_objects[i]; - if (frustum.CheckBox(aabb)) - { - const ObjectComponent& object = scene.objects[i]; - if (object.IsRenderable() && object.IsCastingShadow()) - { - Entity cullable_entity = scene.aabb_objects.GetEntity(i); - const LayerComponent& layer = *scene.layers.GetComponent(cullable_entity); - if (all_layers || (layerMask & layer.GetLayerMask())) - { - RenderBatch* batch = (RenderBatch*)frameAllocators[threadID].allocate(sizeof(RenderBatch)); - size_t meshIndex = scene.meshes.GetIndex(object.meshID); - batch->Create(meshIndex, i, 0); - renderQueue.add(batch); - - if (object.GetRenderTypes() & RENDERTYPE_TRANSPARENT || object.GetRenderTypes() & RENDERTYPE_WATER) - { - transparentShadowsRequested = true; - } - } - } - } - } - if (!renderQueue.empty()) - { - CameraCB cb; - XMStoreFloat4x4(&cb.g_xCamera_VP, light.shadowCam_spotLight[0].getVP()); - GetDevice()->UpdateBuffer(constantBuffers[CBTYPE_CAMERA], &cb, threadID); - - GetDevice()->ClearDepthStencil(shadowMapArray_2D, CLEAR_DEPTH, 0.0f, 0, threadID, light.shadowMap_index); - - // unfortunately we will always have to clear the associated transparent shadowmap to avoid discrepancy with shadowmap indexing changes across frames - GetDevice()->ClearRenderTarget(shadowMapArray_Transparent, transparentShadowClearColor, threadID, light.shadowMap_index); - - // render opaque shadowmap: - GetDevice()->BindRenderTargets(0, nullptr, shadowMapArray_2D, threadID, light.shadowMap_index); - RenderMeshes(frustum.getCamPos(), renderQueue, SHADERTYPE_SHADOW, RENDERTYPE_OPAQUE, threadID); - - if (GetTransparentShadowsEnabled() && transparentShadowsRequested) - { - // render transparent shadowmap: - Texture2D* rts[] = { - shadowMapArray_Transparent - }; - GetDevice()->BindRenderTargets(ARRAYSIZE(rts), rts, shadowMapArray_2D, threadID, light.shadowMap_index); - RenderMeshes(frustum.getCamPos(), renderQueue, SHADERTYPE_SHADOW, RENDERTYPE_TRANSPARENT | RENDERTYPE_WATER, threadID); - } - } - - } - break; - case LightComponent::POINT: - case LightComponent::SPHERE: - case LightComponent::DISC: - case LightComponent::RECTANGLE: - case LightComponent::TUBE: - { - if (shadowCounter_Cube >= SHADOWCOUNT_CUBE || light.shadowMap_index < 0 || light.shadowCam_pointLight.empty()) - break; - shadowCounter_Cube++; // shadow indices are already complete so a shadow slot is consumed here even if no rendering actually happens! - - RenderQueue renderQueue; - for (size_t i = 0; i < scene.aabb_objects.GetCount(); ++i) - { - const AABB& aabb = scene.aabb_objects[i]; - if (SPHERE(light.position, light.range).intersects(aabb)) - { - const ObjectComponent& object = scene.objects[i]; - if (object.IsRenderable() && object.IsCastingShadow() && object.GetRenderTypes() == RENDERTYPE_OPAQUE) - { - Entity cullable_entity = scene.aabb_objects.GetEntity(i); - const LayerComponent& layer = *scene.layers.GetComponent(cullable_entity); - if (all_layers || (layerMask & layer.GetLayerMask())) - { - RenderBatch* batch = (RenderBatch*)frameAllocators[threadID].allocate(sizeof(RenderBatch)); - size_t meshIndex = scene.meshes.GetIndex(object.meshID); - batch->Create(meshIndex, i, 0); - renderQueue.add(batch); - } - } - } - } - if (!renderQueue.empty()) - { - GetDevice()->BindRenderTargets(0, nullptr, shadowMapArray_Cube, threadID, light.shadowMap_index); - GetDevice()->ClearDepthStencil(shadowMapArray_Cube, CLEAR_DEPTH, 0.0f, 0, threadID, light.shadowMap_index); - - MiscCB miscCb; - miscCb.g_xColor = float4(light.position.x, light.position.y, light.position.z, 1.0f / light.GetRange()); // reciprocal range, to avoid division in shader - GetDevice()->UpdateBuffer(constantBuffers[CBTYPE_MISC], &miscCb, threadID); - - CubemapRenderCB cb; - for (unsigned int shcam = 0; shcam < light.shadowCam_pointLight.size(); ++shcam) - { - XMStoreFloat4x4(&cb.xCubeShadowVP[shcam], light.shadowCam_pointLight[shcam].getVP()); - } - - GetDevice()->UpdateBuffer(constantBuffers[CBTYPE_CUBEMAPRENDER], &cb, threadID); - - RenderMeshes(light.position, renderQueue, SHADERTYPE_SHADOWCUBE, RENDERTYPE_OPAQUE, threadID); - } - - } - break; - } // terminate switch - } - + } // terminate switch } - GetDevice()->BindRenderTargets(0, nullptr, nullptr, threadID); } + GetDevice()->BindRenderTargets(0, nullptr, nullptr, threadID); + wiProfiler::GetInstance().EndRange(); // Shadow Rendering GetDevice()->EventEnd(threadID); @@ -5122,10 +4885,11 @@ void DrawWorld(const CameraComponent& camera, bool tessellation, GRAPHICSTHREAD RenderQueue renderQueue; - for (Entity entity : culling.culledObjects) + for (uint32_t instanceIndex : culling.culledObjects) { if (layerMask != ~0) { + Entity entity = scene.objects.GetEntity(instanceIndex); const LayerComponent& layer = *scene.layers.GetComponent(entity); if (!(layer.GetLayerMask() & layerMask)) { @@ -5133,9 +4897,11 @@ void DrawWorld(const CameraComponent& camera, bool tessellation, GRAPHICSTHREAD } } - size_t instanceIndex = scene.objects.GetIndex(entity); const ObjectComponent& object = scene.objects[instanceIndex]; + if (GetOcclusionCullingEnabled() && occlusionCulling && object.IsOccluded()) + continue; + if (object.IsRenderable() && object.GetRenderTypes() & RENDERTYPE_OPAQUE) { RenderBatch* batch = (RenderBatch*)frameAllocators[threadID].allocate(sizeof(RenderBatch)); @@ -5147,7 +4913,10 @@ void DrawWorld(const CameraComponent& camera, bool tessellation, GRAPHICSTHREAD if (!renderQueue.empty()) { renderQueue.sort(RenderQueue::SORT_FRONT_TO_BACK); - RenderMeshes(camera.Eye, renderQueue, shaderType, RENDERTYPE_OPAQUE, threadID, tessellation, GetOcclusionCullingEnabled() && occlusionCulling); + RenderImpostors(camera.Eye, renderQueue, shaderType, RENDERTYPE_OPAQUE, threadID); + RenderMeshes(camera.Eye, renderQueue, shaderType, RENDERTYPE_OPAQUE, threadID, tessellation); + + frameAllocators[threadID].free(sizeof(RenderBatch) * renderQueue.batchCount); } GetDevice()->EventEnd(threadID); @@ -5190,10 +4959,11 @@ void DrawWorldTransparent(const CameraComponent& camera, SHADERTYPE shaderType, } RenderQueue renderQueue; - for (Entity entity : culling.culledObjects) + for (uint32_t instanceIndex : culling.culledObjects) { if (layerMask != ~0) { + Entity entity = scene.objects.GetEntity(instanceIndex); const LayerComponent& layer = *scene.layers.GetComponent(entity); if (!(layer.GetLayerMask() & layerMask)) { @@ -5201,9 +4971,11 @@ void DrawWorldTransparent(const CameraComponent& camera, SHADERTYPE shaderType, } } - size_t instanceIndex = scene.objects.GetIndex(entity); const ObjectComponent& object = scene.objects[instanceIndex]; + if (GetOcclusionCullingEnabled() && occlusionCulling && object.IsOccluded()) + continue; + if (object.IsRenderable() && object.GetRenderTypes() & RENDERTYPE_TRANSPARENT) { RenderBatch* batch = (RenderBatch*)frameAllocators[threadID].allocate(sizeof(RenderBatch)); @@ -5215,7 +4987,9 @@ void DrawWorldTransparent(const CameraComponent& camera, SHADERTYPE shaderType, if (!renderQueue.empty()) { renderQueue.sort(RenderQueue::SORT_BACK_TO_FRONT); - RenderMeshes(camera.Eye, renderQueue, shaderType, RENDERTYPE_TRANSPARENT | RENDERTYPE_WATER, threadID, false, GetOcclusionCullingEnabled() && occlusionCulling); + RenderMeshes(camera.Eye, renderQueue, shaderType, RENDERTYPE_TRANSPARENT | RENDERTYPE_WATER, threadID, false); + + frameAllocators[threadID].free(sizeof(RenderBatch) * renderQueue.batchCount); } GetDevice()->EventEnd(threadID); @@ -5254,7 +5028,7 @@ void DrawDebugWorld(const CameraComponent& camera, GRAPHICSTHREAD threadID) XMFLOAT4 a, colorA, b, colorB; }; UINT offset; - void* mem = device->AllocateFromRingBuffer(dynamicVertexBufferPool, sizeof(LineSegment) * armature.boneCollection.size(), offset, threadID); + void* mem = device->AllocateFromRingBuffer(&dynamicVertexBufferPools[threadID], sizeof(LineSegment) * armature.boneCollection.size(), offset, threadID); int j = 0; for (Entity entity : armature.boneCollection) @@ -5277,10 +5051,10 @@ void DrawDebugWorld(const CameraComponent& camera, GRAPHICSTHREAD threadID) j++; } - device->InvalidateBufferAccess(dynamicVertexBufferPool, threadID); + device->InvalidateBufferAccess(&dynamicVertexBufferPools[threadID], threadID); GPUBuffer* vbs[] = { - dynamicVertexBufferPool, + &dynamicVertexBufferPools[threadID], }; const UINT strides[] = { sizeof(XMFLOAT4) + sizeof(XMFLOAT4), @@ -5313,7 +5087,7 @@ void DrawDebugWorld(const CameraComponent& camera, GRAPHICSTHREAD threadID) XMFLOAT4 a, colorA, b, colorB; }; UINT offset; - void* mem = device->AllocateFromRingBuffer(dynamicVertexBufferPool, sizeof(LineSegment) * renderableLines.size(), offset, threadID); + void* mem = device->AllocateFromRingBuffer(&dynamicVertexBufferPools[threadID], sizeof(LineSegment) * renderableLines.size(), offset, threadID); int i = 0; for (auto& line : renderableLines) @@ -5327,10 +5101,10 @@ void DrawDebugWorld(const CameraComponent& camera, GRAPHICSTHREAD threadID) i++; } - device->InvalidateBufferAccess(dynamicVertexBufferPool, threadID); + device->InvalidateBufferAccess(&dynamicVertexBufferPools[threadID], threadID); GPUBuffer* vbs[] = { - dynamicVertexBufferPool, + &dynamicVertexBufferPools[threadID], }; const UINT strides[] = { sizeof(XMFLOAT4) + sizeof(XMFLOAT4), @@ -5695,29 +5469,26 @@ void DrawSun(GRAPHICSTHREAD threadID) void DrawDecals(const CameraComponent& camera, GRAPHICSTHREAD threadID) { - GraphicsDevice* device = GetDevice(); - Scene& scene = GetScene(); + const FrameCulling& culling = frameCullings[&camera]; - bool boundCB = false; - if(scene.decals.GetCount() > 0) + if(!culling.culledDecals.empty()) { + GraphicsDevice* device = GetDevice(); + device->EventBegin("Decals", threadID); - if (!boundCB) - { - boundCB = true; - device->BindConstantBuffer(PS, constantBuffers[CBTYPE_DECAL], CB_GETBINDSLOT(DecalCB),threadID); - } + Scene& scene = GetScene(); + + device->BindConstantBuffer(PS, constantBuffers[CBTYPE_DECAL], CB_GETBINDSLOT(DecalCB),threadID); device->BindStencilRef(STENCILREF_DEFAULT, threadID); device->BindGraphicsPSO(PSO_decal, threadID); - for (size_t i = 0; i < scene.decals.GetCount(); ++i) + for (size_t decalIndex : culling.culledDecals) { - DecalComponent& decal = scene.decals[i]; - Entity entity = scene.decals.GetEntity(i); - const AABB& aabb = *scene.aabb_decals.GetComponent(entity); + const DecalComponent& decal = scene.decals[decalIndex]; + const AABB& aabb = scene.aabb_decals[decalIndex]; if ((decal.texture != nullptr || decal.normal != nullptr) && camera.frustum.CheckBox(aabb)) { @@ -5929,6 +5700,8 @@ void RefreshEnvProbes(GRAPHICSTHREAD threadID) if (!renderQueue.empty()) { RenderMeshes(center, renderQueue, SHADERTYPE_ENVMAPCAPTURE, RENDERTYPE_OPAQUE | RENDERTYPE_TRANSPARENT, threadID); + + frameAllocators[threadID].free(sizeof(RenderBatch) * renderQueue.batchCount); } // sky @@ -6100,6 +5873,7 @@ void VoxelRadiance(GRAPHICSTHREAD threadID) device->BindUAVs(PS, UAVs, 0, 1, threadID); RenderMeshes(center, renderQueue, SHADERTYPE_VOXELIZE, RENDERTYPE_OPAQUE, threadID); + frameAllocators[threadID].free(sizeof(RenderBatch) * renderQueue.batchCount); // Copy the packed voxel scene data to a 3D texture, then delete the voxel scene emission data. The cone tracing will operate on the 3D texture device->EventBegin("Voxel Scene Copy - Clear", threadID); @@ -7287,19 +7061,18 @@ void DrawTracedScene(const CameraComponent& camera, Texture2D* result, GRAPHICST const MaterialComponent& material = *scene.materials.GetComponent(subset.materialID); MaterialCB mat; - mat.Create(material); // Copy base params: - materialArray[totalMaterials].baseColor = mat.baseColor; - materialArray[totalMaterials].texMulAdd = mat.texMulAdd; - materialArray[totalMaterials].roughness = mat.roughness; - materialArray[totalMaterials].reflectance = mat.reflectance; - materialArray[totalMaterials].metalness = mat.metalness; - materialArray[totalMaterials].emissive = mat.emissive; - materialArray[totalMaterials].refractionIndex = mat.refractionIndex; - materialArray[totalMaterials].subsurfaceScattering = mat.subsurfaceScattering; - materialArray[totalMaterials].normalMapStrength = mat.normalMapStrength; - materialArray[totalMaterials].parallaxOcclusionMapping = mat.normalMapStrength; + materialArray[totalMaterials].baseColor = mat.g_xMat_baseColor; + materialArray[totalMaterials].texMulAdd = mat.g_xMat_texMulAdd; + materialArray[totalMaterials].roughness = mat.g_xMat_roughness; + materialArray[totalMaterials].reflectance = mat.g_xMat_reflectance; + materialArray[totalMaterials].metalness = mat.g_xMat_metalness; + materialArray[totalMaterials].emissive = mat.g_xMat_emissive; + materialArray[totalMaterials].refractionIndex = mat.g_xMat_refractionIndex; + materialArray[totalMaterials].subsurfaceScattering = mat.g_xMat_subsurfaceScattering; + materialArray[totalMaterials].normalMapStrength = mat.g_xMat_normalMapStrength; + materialArray[totalMaterials].parallaxOcclusionMapping = mat.g_xMat_normalMapStrength; // Add extended properties: const TextureDesc& desc = atlasTexture->GetDesc(); @@ -7731,13 +7504,6 @@ void BindPersistentState(GRAPHICSTHREAD threadID) } - device->BindConstantBuffer(PS, constantBuffers[CBTYPE_WORLD], CB_GETBINDSLOT(WorldCB), threadID); - device->BindConstantBuffer(VS, constantBuffers[CBTYPE_WORLD], CB_GETBINDSLOT(WorldCB), threadID); - device->BindConstantBuffer(GS, constantBuffers[CBTYPE_WORLD], CB_GETBINDSLOT(WorldCB), threadID); - device->BindConstantBuffer(HS, constantBuffers[CBTYPE_WORLD], CB_GETBINDSLOT(WorldCB), threadID); - device->BindConstantBuffer(DS, constantBuffers[CBTYPE_WORLD], CB_GETBINDSLOT(WorldCB), threadID); - device->BindConstantBuffer(CS, constantBuffers[CBTYPE_WORLD], CB_GETBINDSLOT(WorldCB), threadID); - device->BindConstantBuffer(PS, constantBuffers[CBTYPE_FRAME], CB_GETBINDSLOT(FrameCB), threadID); device->BindConstantBuffer(VS, constantBuffers[CBTYPE_FRAME], CB_GETBINDSLOT(FrameCB), threadID); device->BindConstantBuffer(GS, constantBuffers[CBTYPE_FRAME], CB_GETBINDSLOT(FrameCB), threadID); @@ -7762,67 +7528,52 @@ void BindPersistentState(GRAPHICSTHREAD threadID) device->BindConstantBuffer(VS, constantBuffers[CBTYPE_API], CB_GETBINDSLOT(APICB), threadID); device->BindConstantBuffer(PS, constantBuffers[CBTYPE_API], CB_GETBINDSLOT(APICB), threadID); } -void UpdateWorldCB(GRAPHICSTHREAD threadID) -{ - const Scene& scene = GetScene(); - - static WorldCB prevcb[GRAPHICSTHREAD_COUNT]; - - WorldCB value; - ZeroMemory(&value, sizeof(value)); - - value.g_xWorld_ScreenWidthHeight = float2((float)GetDevice()->GetScreenWidth(), (float)GetDevice()->GetScreenHeight()); - value.g_xWorld_ScreenWidthHeight_Inverse = float2(1.0f / value.g_xWorld_ScreenWidthHeight.x, 1.0f / value.g_xWorld_ScreenWidthHeight.y); - value.g_xWorld_InternalResolution = float2((float)GetInternalResolution().x, (float)GetInternalResolution().y); - value.g_xWorld_InternalResolution_Inverse = float2(1.0f / value.g_xWorld_InternalResolution.x, 1.0f / value.g_xWorld_InternalResolution.y); - value.g_xWorld_Gamma = GetGamma(); - value.g_xWorld_SunColor = scene.weather.sunColor; - value.g_xWorld_SunDirection = scene.weather.sunDirection; - value.g_xWorld_Ambient = scene.weather.ambient; - value.g_xWorld_Cloudiness = scene.weather.cloudiness; - value.g_xWorld_CloudScale = scene.weather.cloudScale; - value.g_xWorld_Fog = float3(scene.weather.fogStart, scene.weather.fogEnd, scene.weather.fogHeight); - value.g_xWorld_Horizon = scene.weather.horizon; - value.g_xWorld_Zenith = scene.weather.zenith; - value.g_xWorld_SpecularAA = SPECULARAA; - value.g_xWorld_VoxelRadianceDataSize = voxelSceneData.voxelsize; - value.g_xWorld_VoxelRadianceDataSize_Inverse = 1.0f / (float)value.g_xWorld_VoxelRadianceDataSize; - value.g_xWorld_VoxelRadianceDataRes = GetVoxelRadianceEnabled() ? (uint)voxelSceneData.res : 0; - value.g_xWorld_VoxelRadianceDataRes_Inverse = 1.0f / (float)value.g_xWorld_VoxelRadianceDataRes; - value.g_xWorld_VoxelRadianceDataMIPs = voxelSceneData.mips; - value.g_xWorld_VoxelRadianceNumCones = max(min(voxelSceneData.numCones, 16), 1); - value.g_xWorld_VoxelRadianceNumCones_Inverse = 1.0f / (float)value.g_xWorld_VoxelRadianceNumCones; - value.g_xWorld_VoxelRadianceRayStepSize = voxelSceneData.rayStepSize; - value.g_xWorld_VoxelRadianceReflectionsEnabled = voxelSceneData.reflectionsEnabled; - value.g_xWorld_VoxelRadianceDataCenter = voxelSceneData.center; - value.g_xWorld_AdvancedRefractions = GetAdvancedRefractionsEnabled() ? 1 : 0; - value.g_xWorld_EntityCullingTileCount = GetEntityCullingTileCount(); - value.g_xWorld_TransparentShadowsEnabled = TRANSPARENTSHADOWSENABLED; - value.g_xWorld_GlobalEnvProbeIndex = -1; - value.g_xWorld_EnvProbeMipCount = 0; - value.g_xWorld_EnvProbeMipCount_Inverse = 1.0f; - if (scene.probes.GetCount() > 0) - { - value.g_xWorld_GlobalEnvProbeIndex = 0; // for now, the global envprobe will be the first probe in the array. Easy change later on if required... - } - if (textures[TEXTYPE_CUBEARRAY_ENVMAPARRAY] != nullptr) - { - value.g_xWorld_EnvProbeMipCount = static_cast(textures[TEXTYPE_CUBEARRAY_ENVMAPARRAY])->GetDesc().MipLevels; - value.g_xWorld_EnvProbeMipCount_Inverse = 1.0f / (float)value.g_xWorld_EnvProbeMipCount; - } - - if (memcmp(&prevcb[threadID], &value, sizeof(WorldCB)) != 0) // prevent overcommit - { - prevcb[threadID] = value; - GetDevice()->UpdateBuffer(constantBuffers[CBTYPE_WORLD], &prevcb[threadID], threadID); - } -} void UpdateFrameCB(GRAPHICSTHREAD threadID) { const Scene& scene = GetScene(); FrameCB cb; + cb.g_xFrame_ScreenWidthHeight = float2((float)GetDevice()->GetScreenWidth(), (float)GetDevice()->GetScreenHeight()); + cb.g_xFrame_ScreenWidthHeight_Inverse = float2(1.0f / cb.g_xFrame_ScreenWidthHeight.x, 1.0f / cb.g_xFrame_ScreenWidthHeight.y); + cb.g_xFrame_InternalResolution = float2((float)GetInternalResolution().x, (float)GetInternalResolution().y); + cb.g_xFrame_InternalResolution_Inverse = float2(1.0f / cb.g_xFrame_InternalResolution.x, 1.0f / cb.g_xFrame_InternalResolution.y); + cb.g_xFrame_Gamma = GetGamma(); + cb.g_xFrame_SunColor = scene.weather.sunColor; + cb.g_xFrame_SunDirection = scene.weather.sunDirection; + cb.g_xFrame_Ambient = scene.weather.ambient; + cb.g_xFrame_Cloudiness = scene.weather.cloudiness; + cb.g_xFrame_CloudScale = scene.weather.cloudScale; + cb.g_xFrame_Fog = float3(scene.weather.fogStart, scene.weather.fogEnd, scene.weather.fogHeight); + cb.g_xFrame_Horizon = scene.weather.horizon; + cb.g_xFrame_Zenith = scene.weather.zenith; + cb.g_xFrame_SpecularAA = SPECULARAA; + cb.g_xFrame_VoxelRadianceDataSize = voxelSceneData.voxelsize; + cb.g_xFrame_VoxelRadianceDataSize_Inverse = 1.0f / (float)cb.g_xFrame_VoxelRadianceDataSize; + cb.g_xFrame_VoxelRadianceDataRes = GetVoxelRadianceEnabled() ? (uint)voxelSceneData.res : 0; + cb.g_xFrame_VoxelRadianceDataRes_Inverse = 1.0f / (float)cb.g_xFrame_VoxelRadianceDataRes; + cb.g_xFrame_VoxelRadianceDataMIPs = voxelSceneData.mips; + cb.g_xFrame_VoxelRadianceNumCones = max(min(voxelSceneData.numCones, 16), 1); + cb.g_xFrame_VoxelRadianceNumCones_Inverse = 1.0f / (float)cb.g_xFrame_VoxelRadianceNumCones; + cb.g_xFrame_VoxelRadianceRayStepSize = voxelSceneData.rayStepSize; + cb.g_xFrame_VoxelRadianceReflectionsEnabled = voxelSceneData.reflectionsEnabled; + cb.g_xFrame_VoxelRadianceDataCenter = voxelSceneData.center; + cb.g_xFrame_AdvancedRefractions = GetAdvancedRefractionsEnabled() ? 1 : 0; + cb.g_xFrame_EntityCullingTileCount = GetEntityCullingTileCount(); + cb.g_xFrame_TransparentShadowsEnabled = TRANSPARENTSHADOWSENABLED; + cb.g_xFrame_GlobalEnvProbeIndex = -1; + cb.g_xFrame_EnvProbeMipCount = 0; + cb.g_xFrame_EnvProbeMipCount_Inverse = 1.0f; + if (scene.probes.GetCount() > 0) + { + cb.g_xFrame_GlobalEnvProbeIndex = 0; // for now, the global envprobe will be the first probe in the array. Easy change later on if required... + } + if (textures[TEXTYPE_CUBEARRAY_ENVMAPARRAY] != nullptr) + { + cb.g_xFrame_EnvProbeMipCount = static_cast(textures[TEXTYPE_CUBEARRAY_ENVMAPARRAY])->GetDesc().MipLevels; + cb.g_xFrame_EnvProbeMipCount_Inverse = 1.0f / (float)cb.g_xFrame_EnvProbeMipCount; + } + cb.g_xFrame_Time = renderTime; cb.g_xFrame_TimePrev = renderTime_Prev; cb.g_xFrame_DeltaTime = deltaTime; @@ -8191,12 +7942,12 @@ void CreateImpostor(Entity entity, GRAPHICSTHREAD threadID) { MaterialCB mcb; ZeroMemory(&mcb, sizeof(mcb)); - mcb.baseColor = XMFLOAT4(1, 1, 1, 1); - mcb.texMulAdd = XMFLOAT4(1, 1, 0, 0); - mcb.normalMapStrength = 1.0f; - mcb.roughness = 1.0f; - mcb.reflectance = 1.0f; - mcb.metalness = 1.0f; + mcb.g_xMat_baseColor = XMFLOAT4(1, 1, 1, 1); + mcb.g_xMat_texMulAdd = XMFLOAT4(1, 1, 0, 0); + mcb.g_xMat_normalMapStrength = 1.0f; + mcb.g_xMat_roughness = 1.0f; + mcb.g_xMat_reflectance = 1.0f; + mcb.g_xMat_metalness = 1.0f; GPUBufferDesc bd; bd.BindFlags = BIND_CONSTANT_BUFFER; @@ -8421,16 +8172,16 @@ void CreateImpostor(Entity entity, GRAPHICSTHREAD threadID) InstancePrev instancePrev; }; UINT instancesOffset; - volatile InstBuf* buff = (volatile InstBuf*)device->AllocateFromRingBuffer(dynamicVertexBufferPool, sizeof(InstBuf), instancesOffset, threadID); + volatile InstBuf* buff = (volatile InstBuf*)device->AllocateFromRingBuffer(&dynamicVertexBufferPools[threadID], sizeof(InstBuf), instancesOffset, threadID); buff->instance.Create(__identity); buff->instancePrev.Create(__identity); - device->InvalidateBufferAccess(dynamicVertexBufferPool, threadID); + device->InvalidateBufferAccess(&dynamicVertexBufferPools[threadID], threadID); GPUBuffer* vbs[] = { mesh.IsSkinned() ? mesh.streamoutBuffer_POS.get() : mesh.vertexBuffer_POS.get(), mesh.vertexBuffer_TEX.get(), mesh.IsSkinned() ? mesh.streamoutBuffer_PRE.get() : mesh.vertexBuffer_POS.get(), - dynamicVertexBufferPool + &dynamicVertexBufferPools[threadID] }; UINT strides[] = { sizeof(MeshComponent::Vertex_POS), diff --git a/WickedEngine/wiRenderer.h b/WickedEngine/wiRenderer.h index f1e54978b..6c583296a 100644 --- a/WickedEngine/wiRenderer.h +++ b/WickedEngine/wiRenderer.h @@ -77,7 +77,6 @@ namespace wiRenderer void BindPersistentState(GRAPHICSTHREAD threadID); - void UpdateWorldCB(GRAPHICSTHREAD threadID); void UpdateFrameCB(GRAPHICSTHREAD threadID); void UpdateCameraCB(const wiSceneSystem::CameraComponent& camera, GRAPHICSTHREAD threadID); void SetClipPlane(const XMFLOAT4& clipPlane, GRAPHICSTHREAD threadID); diff --git a/WickedEngine/wiResourceManager.cpp b/WickedEngine/wiResourceManager.cpp index 8256a1f02..bcc7ba6a1 100644 --- a/WickedEngine/wiResourceManager.cpp +++ b/WickedEngine/wiResourceManager.cpp @@ -188,7 +188,9 @@ void* wiResourceManager::add(const wiHashString& name, Data_Type newType) } } - wiRenderer::GetDevice()->CreateTexture2D(&desc, InitData.data(), &image); + HRESULT hr = wiRenderer::GetDevice()->CreateTexture2D(&desc, InitData.data(), &image); + assert(SUCCEEDED(hr)); + wiRenderer::GetDevice()->SetName(image, nameStr); } @@ -228,6 +230,7 @@ void* wiResourceManager::add(const wiHashString& name, Data_Type newType) image->RequestIndependentUnorderedAccessResourcesForMIPs(true); HRESULT hr = wiRenderer::GetDevice()->CreateTexture2D(&desc, InitData, &image); assert(SUCCEEDED(hr)); + wiRenderer::GetDevice()->SetName(image, nameStr); if (image != nullptr) { diff --git a/WickedEngine/wiSceneSystem.cpp b/WickedEngine/wiSceneSystem.cpp index 3eb596d0b..440b1a488 100644 --- a/WickedEngine/wiSceneSystem.cpp +++ b/WickedEngine/wiSceneSystem.cpp @@ -814,7 +814,7 @@ namespace wiSceneSystem RunAnimationUpdateSystem(animations, transforms, dt); - wiPhysics::RunPhysicsUpdateSystem(weather, transforms, meshes, objects, rigidbodies, softbodies, dt); + wiPhysicsEngine::RunPhysicsUpdateSystem(weather, transforms, meshes, objects, rigidbodies, softbodies, dt); RunTransformUpdateSystem(transforms); @@ -1454,7 +1454,6 @@ namespace wiSceneSystem for (size_t i = 0; i < armatures.GetCount(); ++i) { ArmatureComponent& armature = armatures[i]; - Entity entity = armatures.GetEntity(i); if (armature.skinningMatrices.size() != armature.boneCollection.size()) { @@ -1518,7 +1517,6 @@ namespace wiSceneSystem for (size_t i = 0; i < objects.GetCount(); ++i) { ObjectComponent& object = objects[i]; - Entity entity = objects.GetEntity(i); AABB& aabb = aabb_objects[i]; aabb = AABB(); @@ -1528,6 +1526,7 @@ namespace wiSceneSystem if (object.meshID != INVALID_ENTITY) { + Entity entity = objects.GetEntity(i); const TransformComponent* transform = transforms.GetComponent(entity); const MeshComponent* mesh = meshes.GetComponent(object.meshID);