From 3c0aaf5138bb819c1f39db7005df1ed302b11f3b Mon Sep 17 00:00:00 2001 From: troublemaker52025 Date: Fri, 8 Sep 2023 14:53:38 +0800 Subject: [PATCH 1/5] fix debugView UBO data for native custom pipeline (#16218) --- cocos/rendering/pipeline-ubo.ts | 41 +++++++------- .../pipeline/custom/NativeBuiltinUtils.cpp | 54 ++++++------------- 2 files changed, 39 insertions(+), 56 deletions(-) diff --git a/cocos/rendering/pipeline-ubo.ts b/cocos/rendering/pipeline-ubo.ts index 38bfb231123..81c84ebcf3f 100644 --- a/cocos/rendering/pipeline-ubo.ts +++ b/cocos/rendering/pipeline-ubo.ts @@ -36,6 +36,7 @@ import { builtinResMgr } from '../asset/asset-manager/builtin-res-mgr'; import { Texture2D } from '../asset/assets'; import { DebugViewCompositeType } from './debug-view'; import { getDescBindingFromName } from './custom/define'; +import { Root } from '../root'; const _matShadowView = new Mat4(); const _matShadowProj = new Mat4(); @@ -47,7 +48,7 @@ const _tempVec3 = new Vec3(); export class PipelineUBO { public static updateGlobalUBOView (window: RenderWindow, bufferView: Float32Array): void { const director = cclegacy.director; - const root = director.root; + const root = director.root as Root; const fv = bufferView; const shadingWidth = Math.floor(window.width); @@ -75,19 +76,21 @@ export class PipelineUBO { } const debugView = root.debugView; - fv[UBOGlobal.DEBUG_VIEW_MODE_OFFSET] = debugView.singleMode as number; - - for (let i = 1; i <= 3; i++) { + for (let i = 0; i <= 3; i++) { fv[UBOGlobal.DEBUG_VIEW_MODE_OFFSET + i] = 0.0; } - for (let i = DebugViewCompositeType.DIRECT_DIFFUSE as number; i < DebugViewCompositeType.MAX_BIT_COUNT; i++) { - const offset = i >> 3; - const bit = i % 8; - fv[UBOGlobal.DEBUG_VIEW_MODE_OFFSET + 1 + offset] += (debugView.isCompositeModeEnabled(i) ? 1.0 : 0.0) * (10.0 ** bit); - } + if (debugView.isEnabled()) { + fv[UBOGlobal.DEBUG_VIEW_MODE_OFFSET] = debugView.singleMode as number; + + for (let i = DebugViewCompositeType.DIRECT_DIFFUSE as number; i < (DebugViewCompositeType.MAX_BIT_COUNT as unknown as number); i++) { + const offset = i >> 3; + const bit = i % 8; + fv[UBOGlobal.DEBUG_VIEW_MODE_OFFSET + 1 + offset] += (debugView.isCompositeModeEnabled(i) ? 1.0 : 0.0) * (10.0 ** bit); + } - fv[UBOGlobal.DEBUG_VIEW_MODE_OFFSET + 3] += (debugView.lightingWithAlbedo ? 1.0 : 0.0) * (10.0 ** 6.0); - fv[UBOGlobal.DEBUG_VIEW_MODE_OFFSET + 3] += (debugView.csmLayerColoration ? 1.0 : 0.0) * (10.0 ** 7.0); + fv[UBOGlobal.DEBUG_VIEW_MODE_OFFSET + 3] += (debugView.lightingWithAlbedo ? 1.0 : 0.0) * (10.0 ** 6.0); + fv[UBOGlobal.DEBUG_VIEW_MODE_OFFSET + 3] += (debugView.csmLayerColoration ? 1.0 : 0.0) * (10.0 ** 7.0); + } } public static updateCameraUBOView ( @@ -96,7 +99,7 @@ export class PipelineUBO { camera: Camera, ): void { const scene = camera.scene ? camera.scene : cclegacy.director.getScene().renderScene; - const mainLight = scene.mainLight; + const mainLight = scene.mainLight as DirectionalLight; const sceneData = pipeline.pipelineSceneData; const ambient = sceneData.ambient; const skybox = sceneData.skybox; @@ -339,9 +342,9 @@ export class PipelineUBO { if (shadowInfo.type === ShadowType.ShadowMap) { let near = 0.1; let far = 0; - let matShadowView; - let matShadowProj; - let matShadowViewProj; + let matShadowView: Mat4; + let matShadowProj: Mat4; + let matShadowViewProj: Mat4; let levelCount = 0; if (mainLight.shadowFixedArea || mainLight.csmLevel === CSMLevel.LEVEL_1 || !csmSupported) { matShadowView = csmLayers.specialLayer.matShadowView; @@ -403,10 +406,10 @@ export class PipelineUBO { Mat4.perspective( _matShadowProj, - (light as any).angle, + spotLight.angle, 1.0, 0.001, - (light as any).range, + spotLight.range, true, cap.clipSpaceMinZ, cap.clipSpaceSignY, @@ -564,6 +567,6 @@ export class PipelineUBO { } } - public destroy (): void { - } + // eslint-disable-next-line @typescript-eslint/no-empty-function + public destroy (): void {} } diff --git a/native/cocos/renderer/pipeline/custom/NativeBuiltinUtils.cpp b/native/cocos/renderer/pipeline/custom/NativeBuiltinUtils.cpp index 07c7d0aaa33..fabf92f5d61 100644 --- a/native/cocos/renderer/pipeline/custom/NativeBuiltinUtils.cpp +++ b/native/cocos/renderer/pipeline/custom/NativeBuiltinUtils.cpp @@ -42,10 +42,10 @@ namespace cc { namespace render { void setupQuadVertexBuffer(gfx::Device &device, const Vec4 &viewport, float vbData[16]) { - auto minX = static_cast(viewport.x); - auto maxX = static_cast(viewport.x + viewport.z); - auto minY = static_cast(viewport.y); - auto maxY = static_cast(viewport.y + viewport.w); + const float minX = viewport.x; + const float maxX = viewport.x + viewport.z; + float minY = viewport.y; + float maxY = viewport.y + viewport.w; if (device.getCapabilities().screenSpaceSignY > 0) { std::swap(minY, maxY); } @@ -87,43 +87,23 @@ void updateRasterPassConstants(uint32_t width, uint32_t height, Setter &setter) setter.setVec4( "cc_nativeSize", Vec4(shadingWidth, shadingHeight, 1.0F / shadingWidth, 1.0F / shadingHeight)); -#if 0 + const auto *debugView = root.getDebugView(); - if (debugView) { - setter.setVec4( - "cc_debug_view_mode", - Vec4(static_cast(debugView->getSingleMode()), - debugView->isLightingWithAlbedo() ? 1.0F : 0.0F, - debugView->isCsmLayerColoration() ? 1.0F : 0.0F, - 0.0F)); - Vec4 debugPackVec{}; - for (auto i = static_cast(pipeline::DebugViewCompositeType::DIRECT_DIFFUSE); - i < static_cast(pipeline::DebugViewCompositeType::MAX_BIT_COUNT); ++i) { - const auto idx = i % 4; - (&debugPackVec.x)[idx] = debugView->isCompositeModeEnabled(i) ? 1.0F : 0.0F; - const auto packIdx = static_cast(floor(static_cast(i) / 4.0F)); - if (idx == 3) { - std::string name("cc_debug_view_composite_pack_"); - name.append(std::to_string(packIdx + 1)); - setter.setVec4(name, debugPackVec); - } - } - } else { - setter.setVec4("cc_debug_view_mode", Vec4(0.0F, 1.0F, 0.0F, 0.0F)); - Vec4 debugPackVec{}; + float debugViewData[4] = {0.0F, 0.0F, 0.0F, 0.0F}; + if (debugView && debugView->isEnabled()) { + debugViewData[0] = static_cast(debugView->getSingleMode()); for (auto i = static_cast(pipeline::DebugViewCompositeType::DIRECT_DIFFUSE); i < static_cast(pipeline::DebugViewCompositeType::MAX_BIT_COUNT); ++i) { - const auto idx = i % 4; - (&debugPackVec.x)[idx] = 1.0F; - const auto packIdx = static_cast(floor(i / 4.0)); - if (idx == 3) { - std::string name("cc_debug_view_composite_pack_"); - name.append(std::to_string(packIdx + 1)); - setter.setVec4(name, debugPackVec); - } + const uint32_t offset = i >> 3; + const uint32_t bit = i % 8; + debugViewData[1 + offset] += (debugView->isCompositeModeEnabled(i) ? 1.0F : 0.0F) * powf(10.0F, static_cast(bit)); } + debugViewData[3] += (debugView->isLightingWithAlbedo() ? 1.0F : 0.0F) * powf(10.0F, static_cast(6)); + debugViewData[3] += (debugView->isCsmLayerColoration() ? 1.0F : 0.0F) * powf(10.0F, static_cast(7)); } -#endif + setter.setVec4( + "cc_debug_view_mode", + Vec4(debugViewData[0], debugViewData[1], debugViewData[2], debugViewData[3])); } namespace { @@ -373,7 +353,7 @@ void setShadowUBOView( } } } else { - Vec3 tempVec3 = shadowInfo.getNormal().getNormalized(); + const Vec3 tempVec3 = shadowInfo.getNormal().getNormalized(); setVec4Impl(data, layoutGraph, "cc_planarNDInfo", Vec4(tempVec3.x, tempVec3.y, tempVec3.z, -shadowInfo.getDistance())); From 794b8b3ccfc585963e6d2cdd4d295016719cfb71 Mon Sep 17 00:00:00 2001 From: troublemaker52025 Date: Fri, 8 Sep 2023 18:53:16 +0800 Subject: [PATCH 2/5] Revert "Resolved error when getting length for Record type (#16213)" (#16233) --- cocos/render-scene/scene/submodel.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/cocos/render-scene/scene/submodel.ts b/cocos/render-scene/scene/submodel.ts index 66a026c2fb8..a1e51f42617 100644 --- a/cocos/render-scene/scene/submodel.ts +++ b/cocos/render-scene/scene/submodel.ts @@ -333,12 +333,10 @@ export class SubModel { if (!this._globalPatches && pipelinePatches.length === 0) { return; } else if (pipelinePatches.length) { - if (this._globalPatches) { + if (this._globalPatches && pipelinePatches.length === this._globalPatches.length) { const globalPatches = Object.entries(this._globalPatches); - if (pipelinePatches.length === globalPatches.length) { - const patchesStateUnchanged = JSON.stringify(pipelinePatches.sort()) === JSON.stringify(globalPatches.sort()); - if (patchesStateUnchanged) return; - } + const patchesStateUnchanged = JSON.stringify(pipelinePatches.sort()) === JSON.stringify(globalPatches.sort()); + if (patchesStateUnchanged) return; } } this._globalPatches = pipeline.macros; From 290746adb0889e63ba2e23ce9e42af7db252fbb9 Mon Sep 17 00:00:00 2001 From: qiuguohua Date: Mon, 11 Sep 2023 15:07:02 +0800 Subject: [PATCH 3/5] Fixed the problem of incorrect display position of the emulator. (#16238) --- native/cocos/platform/mac/View.h | 2 +- native/cocos/platform/mac/View.mm | 16 ++++++++++++++++ .../cocos/platform/mac/modules/SystemWindow.mm | 6 +++--- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/native/cocos/platform/mac/View.h b/native/cocos/platform/mac/View.h index 2e9cec833c3..adc21ab9d94 100644 --- a/native/cocos/platform/mac/View.h +++ b/native/cocos/platform/mac/View.h @@ -28,7 +28,7 @@ #import @interface View : NSView - +- (int)getWindowId; @property (nonatomic, assign) id device; @end diff --git a/native/cocos/platform/mac/View.mm b/native/cocos/platform/mac/View.mm index 31b631a5e05..160d2fa2410 100644 --- a/native/cocos/platform/mac/View.mm +++ b/native/cocos/platform/mac/View.mm @@ -35,6 +35,20 @@ of this software and associated engine source code (the "Software"), a limited, #import "platform/mac/modules/SystemWindow.h" #import "platform/mac/modules/SystemWindowManager.h" +#include "SDL2/SDL.h" + +static int MetalViewEventWatch(void* userData, SDL_Event*event) { + if (event->type == SDL_WINDOWEVENT && event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) { + @autoreleasepool { + auto *view = (__bridge View *)userData; + if ([view getWindowId] == event->window.windowID) { + [view viewDidChangeBackingProperties]; + } + } + } + return 0; +} + @implementation View { cc::MouseEvent _mouseEvent; cc::KeyboardEvent _keyboardEvent; @@ -50,6 +64,8 @@ - (CALayer *)makeBackingLayer { - (instancetype)initWithFrame:(NSRect)frameRect { if (self = [super initWithFrame:frameRect]) { + // View is used as a subview, so the resize message needs to be handled manually. + SDL_AddEventWatch(MetalViewEventWatch, (__bridge void*)(self)); [self.window makeFirstResponder:self]; int pixelRatio = [[NSScreen mainScreen] backingScaleFactor]; CGSize size = CGSizeMake(frameRect.size.width * pixelRatio, frameRect.size.height * pixelRatio); diff --git a/native/cocos/platform/mac/modules/SystemWindow.mm b/native/cocos/platform/mac/modules/SystemWindow.mm index 42a4ed8ad8b..310082a1bfd 100644 --- a/native/cocos/platform/mac/modules/SystemWindow.mm +++ b/native/cocos/platform/mac/modules/SystemWindow.mm @@ -24,6 +24,7 @@ of this software and associated documentation files (the "Software"), to deal #include "platform/mac/modules/SystemWindow.h" #include "platform/mac/View.h" + #include "base/Log.h" #include "base/Macros.h" @@ -57,15 +58,14 @@ of this software and associated documentation files (the "Software"), to deal void SystemWindow::initWindowProperty(SDL_Window* window, const char *title, int x, int y, int w, int h) { CC_ASSERT(window != nullptr); auto* nsWindow = reinterpret_cast(SDLHelper::getWindowHandle(window)); - NSRect rect = NSMakeRect(x, y, w, h); NSString *astring = [NSString stringWithUTF8String:title]; nsWindow.title = astring; + // contentView is created internally by sdl. NSView *view = nsWindow.contentView; - auto* newView = [[View alloc] initWithFrame:rect]; + auto* newView = [[View alloc] initWithFrame:view.frame]; [view addSubview:newView]; [nsWindow.contentView setWantsBestResolutionOpenGLSurface:YES]; [nsWindow makeKeyAndOrderFront:nil]; - _windowHandle = reinterpret_cast(nsWindow.contentView) ; auto dpr = [nsWindow backingScaleFactor]; From 608660ae604b5649d250ae589913baf16a697ca8 Mon Sep 17 00:00:00 2001 From: qiuguohua Date: Fri, 15 Sep 2023 10:19:08 +0800 Subject: [PATCH 4/5] Fixed multi-threading causing view read failure (#16253) --- native/cocos/platform/mac/modules/Screen.mm | 6 +++--- native/cocos/platform/mac/modules/SystemWindow.mm | 2 +- native/cocos/renderer/gfx-metal/MTLSwapchain.mm | 7 ------- 3 files changed, 4 insertions(+), 11 deletions(-) diff --git a/native/cocos/platform/mac/modules/Screen.mm b/native/cocos/platform/mac/modules/Screen.mm index e2049c8f135..10a3de9a762 100644 --- a/native/cocos/platform/mac/modules/Screen.mm +++ b/native/cocos/platform/mac/modules/Screen.mm @@ -32,7 +32,7 @@ of this software and associated engine source code (the "Software"), a limited, #include "base/Macros.h" #include "cocos/bindings/jswrapper/SeApi.h" #include "platform/interfaces/modules/ISystemWindowManager.h" -#include "platform/interfaces/modules/ISystemWindow.h" +#include "platform/mac/modules/SystemWindow.h" #include "application/ApplicationManager.h" namespace cc { @@ -52,8 +52,8 @@ of this software and associated engine source code (the "Software"), a limited, global->getProperty("devicePixelRatio", &devicePixelRatioVal); return devicePixelRatioVal.isNumber() ? devicePixelRatioVal.toFloat() : 1.F; #else - auto* window = CC_GET_MAIN_SYSTEM_WINDOW(); - NSWindow* nsWindow = reinterpret_cast(window->getWindowHandle()) ; + auto* systemWindow = static_cast(CC_GET_MAIN_SYSTEM_WINDOW()); + auto* nsWindow = systemWindow->getNSWindow(); return [nsWindow backingScaleFactor]; #endif } diff --git a/native/cocos/platform/mac/modules/SystemWindow.mm b/native/cocos/platform/mac/modules/SystemWindow.mm index 310082a1bfd..4575cf39e41 100644 --- a/native/cocos/platform/mac/modules/SystemWindow.mm +++ b/native/cocos/platform/mac/modules/SystemWindow.mm @@ -66,7 +66,7 @@ of this software and associated documentation files (the "Software"), to deal [view addSubview:newView]; [nsWindow.contentView setWantsBestResolutionOpenGLSurface:YES]; [nsWindow makeKeyAndOrderFront:nil]; - _windowHandle = reinterpret_cast(nsWindow.contentView) ; + _windowHandle = reinterpret_cast(newView); auto dpr = [nsWindow backingScaleFactor]; _width = w * dpr; diff --git a/native/cocos/renderer/gfx-metal/MTLSwapchain.mm b/native/cocos/renderer/gfx-metal/MTLSwapchain.mm index 313f0c33edb..0fbbce7202a 100644 --- a/native/cocos/renderer/gfx-metal/MTLSwapchain.mm +++ b/native/cocos/renderer/gfx-metal/MTLSwapchain.mm @@ -61,13 +61,6 @@ of this software and associated engine source code (the "Software"), a limited, if (!layer.device) { layer.device = MTLCreateSystemDefaultDevice(); } -#elif CC_PLATFORM == CC_PLATFORM_MACOS - CAMetalLayer *layer = nullptr; - auto *view = (CCView *)info.windowHandle; - if(view && view.subviews.count > 0) { - NSView* subView = view.subviews[0]; - layer = static_cast(subView.layer); - } #else auto *view = (CCView *)info.windowHandle; CAMetalLayer *layer = static_cast(view.layer); From dae0f55fec9aa35b5496e55f8aff15fea21eaa82 Mon Sep 17 00:00:00 2001 From: jk20012001 Date: Tue, 19 Sep 2023 15:41:49 +0800 Subject: [PATCH 5/5] fix albedoScaleAndCutOff type mismatch (#16282) --- .../util/dcc/imported-metallic-roughness.effect | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/editor/assets/effects/util/dcc/imported-metallic-roughness.effect b/editor/assets/effects/util/dcc/imported-metallic-roughness.effect index a385d707dde..2ab10f99fb8 100644 --- a/editor/assets/effects/util/dcc/imported-metallic-roughness.effect +++ b/editor/assets/effects/util/dcc/imported-metallic-roughness.effect @@ -25,7 +25,7 @@ CCEffect %{ emissiveMap: { value: grey, editor: { displayName: EmissiveColorMap } } alphaSource: { value: 1.0, editor: { slide: true, range: [0, 1.0], step: 0.001 } } alphaSourceMap: { value: grey, editor: { parent: USE_OPACITY_MAP } } - alphaThreshold: { value: 0.5, target: albedoScaleAndCutoff.w, editor: { parent: USE_ALPHA_TEST, slide: true, range: [0, 1.0], step: 0.001 } } + alphaThreshold: { value: 0.5, editor: { parent: USE_ALPHA_TEST, slide: true, range: [0, 1.0], step: 0.001 } } normalStrength: { value: 1.0, editor: { parent: USE_NORMAL_MAP, slide: true, range: [0, 1.0], step: 0.001 } } normalMap: { value: normal } - &forward-add @@ -55,8 +55,8 @@ CCEffect %{ properties: tilingOffset: { value: [1.0, 1.0, 0.0, 0.0] } mainColor: { value: [1.0, 1.0, 1.0, 1.0], target: albedo, editor: { displayName: Albedo, type: color } } - albedoScale: { value: [1.0, 1.0, 1.0], target: albedoScaleAndCutoff.xyz } - alphaThreshold: { value: 0.5, target: albedoScaleAndCutoff.w, editor: { parent: USE_ALPHA_TEST } } + albedoScale: { value: 1.0, editor: { displayName: BaseWeight } } + alphaThreshold: { value: 0.5, editor: { parent: USE_ALPHA_TEST } } mainTexture: { value: grey, target: albedoMap, editor: { displayName: AlbedoMap } } alphaSource: { value: 1.0, editor: { slide: true, range: [0, 1.0], step: 0.001 } } alphaSourceMap: { value: grey, editor: { parent: USE_OPACITY_MAP } } @@ -96,7 +96,6 @@ CCProgram shared-ubos %{ uniform Constants { vec4 tilingOffset; vec4 albedo; - vec4 albedoScaleAndCutoff; vec4 emissive; float emissiveScale; float occlusion; @@ -105,6 +104,7 @@ CCProgram shared-ubos %{ float normalStrength; float alphaSource; float albedoScale; + float alphaThreshold; float specularIntensity; }; }% @@ -237,7 +237,7 @@ CCProgram surface-fragment %{ #endif #endif #if USE_ALPHA_TEST - if (baseColor.a < albedoScaleAndCutoff.w) discard; + if (baseColor.a < alphaThreshold) discard; #endif return baseColor; } @@ -265,7 +265,7 @@ CCProgram surface-fragment %{ #endif #endif - if (alpha < albedoScaleAndCutoff.w) discard; + if (alpha < alphaThreshold) discard; #endif }