This repository has been archived by the owner on Apr 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Safer implementaion of Texture/Buffer nodes and their views. #237
- Loading branch information
Showing
3 changed files
with
399 additions
and
221 deletions.
There are no files selected for viewing
190 changes: 190 additions & 0 deletions
190
packages/VL.Stride.Runtime/src/Graphics/GraphicsNodes.BufferBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
using Stride.Core; | ||
using Stride.Engine; | ||
using Stride.Graphics; | ||
using System; | ||
using VL.Core; | ||
using VL.Lib.Basics.Resources; | ||
using Buffer = Stride.Graphics.Buffer; | ||
|
||
namespace VL.Stride.Graphics | ||
{ | ||
static partial class GraphicsNodes | ||
{ | ||
class BufferBuilder | ||
{ | ||
private BufferDescription description; | ||
private BufferViewDescription viewDescription; | ||
private IntPtr initalData; | ||
private bool needsRebuild = true; | ||
private Buffer buffer; | ||
internal bool Recreate; | ||
private readonly IResourceHandle<Game> gameHandle; | ||
|
||
public Buffer Buffer | ||
{ | ||
get | ||
{ | ||
if (needsRebuild || Recreate) | ||
{ | ||
RebuildBuffer(); | ||
needsRebuild = false; | ||
} | ||
return buffer; | ||
} | ||
|
||
private set => buffer = value; | ||
} | ||
|
||
public BufferDescription Description | ||
{ | ||
get => description; | ||
set | ||
{ | ||
description = value; | ||
needsRebuild = true; | ||
} | ||
} | ||
|
||
public BufferViewDescription ViewDescription | ||
{ | ||
get => viewDescription; | ||
set | ||
{ | ||
viewDescription = value; | ||
needsRebuild = true; | ||
} | ||
} | ||
|
||
public IntPtr InitalData | ||
{ | ||
get => initalData; | ||
set | ||
{ | ||
initalData = value; | ||
needsRebuild = true; | ||
} | ||
} | ||
|
||
public BufferBuilder(NodeContext nodeContext) | ||
{ | ||
gameHandle = nodeContext.GetGameHandle(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
buffer?.Dispose(); | ||
buffer = null; | ||
gameHandle.Dispose(); | ||
} | ||
|
||
private void RebuildBuffer() | ||
{ | ||
try | ||
{ | ||
buffer?.Dispose(); | ||
buffer = null; | ||
var game = gameHandle.Resource; | ||
buffer = BufferExtensions.New(game.GraphicsDevice, description, viewDescription, initalData); | ||
} | ||
catch | ||
{ | ||
buffer = null; | ||
} | ||
} | ||
} | ||
|
||
class BufferViewBuilder | ||
{ | ||
private Buffer buffer; | ||
private BufferViewDescription viewDescription; | ||
private bool needsRebuild = true; | ||
private Buffer bufferView; | ||
internal bool Recreate; | ||
private readonly IResourceHandle<Game> gameHandle; | ||
|
||
public Buffer Buffer | ||
{ | ||
get | ||
{ | ||
if (needsRebuild || Recreate) | ||
{ | ||
RebuildBufferView(); | ||
needsRebuild = false; | ||
} | ||
return bufferView; | ||
} | ||
|
||
private set => bufferView = value; | ||
} | ||
|
||
public Buffer Input | ||
{ | ||
get => buffer; | ||
set | ||
{ | ||
buffer = value; | ||
needsRebuild = true; | ||
} | ||
} | ||
|
||
public BufferViewDescription ViewDescription | ||
{ | ||
get => viewDescription; | ||
set | ||
{ | ||
viewDescription = value; | ||
needsRebuild = true; | ||
} | ||
} | ||
|
||
public BufferViewBuilder(NodeContext nodeContext) | ||
{ | ||
gameHandle = nodeContext.GetGameHandle(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
bufferView?.Dispose(); | ||
bufferView = null; | ||
gameHandle.Dispose(); | ||
} | ||
|
||
private void RebuildBufferView() | ||
{ | ||
try | ||
{ | ||
if (bufferView != null) | ||
{ | ||
bufferView.Destroyed -= BufferView_Destroyed; | ||
bufferView.Dispose(); | ||
bufferView = null; | ||
} | ||
|
||
if (buffer != null | ||
&& viewDescription.Flags != BufferFlags.None | ||
&& ((buffer.Flags & BufferFlags.RawBuffer) != 0)) | ||
{ | ||
bufferView ??= new Buffer(); | ||
var game = gameHandle.Resource; | ||
bufferView = BufferExtensions.ToBufferView(bufferView, buffer, viewDescription, game.GraphicsDevice); | ||
bufferView.DisposeBy(buffer); | ||
bufferView.Destroyed += BufferView_Destroyed; | ||
} | ||
else | ||
{ | ||
bufferView = null; | ||
} | ||
} | ||
catch | ||
{ | ||
bufferView = null; | ||
} | ||
} | ||
|
||
private void BufferView_Destroyed(object sender, EventArgs e) | ||
{ | ||
bufferView = null; | ||
} | ||
} | ||
} | ||
} |
185 changes: 185 additions & 0 deletions
185
packages/VL.Stride.Runtime/src/Graphics/GraphicsNodes.TextureBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
using Stride.Core; | ||
using Stride.Engine; | ||
using Stride.Graphics; | ||
using VL.Core; | ||
using VL.Lib.Basics.Resources; | ||
|
||
namespace VL.Stride.Graphics | ||
{ | ||
static partial class GraphicsNodes | ||
{ | ||
class TextureBuilder | ||
{ | ||
private TextureDescription description; | ||
private TextureViewDescription viewDescription; | ||
private DataBox[] initalData; | ||
private bool needsRebuild = true; | ||
private Texture texture; | ||
internal bool Recreate; | ||
private readonly IResourceHandle<Game> gameHandle; | ||
|
||
public Texture Texture | ||
{ | ||
get | ||
{ | ||
if (needsRebuild || Recreate) | ||
{ | ||
RebuildTexture(); | ||
needsRebuild = false; | ||
} | ||
return texture; | ||
} | ||
|
||
private set => texture = value; | ||
} | ||
|
||
public TextureDescription Description | ||
{ | ||
get => description; | ||
set | ||
{ | ||
description = value; | ||
needsRebuild = true; | ||
} | ||
} | ||
|
||
public TextureViewDescription ViewDescription | ||
{ | ||
get => viewDescription; | ||
set | ||
{ | ||
viewDescription = value; | ||
needsRebuild = true; | ||
} | ||
} | ||
|
||
public DataBox[] InitalData | ||
{ | ||
get => initalData; | ||
set | ||
{ | ||
initalData = value; | ||
needsRebuild = true; | ||
} | ||
} | ||
|
||
public TextureBuilder(NodeContext nodeContext) | ||
{ | ||
gameHandle = nodeContext.GetGameHandle(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
texture?.Dispose(); | ||
texture = null; | ||
gameHandle.Dispose(); | ||
} | ||
|
||
private void RebuildTexture() | ||
{ | ||
try | ||
{ | ||
texture?.Dispose(); | ||
texture = null; | ||
var game = gameHandle.Resource; | ||
texture = Texture.New(game.GraphicsDevice, description, viewDescription, initalData); | ||
} | ||
catch | ||
{ | ||
texture = null; | ||
} | ||
} | ||
} | ||
|
||
class TextureViewBuilder | ||
{ | ||
private Texture texture; | ||
private TextureViewDescription viewDescription; | ||
private bool needsRebuild = true; | ||
private Texture textureView; | ||
internal bool Recreate; | ||
private readonly IResourceHandle<Game> gameHandle; | ||
|
||
public Texture TextureView | ||
{ | ||
get | ||
{ | ||
if (needsRebuild || Recreate) | ||
{ | ||
RebuildTextureView(); | ||
needsRebuild = false; | ||
} | ||
return textureView; | ||
} | ||
|
||
private set => textureView = value; | ||
} | ||
|
||
public Texture Input | ||
{ | ||
get => texture; | ||
set | ||
{ | ||
texture = value; | ||
needsRebuild = true; | ||
} | ||
} | ||
|
||
public TextureViewDescription ViewDescription | ||
{ | ||
get => viewDescription; | ||
set | ||
{ | ||
viewDescription = value; | ||
needsRebuild = true; | ||
} | ||
} | ||
|
||
public TextureViewBuilder(NodeContext nodeContext) | ||
{ | ||
gameHandle = nodeContext.GetGameHandle(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
textureView?.Dispose(); | ||
textureView = null; | ||
gameHandle.Dispose(); | ||
} | ||
|
||
private void RebuildTextureView() | ||
{ | ||
try | ||
{ | ||
if (textureView != null) | ||
{ | ||
textureView.Destroyed -= TextureView_Destroyed; | ||
textureView.Dispose(); | ||
textureView = null; | ||
} | ||
|
||
if (texture != null && ( | ||
viewDescription.Format == PixelFormat.None | ||
|| (texture.Format == viewDescription.Format) | ||
|| (texture.Format.IsTypeless() && (texture.Format.BlockSize() == viewDescription.Format.BlockSize())) | ||
)) | ||
{ | ||
var game = gameHandle.Resource; | ||
textureView = texture.ToTextureView(viewDescription); | ||
textureView.DisposeBy(texture); | ||
textureView.Destroyed += TextureView_Destroyed; | ||
} | ||
} | ||
catch | ||
{ | ||
textureView = null; | ||
} | ||
} | ||
|
||
private void TextureView_Destroyed(object sender, System.EventArgs e) | ||
{ | ||
textureView = null; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.