diff --git a/.vsconfig b/.vsconfig index b3c233d..3b919ea 100644 --- a/.vsconfig +++ b/.vsconfig @@ -1,10 +1,12 @@ { "version": "1.0", "components": [ + "Component.Unreal.Debugger", + "Component.Unreal.Ide", "Microsoft.Net.Component.4.6.2.TargetingPack", - "Microsoft.VisualStudio.Component.Unreal.Workspace", "Microsoft.VisualStudio.Component.VC.14.38.17.8.ATL", "Microsoft.VisualStudio.Component.VC.14.38.17.8.x86.x64", + "Microsoft.VisualStudio.Component.VC.Llvm.Clang", "Microsoft.VisualStudio.Component.VC.Tools.x86.x64", "Microsoft.VisualStudio.Component.Windows11SDK.22621", "Microsoft.VisualStudio.Workload.CoreEditor", diff --git a/BladeSmith.uproject b/BladeSmith.uproject index 511e59d..4134dc4 100644 --- a/BladeSmith.uproject +++ b/BladeSmith.uproject @@ -1,6 +1,6 @@ { "FileVersion": 3, - "EngineAssociation": "5.5", + "EngineAssociation": "5.6", "Category": "", "Description": "", "Modules": [ diff --git a/Source/BladeSmith/IronBlock.cpp b/Source/BladeSmith/IronBlock.cpp index 456ff25..5ee46be 100644 --- a/Source/BladeSmith/IronBlock.cpp +++ b/Source/BladeSmith/IronBlock.cpp @@ -104,20 +104,21 @@ UTextureRenderTarget2D* AIronBlock::CreateDeformRT(const FString& RTName, int32 if (RenderTarget) { // **关键修复 1:** 设置为32位浮点数格式,以匹配 FLinearColor 和 CS 的 PF_A32B32G32R32F - RenderTarget->RenderTargetFormat = RTF_RGBA32f; + //RenderTarget->RenderTargetFormat = RTF_RGBA32f; // **关键修复 2:** 明确告诉引擎我们需要为这个纹理创建UAV RenderTarget->bCanCreateUAV = true; + RenderTarget->InitCustomFormat(Size, Size, PF_A32B32G32R32F, false); RenderTarget->bNeedsTwoCopies = true; // 告知引擎这个纹理会被用于Ping-Pong,优化内存 - RenderTarget->SizeX = Size; - RenderTarget->SizeY = Size; + //RenderTarget->SizeX = Size; + //RenderTarget->SizeY = Size; RenderTarget->bAutoGenerateMips = false; RenderTarget->AddressX = TA_Clamp; RenderTarget->AddressY = TA_Clamp; // 立即创建RHI资源 - RenderTarget->UpdateResource(); + //RenderTarget->UpdateResource(); } return RenderTarget; } @@ -282,7 +283,7 @@ void AIronBlock::OnHammerHit(const FHitResult& HitResult) UpdateComputeShaderParams(LocalImpactPoint, LocalImpactNormal, ImpactForce); - ExecuteDeformationCS(); + ExecuteDeformationCS(); } @@ -325,23 +326,13 @@ void AIronBlock::ExecuteDeformationCS() UTextureRenderTarget2D* ReadTarget = (CurrentTextureIndex == 0) ? PositionRenderTarget0 : PositionRenderTarget1; UTextureRenderTarget2D* WriteTarget = (CurrentTextureIndex == 1) ? PositionRenderTarget0 : PositionRenderTarget1; // Flipped - // --- 关键修复: 直接获取 RT 的 RHI 和 UAV --- + // --- 关键修复: 在游戏线程只获取 Input RHI Texture 句柄 --- FTextureRHIRef InputRHI = GetRenderTargetRHI(ReadTarget); - // 从 WriteTarget 获取 UAV。因为我们创建时设置了 bCanCreateUAV=true,所以这个UAV是有效的。 - FUnorderedAccessViewRHIRef OutputUAV = nullptr; - if (WriteTarget && WriteTarget->GetResource()) - { - FTextureResource* RTResource = WriteTarget->GetResource(); - FTextureRenderTargetResource* WriteRTResource = static_cast(RTResource); - if (WriteRTResource) - { - OutputUAV = WriteRTResource->GetRenderTargetUAV(); - } - } - if (!InputRHI || !OutputUAV) + // 检查 Input RHI 和 WriteTarget UObject 是否有效 + if (!InputRHI.IsValid() || !WriteTarget) { - UE_LOG(LogTemp, Error, TEXT("Invalid texture RHI resources or UAV!")); + UE_LOG(LogTemp, Error, TEXT("Invalid texture RHI resources or WriteTarget UObject!")); return; } @@ -372,16 +363,54 @@ void AIronBlock::ExecuteDeformationCS() Params.DeformationRadius = CurrentDeformationRadius; Params.Plasticity = CurrentPlasticity; Params.InputTexture = InputRHI; - Params.OutputTexture = OutputUAV; // **直接使用RT的UAV** + //Params.OutputTexture = OutputUAV; // **直接使用RT的UAV** Params.UpdateRegion = UpdateRegion; Params.TextureSize = FIntVector(TexSize, TexSize, 1); // 在渲染线程中执行Compute Shader ENQUEUE_RENDER_COMMAND(ExecuteDeformationCS)( - [Params](FRHICommandListImmediate& RHICmdList) + [Params, WriteTarget](FRHICommandListImmediate& RHICmdList) { - // 在这里调用你的 Compute Shader 调度逻辑 - FDeformationCS::Dispatch(RHICmdList, Params); + // --- 现在我们在渲染线程上 --- + + // 1. 从 UObject 获取 FTextureResource + FTextureResource* RTResource = WriteTarget->GetResource(); + if (!RTResource) + { + UE_LOG(LogTemp, Error, TEXT("Failed to get WriteTarget resource on render thread!")); + return; + } + + // 2. 转换为 FTextureRenderTargetResource + FTextureRenderTargetResource* WriteRTResource = static_cast(RTResource); + if (!WriteRTResource) + { + UE_LOG(LogTemp, Error, TEXT("Failed to cast to FTextureRenderTargetResource on render thread!")); + return; + } + + // 3. ✨ 在渲染线程上调用 GetRenderTargetUAV() + FUnorderedAccessViewRHIRef OutputUAV = WriteRTResource->GetRenderTargetUAV(); + + // 4. 检查是否成功 + if (!OutputUAV.IsValid()) + { + // 如果在这里仍然失败,说明 bCanCreateUAV=true 没有生效, + // 或者 RHI 后端没有为这个 RT 创建 UAV。 + UE_LOG(LogTemp, Error, TEXT("GetRenderTargetUAV() returned null ON RENDER THREAD!")); + return; + } + + // 5. 填充参数并调度 + FDeformationCSDispatchParams RenderParams = Params; + RenderParams.OutputTexture = OutputUAV; + + FDeformationCS::Dispatch(RHICmdList, RenderParams); + + // 6. (重要) 添加一个UAV屏障 + // 告诉GPU,Compute Shader对UAV的写入操作已完成, + // 在下一帧中,图形管线(材质)可以安全地将其作为 SRV (TextureSample) 读取。 + RHICmdList.Transition(FRHITransitionInfo(OutputUAV, ERHIAccess::UAVCompute, ERHIAccess::SRVGraphics)); }); // 等待渲染命令完成,以便在CPU上读取 (如果需要立即更新碰撞体)