From 9cac65445110b3111ce74e6cf7ea80304f9a5f92 Mon Sep 17 00:00:00 2001 From: jklawreszuk Date: Thu, 24 Oct 2024 17:38:15 +0200 Subject: [PATCH] fix: Enable to build SkyBoxAsset using OpenGL --- .../Skyboxes/SkyboxAssetCompiler.cs | 2 +- .../OpenGL/GraphicsDevice.OpenGL.cs | 14 ++--- .../Stride.Graphics/OpenGL/Texture.OpenGL.cs | 6 +-- sources/engine/Stride.Graphics/Texture.cs | 9 ++-- .../GGXPrefiltering/Hammersley.sdsl | 2 +- .../RadiancePrefilteringGGXNoCompute.cs | 54 +++++++++---------- 6 files changed, 40 insertions(+), 47 deletions(-) diff --git a/sources/engine/Stride.Assets/Skyboxes/SkyboxAssetCompiler.cs b/sources/engine/Stride.Assets/Skyboxes/SkyboxAssetCompiler.cs index a99d7b1715..9ec498b49d 100644 --- a/sources/engine/Stride.Assets/Skyboxes/SkyboxAssetCompiler.cs +++ b/sources/engine/Stride.Assets/Skyboxes/SkyboxAssetCompiler.cs @@ -73,7 +73,7 @@ protected override void Prepare(AssetCompilerContext context, AssetItem assetIte var textureAssetItem = new AssetItem(textureUrl, textureAsset); // Create and add the texture command. - var textureParameters = new TextureConvertParameters(assetSource, textureAsset, PlatformType.Windows, GraphicsPlatform.Direct3D11, graphicsProfile, gameSettingsAsset.GetOrCreate().TextureQuality, colorSpace); + var textureParameters = new TextureConvertParameters(assetSource, textureAsset, context.Platform, context.Platform.GetDefaultGraphicsPlatform(), graphicsProfile, gameSettingsAsset.GetOrCreate().TextureQuality, colorSpace); var prereqStep = new AssetBuildStep(textureAssetItem); prereqStep.Add(new TextureAssetCompiler.TextureConvertCommand(textureUrl, textureParameters, assetItem.Package)); result.BuildSteps.Add(prereqStep); diff --git a/sources/engine/Stride.Graphics/OpenGL/GraphicsDevice.OpenGL.cs b/sources/engine/Stride.Graphics/OpenGL/GraphicsDevice.OpenGL.cs index ca47aebae3..411ed2bc80 100644 --- a/sources/engine/Stride.Graphics/OpenGL/GraphicsDevice.OpenGL.cs +++ b/sources/engine/Stride.Graphics/OpenGL/GraphicsDevice.OpenGL.cs @@ -658,23 +658,23 @@ protected unsafe void InitializePlatformDevice(GraphicsProfile[] graphicsProfile #endif #if STRIDE_UI_SDL - gameWindow = (Stride.Graphics.SDL.Window)windowHandle.NativeWindow; + gameWindow = windowHandle?.NativeWindow as SDL.Window ?? new SDL.Window(""); var SDL = Graphics.SDL.Window.SDL; #if STRIDE_GRAPHICS_API_OPENGLES - SDL.GLSetAttribute(GLattr.GLContextProfileMask, (int)GLprofile.GLContextProfileES); + SDL.GLSetAttribute(GLattr.ContextProfileMask, (int)GLprofile.ES); #else - SDL.GLSetAttribute(GLattr.GLContextProfileMask, (int)GLprofile.GLContextProfileCore); + SDL.GLSetAttribute(GLattr.ContextProfileMask, (int)GLprofile.Core); #endif if (IsDebugMode) - SDL.GLSetAttribute(GLattr.GLContextFlags, (int)GLcontextFlag.GLContextDebugFlag); + SDL.GLSetAttribute(GLattr.ContextFlags, (int)GLcontextFlag.DebugFlag); // Setup version - SDL.GLSetAttribute(GLattr.GLContextMajorVersion, currentVersion / 100); - SDL.GLSetAttribute(GLattr.GLContextMinorVersion, (currentVersion / 10) % 10); + SDL.GLSetAttribute(GLattr.ContextMajorVersion, currentVersion / 100); + SDL.GLSetAttribute(GLattr.ContextMinorVersion, (currentVersion / 10) % 10); MainGraphicsContext = new SdlContext(SDL, (Silk.NET.SDL.Window*)gameWindow.SdlHandle); ((SdlContext)MainGraphicsContext).Create(); @@ -690,7 +690,7 @@ protected unsafe void InitializePlatformDevice(GraphicsProfile[] graphicsProfile #endif // Create shared context for creating graphics resources from other threads - SDL.GLSetAttribute(GLattr.GLShareWithCurrentContext, 1); + SDL.GLSetAttribute(GLattr.ShareWithCurrentContext, 1); deviceCreationContext = new SdlContext(SDL, (Silk.NET.SDL.Window*)gameWindow.SdlHandle); ((SdlContext)deviceCreationContext).Create(); diff --git a/sources/engine/Stride.Graphics/OpenGL/Texture.OpenGL.cs b/sources/engine/Stride.Graphics/OpenGL/Texture.OpenGL.cs index 557f2509b0..6faf21b9c8 100644 --- a/sources/engine/Stride.Graphics/OpenGL/Texture.OpenGL.cs +++ b/sources/engine/Stride.Graphics/OpenGL/Texture.OpenGL.cs @@ -413,11 +413,11 @@ private void CreateTexture2DArray(bool compressed, int width, int height, int mi { if (compressed) { - GL.CompressedTexImage3D(TextureTarget, mipLevel, TextureInternalFormat, (uint) width, (uint) height, (uint) ArraySize, border: 0, imageSize: 0, data: IntPtr.Zero); + GL.CompressedTexImage3D(TextureTarget, mipLevel, TextureInternalFormat, (uint) width, (uint) height, (uint) ArraySize, border: 0, imageSize: 0, data: null); } else { - GL.TexImage3D(TextureTarget, mipLevel, TextureInternalFormat, (uint) width, (uint) height, (uint) ArraySize, border: 0, TextureFormat, TextureType, IntPtr.Zero); + GL.TexImage3D(TextureTarget, mipLevel, TextureInternalFormat, (uint) width, (uint) height, (uint) ArraySize, border: 0, TextureFormat, TextureType, null); } } @@ -620,7 +620,7 @@ internal uint GeneratePixelBufferObject(BufferTargetARB target, PixelStoreParame GL.BindBuffer(target, result); if (RowPitch < 4) GL.PixelStore(alignment, 1); - GL.BufferData(target, (UIntPtr) totalSize, IntPtr.Zero, bufferUsage); + GL.BufferData(target, (UIntPtr) totalSize, null, bufferUsage); GL.BindBuffer(target, 0); return result; diff --git a/sources/engine/Stride.Graphics/Texture.cs b/sources/engine/Stride.Graphics/Texture.cs index 0571bb0faa..64043d3c5a 100644 --- a/sources/engine/Stride.Graphics/Texture.cs +++ b/sources/engine/Stride.Graphics/Texture.cs @@ -474,10 +474,7 @@ internal Texture InitializeFrom(Texture parentTexture, TextureViewDescription vi internal Texture InitializeFrom(Texture parentTexture, TextureDescription description, TextureViewDescription viewDescription, DataBox[] textureDatas = null) { ParentTexture = parentTexture; - if (ParentTexture != null) - { - ParentTexture.AddReferenceInternal(); - } + ParentTexture?.AddReferenceInternal(); textureDescription = description; textureViewDescription = viewDescription; @@ -1143,8 +1140,8 @@ public Image GetDataAsImage(CommandList commandList) if (Usage == GraphicsResourceUsage.Staging) return GetDataAsImage(commandList, this); // Directly if this is a staging resource - using (var stagingTexture = ToStaging()) - return GetDataAsImage(commandList, stagingTexture); + using var stagingTexture = ToStaging(); + return GetDataAsImage(commandList, stagingTexture); } /// diff --git a/sources/engine/Stride.Rendering/Rendering/ComputeEffect/GGXPrefiltering/Hammersley.sdsl b/sources/engine/Stride.Rendering/Rendering/ComputeEffect/GGXPrefiltering/Hammersley.sdsl index 48c399d9f0..ef8c96a6cd 100644 --- a/sources/engine/Stride.Rendering/Rendering/ComputeEffect/GGXPrefiltering/Hammersley.sdsl +++ b/sources/engine/Stride.Rendering/Rendering/ComputeEffect/GGXPrefiltering/Hammersley.sdsl @@ -12,7 +12,7 @@ namespace Stride.Rendering.Images { var u = 0.0; var p = 0.5; - for (int kk=k; kk; p*=0.5, kk>>=1) + for (int kk=k; kk > 0; p*=0.5, kk>>=1) { if (kk & 1) // kk mod 2 == 1 u += p; diff --git a/sources/engine/Stride.Rendering/Rendering/ComputeEffect/GGXPrefiltering/RadiancePrefilteringGGXNoCompute.cs b/sources/engine/Stride.Rendering/Rendering/ComputeEffect/GGXPrefiltering/RadiancePrefilteringGGXNoCompute.cs index 2c8732cfec..b9aceba33f 100644 --- a/sources/engine/Stride.Rendering/Rendering/ComputeEffect/GGXPrefiltering/RadiancePrefilteringGGXNoCompute.cs +++ b/sources/engine/Stride.Rendering/Rendering/ComputeEffect/GGXPrefiltering/RadiancePrefilteringGGXNoCompute.cs @@ -96,41 +96,37 @@ protected override void DrawCore(RenderDrawContext context) { for (int faceIndex = 0; faceIndex < faceCount; faceIndex++) { - using (var outputView = output.ToTextureView(ViewType.Single, faceIndex, mipLevel)) + using var outputView = output.ToTextureView(ViewType.Single, faceIndex, mipLevel); + var inputLevel = MathUtil.Log2(input.Width / output.Width); + if (mipLevel == 0 && DoNotFilterHighestLevel) { - var inputLevel = MathUtil.Log2(input.Width / output.Width); - if (mipLevel == 0 && DoNotFilterHighestLevel) + if (input.Width >= output.Width && inputLevel < input.MipLevels && input.Format == output.Format) { - if (input.Width >= output.Width && inputLevel < input.MipLevels && input.Format == output.Format) - { - // Optimization: make a simple copy of the texture when possible - var inputSubresource = inputLevel + faceIndex * input.MipLevels; - var outputSubresource = 0 + faceIndex * output.MipLevels; - context.CommandList.CopyRegion(input, inputSubresource, null, output, outputSubresource); - } - else // otherwise rescale the closest mipmap - { - var inputMipmapLevel = Math.Min(inputLevel, input.MipLevels - 1); - using (var inputView = input.ToTextureView(ViewType.Single, faceIndex, inputMipmapLevel)) - { - scaler.SetInput(inputView); - scaler.SetOutput(outputView); - scaler.Draw(context); - } - } + // Optimization: make a simple copy of the texture when possible + var inputSubresource = inputLevel + faceIndex * input.MipLevels; + var outputSubresource = 0 + faceIndex * output.MipLevels; + context.CommandList.CopyRegion(input, inputSubresource, null, output, outputSubresource); } - else + else // otherwise rescale the closest mipmap { - shader.Parameters.Set(RadiancePrefilteringGGXNoComputeShaderKeys.Face, faceIndex); - shader.Parameters.Set(RadiancePrefilteringGGXNoComputeShaderKeys.Roughness, roughness); - shader.Parameters.Set(RadiancePrefilteringGGXNoComputeShaderKeys.MipmapCount, input.MipLevels - 1); - shader.Parameters.Set(RadiancePrefilteringGGXNoComputeShaderKeys.RadianceMap, input); - shader.Parameters.Set(RadiancePrefilteringGGXNoComputeShaderKeys.RadianceMapSize, input.Width); - shader.Parameters.Set(RadiancePrefilteringGGXNoComputeParams.NbOfSamplings, SamplingsCount); - shader.SetOutput(outputView); - shader.Draw(context); + var inputMipmapLevel = Math.Min(inputLevel, input.MipLevels - 1); + using var inputView = input.ToTextureView(ViewType.Single, faceIndex, inputMipmapLevel); + scaler.SetInput(inputView); + scaler.SetOutput(outputView); + scaler.Draw(context); } } + else + { + shader.Parameters.Set(RadiancePrefilteringGGXNoComputeShaderKeys.Face, faceIndex); + shader.Parameters.Set(RadiancePrefilteringGGXNoComputeShaderKeys.Roughness, roughness); + shader.Parameters.Set(RadiancePrefilteringGGXNoComputeShaderKeys.MipmapCount, input.MipLevels - 1); + shader.Parameters.Set(RadiancePrefilteringGGXNoComputeShaderKeys.RadianceMap, input); + shader.Parameters.Set(RadiancePrefilteringGGXNoComputeShaderKeys.RadianceMapSize, input.Width); + shader.Parameters.Set(RadiancePrefilteringGGXNoComputeParams.NbOfSamplings, SamplingsCount); + shader.SetOutput(outputView); + shader.Draw(context); + } } if (mipCount > 1)