调通usf调用,将usf输出添加到材质。但是现在usf正式算法的输出值无法产生形变

This commit is contained in:
2025-11-12 00:17:23 +08:00
parent f12fef4edd
commit b027ec6487
12 changed files with 275 additions and 16 deletions
+2 -1
View File
@@ -23,7 +23,8 @@ public class BladeSmith : ModuleRules
"PhysicsCore",
"AudioExtensions",
"AudioMixer",
"AudioPlatformConfiguration"
"AudioPlatformConfiguration",
"MiceShaders"
});
}
@@ -1,34 +0,0 @@
#include "FDeformComputeShader.h"
#include "GlobalShader.h"
#include "ShaderParameterStruct.h"
#include "RHIStaticStates.h"
#include "Shader.h"
#include "GlobalShader.h"
#include "RHICommandList.h"
#include "RHIResources.h"
#include "ShaderParameterUtils.h"
// 全局参数存储
FThreadSafeDeformationParams GDeformationParams;
// 实现Compute Shader
IMPLEMENT_GLOBAL_SHADER(FDeformComputeShader, "/Engine/Private/MiceCola/DeformationCS.usf", "MainCompute", SF_Compute);
// 线程安全参数管理
void FThreadSafeDeformationParams::AddParams(const FDeformationCSDispatchParams& NewParams)
{
FScopeLock Lock(&ParamsCriticalSection);
PendingParams.Add(NewParams);
}
bool FThreadSafeDeformationParams::ConsumeParams(FDeformationCSDispatchParams& OutParams)
{
FScopeLock Lock(&ParamsCriticalSection);
if (PendingParams.Num() > 0)
{
OutParams = PendingParams[0];
PendingParams.RemoveAt(0);
return true;
}
return false;
}
-104
View File
@@ -1,104 +0,0 @@
#pragma once
#include "CoreMinimal.h"
#include "GlobalShader.h"
#include "ShaderParameterStruct.h"
#include "RenderGraphUtils.h" // 必须包含
// Compute Shader参数结构体
struct FDeformationCSDispatchParams
{
// 碰撞参数
FVector ImpactPoint;
FVector ImpactNormal;
float ImpactForce;
float DeformationRadius;
float Plasticity;
// 纹理资源
FTextureRHIRef InputTexture;
FUnorderedAccessViewRHIRef OutputTexture;
// 更新区域 (优化部分更新)
FIntRect UpdateRegion;
// 纹理尺寸
FIntVector TextureSize;
FDeformationCSDispatchParams() : ImpactPoint(0,0,0),
ImpactNormal(0,0,0),
ImpactForce(0.0f),
DeformationRadius(10.0f),
Plasticity(0.7f),
TextureSize(FIntVector::ZeroValue)
{
}
};
// 支持多实例的线程安全参数
struct FThreadSafeDeformationParams
{
FCriticalSection ParamsCriticalSection;
TArray<FDeformationCSDispatchParams> PendingParams;
void AddParams(const FDeformationCSDispatchParams& NewParams);
bool ConsumeParams(FDeformationCSDispatchParams& OutParams);
};
// 全局参数存储
extern FThreadSafeDeformationParams GDeformationParams;
// DeformationCS.h (续)
// Compute Shader类
class FDeformComputeShader : public FGlobalShader
{
DECLARE_GLOBAL_SHADER(FDeformComputeShader);
SHADER_USE_PARAMETER_STRUCT(FDeformComputeShader, FGlobalShader);
BEGIN_SHADER_PARAMETER_STRUCT(FParameters, )
// 碰撞参数
SHADER_PARAMETER(FVector3f, ImpactPoint)
SHADER_PARAMETER(float, ImpactForce)
SHADER_PARAMETER(FVector3f, ImpactNormal)
SHADER_PARAMETER(float, DeformationRadius)
SHADER_PARAMETER(float, Plasticity)
// 纹理参数
SHADER_PARAMETER_RDG_TEXTURE_SRV(Texture2D, InputTexture)
SHADER_PARAMETER_RDG_TEXTURE_UAV(RWTexture2D<float4>, OutputTexture)
// 更新区域
SHADER_PARAMETER(FIntVector, TextureSize)
SHADER_PARAMETER(FIntVector4, UpdateRegionParam)
END_SHADER_PARAMETER_STRUCT()
static bool ShouldCompilePermutation(const FGlobalShaderPermutationParameters& Parameters)
{
return GetMaxSupportedFeatureLevel(Parameters.Platform) >= ERHIFeatureLevel::SM5;
}
// 2. ✨ 修复 Precache 错误的关键:实现 ShouldPrecachePermutation
// 我们告诉引擎:在开发模式下,请始终预缓存这个着色器。
static EShaderPermutationPrecacheRequest ShouldPrecachePermutation(const FGlobalShaderPermutationParameters& Parameters)
{
// 如果你的着色器没有使用任何模板或标志,只需返回 E_DEVELOPMENT 或 E_ALL。
return EShaderPermutationPrecacheRequest::Precached;
}
static bool ShouldCache(EShaderPlatform Platform)
{
return true;
}
// (假设你的 .usf 文件中线程组大小为 8x8x1)
static FIntVector GetGroupSize()
{
return FIntVector(8, 8, 1);
}
static void ModifyCompilationEnvironment(const FGlobalShaderPermutationParameters& Parameters, FShaderCompilerEnvironment& OutEnvironment)
{
FGlobalShader::ModifyCompilationEnvironment(Parameters, OutEnvironment);
OutEnvironment.SetDefine(TEXT("THREADGROUP_SIZE_X"), 8);
OutEnvironment.SetDefine(TEXT("THREADGROUP_SIZE_Y"), 8);
}
};