Skip to content

Commit a1f7051

Browse files
committed
[Toolkit.Graphics] Add Effect.Clone
1 parent a0855e8 commit a1f7051

15 files changed

+114
-25
lines changed

Source/SharpDX/DisposeCollector.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ public int Count
3939
get { return disposables.Count; }
4040
}
4141

42+
/// <summary>
43+
/// Clears the list of disposable objects without disposing them.
44+
/// </summary>
45+
public void Clear()
46+
{
47+
if (disposables == null)
48+
return;
49+
50+
disposables.Clear();
51+
}
52+
4253
/// <summary>
4354
/// Disposes all object collected by this class and clear the list. The collector can still be used for collecting.
4455
/// </summary>

Source/Toolkit/SharpDX.Toolkit.Graphics/Effect.cs

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,17 @@ public class Effect : GraphicsResource
4141
/// <summary>
4242
/// Gets a collection of constant buffers that are defined for this effect.
4343
/// </summary>
44-
public readonly EffectConstantBufferCollection ConstantBuffers;
44+
public EffectConstantBufferCollection ConstantBuffers { get; private set; }
4545

4646
/// <summary>
4747
/// Gets a collection of parameters that are defined for this effect.
4848
/// </summary>
49-
public readonly EffectParameterCollection Parameters;
49+
public EffectParameterCollection Parameters { get; private set; }
5050

5151
/// <summary>
5252
/// Gets a collection of techniques that are defined for this effect.
5353
/// </summary>
54-
public readonly EffectTechniqueCollection Techniques;
54+
public EffectTechniqueCollection Techniques { get; private set; }
5555

5656
/// <summary>
5757
/// Gets the data associated to this effect.
@@ -90,6 +90,12 @@ public Effect(GraphicsDevice device, byte[] bytecode)
9090
/// <remarks>The effect bytecode must contain only a single effect and will be registered into the <see cref="GraphicsDevice.DefaultEffectPool"/>.</remarks>
9191
public Effect(GraphicsDevice device, EffectData effectData, EffectPool effectPool = null) : base(device)
9292
{
93+
CreateInstanceFrom(device, effectData, effectPool);
94+
}
95+
96+
internal void CreateInstanceFrom(GraphicsDevice device, EffectData effectData, EffectPool effectPool)
97+
{
98+
GraphicsDevice = device;
9399
ConstantBuffers = new EffectConstantBufferCollection();
94100
Parameters = new EffectParameterCollection();
95101
Techniques = new EffectTechniqueCollection();
@@ -113,7 +119,7 @@ public Effect(GraphicsDevice device, EffectData effectData, EffectPool effectPoo
113119
/// Gets the pool this effect attached to.
114120
/// </summary>
115121
/// <value> The pool. </value>
116-
public readonly EffectPool Pool;
122+
public EffectPool Pool { get; private set; }
117123

118124
/// <summary>
119125
/// Occurs when the on apply is applied on a pass.
@@ -283,13 +289,56 @@ public void InitializeFrom(EffectData.Effect effectDataArg)
283289
Initialize();
284290
}
285291

292+
/// <summary>
293+
/// Clones this instance.
294+
/// </summary>
295+
/// <returns>A new instance of this Effect.</returns>
296+
public virtual Effect Clone()
297+
{
298+
var effect = (Effect) MemberwiseClone();
299+
effect.DisposeCollector = new DisposeCollector();
300+
effect.ConstantBuffers = new EffectConstantBufferCollection();
301+
effect.Parameters = new EffectParameterCollection();
302+
effect.Techniques = new EffectTechniqueCollection();
303+
effect.effectConstantBuffersCache = null;
304+
305+
// Initialize from effect
306+
effect.InitializeFrom(effect.RawEffectData);
307+
308+
// Copy the content of the constant buffers to the new instance.
309+
for (int i = 0; i < effect.ConstantBuffers.Count; i++)
310+
{
311+
ConstantBuffers[i].CopyTo(effect.ConstantBuffers[i]);
312+
}
313+
314+
// Copy back all bound resources except constant buffers
315+
// that are already initialized with InitializeFrom method.
316+
for (int i = 0; i < ResourceLinker.Count; i++)
317+
{
318+
if (ResourceLinker.BoundResources[i] is EffectConstantBuffer)
319+
continue;
320+
321+
effect.ResourceLinker.BoundResources[i] = ResourceLinker.BoundResources[i];
322+
unsafe
323+
{
324+
effect.ResourceLinker.Pointers[i] = ResourceLinker.Pointers[i];
325+
}
326+
}
327+
328+
// If everything was fine, then we can register it into the pool
329+
Pool.AddEffect(effect);
330+
331+
return effect;
332+
}
333+
286334
protected virtual void Initialize()
287335
{
288336
}
289337

290338
internal new DisposeCollector DisposeCollector
291339
{
292340
get { return base.DisposeCollector; }
341+
private set { base.DisposeCollector = value; }
293342
}
294343

295344
protected internal virtual EffectPass OnApply(EffectPass pass)

Source/Toolkit/SharpDX.Toolkit.Graphics/EffectConstantBuffer.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,23 @@ public void Update()
8383
Update(device);
8484
}
8585

86+
/// <summary>
87+
/// Copies the content of this buffer to another constant buffer. Destination
88+
/// buffer will be flagged as dirty.
89+
/// </summary>
90+
/// <param name="toBuffer">To buffer to receive the content.</param>
91+
public void CopyTo(EffectConstantBuffer toBuffer)
92+
{
93+
if (Size != toBuffer.Size)
94+
{
95+
throw new ArgumentOutOfRangeException("toBuffer",
96+
"Size of the source and destination buffer are not the same");
97+
}
98+
99+
Utilities.CopyMemory(toBuffer.DataPointer, DataPointer, Size);
100+
toBuffer.IsDirty = true;
101+
}
102+
86103
/// <summary>
87104
/// Updates the specified constant buffer from all parameters value.
88105
/// </summary>

Source/Toolkit/SharpDX.Toolkit.Graphics/EffectContentReader.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1919
// THE SOFTWARE.
2020

21+
using System;
2122
using System.IO;
2223
using SharpDX.Toolkit.Content;
2324

@@ -28,13 +29,13 @@ namespace SharpDX.Toolkit.Graphics
2829
/// </summary>
2930
internal class EffectContentReader : GraphicsResourceContentReaderBase<Effect>
3031
{
31-
protected override Effect ReadContent(IContentManager readerManager, GraphicsDevice device, string assetName, Stream stream, object options)
32+
protected override Effect ReadContent(IContentManager readerManager, GraphicsDevice device, string assetName, Type type, Stream stream, object options = null)
3233
{
3334
var effectData = EffectData.Load(stream);
3435
if (effectData == null)
3536
return null;
3637

37-
return new Effect(device, effectData);
38+
return (Effect)Activator.CreateInstance(type, device, effectData, null);
3839
}
3940
}
4041
}

Source/Toolkit/SharpDX.Toolkit.Graphics/EffectResourceLinker.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ namespace SharpDX.Toolkit.Graphics
2525
/// <summary>
2626
/// A class used to shader input resource parameters and prepare them (i.e. take their NativePointer asap).
2727
/// </summary>
28-
class EffectResourceLinker : IDisposable
28+
internal class EffectResourceLinker : IDisposable
2929
{
3030
/// <summary>
3131
/// Real object resources, as they were set on the parameter.
3232
/// </summary>
33-
private object[] resources;
33+
public object[] BoundResources;
3434

3535
/// <summary>
3636
/// Real object resources, as they were set on the parameter.
@@ -52,7 +52,7 @@ class EffectResourceLinker : IDisposable
5252
/// </summary>
5353
public void Initialize()
5454
{
55-
resources = new object[Count];
55+
BoundResources = new object[Count];
5656
ConstantBuffers = new Buffer[Count];
5757
unsafe
5858
{
@@ -76,12 +76,12 @@ public void Dispose()
7676

7777
public T GetResource<T>(int resourceIndex) where T : class
7878
{
79-
return (T)resources[resourceIndex];
79+
return (T)BoundResources[resourceIndex];
8080
}
8181

8282
public void SetResource<T>(int resourceIndex, EffectResourceType type, object value)
8383
{
84-
resources[resourceIndex] = value;
84+
BoundResources[resourceIndex] = value;
8585
unsafe
8686
{
8787
Pointers[resourceIndex] = GetNativePointer(resourceIndex, type, value);
@@ -102,7 +102,7 @@ public void SetResource<T>(int resourceIndex, EffectResourceType type, params T[
102102
{
103103
foreach (var value in valueArray)
104104
{
105-
resources[resourceIndex] = value;
105+
BoundResources[resourceIndex] = value;
106106
unsafe
107107
{
108108
Pointers[resourceIndex] = GetNativePointer(resourceIndex, type, value);

Source/Toolkit/SharpDX.Toolkit.Graphics/GraphicsResource.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,17 @@ public abstract class GraphicsResource : Component
3333
/// <summary>
3434
/// GraphicsDevice used to create this instance.
3535
/// </summary>
36-
public readonly GraphicsDevice GraphicsDevice;
36+
public GraphicsDevice GraphicsDevice { get; protected set; }
3737

3838
/// <summary>
3939
/// The attached Direct3D11 resource to this instance.
4040
/// </summary>
4141
internal DeviceChild Resource;
4242

43+
internal GraphicsResource()
44+
{
45+
}
46+
4347
protected GraphicsResource(GraphicsDevice graphicsDevice)
4448
{
4549
if (graphicsDevice == null)

Source/Toolkit/SharpDX.Toolkit.Graphics/GraphicsResourceContentReaderBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace SharpDX.Toolkit.Graphics
3030
/// <typeparam name="T"></typeparam>
3131
abstract class GraphicsResourceContentReaderBase<T> : IContentReader
3232
{
33-
object IContentReader.ReadContent(IContentManager readerManager, string assetName, Stream stream, out bool keepStreamOpen, object options)
33+
object IContentReader.ReadContent(IContentManager readerManager, string assetName, Type type, Stream stream, out bool keepStreamOpen, object options = null)
3434
{
3535
keepStreamOpen = false;
3636
var service = readerManager.ServiceProvider.GetService(typeof (IGraphicsDeviceService)) as IGraphicsDeviceService;
@@ -40,9 +40,9 @@ object IContentReader.ReadContent(IContentManager readerManager, string assetNam
4040
if (service.GraphicsDevice == null)
4141
throw new InvalidOperationException("GraphicsDevice is not initialized");
4242

43-
return ReadContent(readerManager, service.GraphicsDevice, assetName, stream);
43+
return ReadContent(readerManager, service.GraphicsDevice, assetName, type, stream);
4444
}
4545

46-
protected abstract T ReadContent(IContentManager readerManager, GraphicsDevice device, string assetName, Stream stream, object options = null);
46+
protected abstract T ReadContent(IContentManager readerManager, GraphicsDevice device, string assetName, Type type, Stream stream, object options = null);
4747
}
4848
}

Source/Toolkit/SharpDX.Toolkit.Graphics/ImageContentReader.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1919
// THE SOFTWARE.
2020

21+
using System;
2122
using System.IO;
2223
using SharpDX.Toolkit.Content;
2324

@@ -28,7 +29,7 @@ namespace SharpDX.Toolkit.Graphics
2829
/// </summary>
2930
class ImageContentReader : IContentReader
3031
{
31-
public object ReadContent(IContentManager readerManager, string assetName, Stream stream, out bool keepStreamOpen, object options)
32+
public object ReadContent(IContentManager readerManager, string assetName, Type type, Stream stream, out bool keepStreamOpen, object options = null)
3233
{
3334
keepStreamOpen = false;
3435
var image = Image.Load(stream);

Source/Toolkit/SharpDX.Toolkit.Graphics/ModelContentReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace SharpDX.Toolkit.Graphics
3030
/// </summary>
3131
internal class ModelContentReader : GraphicsResourceContentReaderBase<Model>
3232
{
33-
protected override Model ReadContent(IContentManager readerManager, GraphicsDevice device, string assetName, Stream stream, object options)
33+
protected override Model ReadContent(IContentManager readerManager, GraphicsDevice device, string assetName, Type type, Stream stream, object options = null)
3434
{
3535
var readerOptions = options as ModelContentReaderOptions;
3636
if (options != null && readerOptions == null)

Source/Toolkit/SharpDX.Toolkit.Graphics/SpriteFontContentReader.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1919
// THE SOFTWARE.
2020

21+
using System;
2122
using System.IO;
2223
using SharpDX.Toolkit.Content;
2324

@@ -28,7 +29,7 @@ namespace SharpDX.Toolkit.Graphics
2829
/// </summary>
2930
internal class SpriteFontContentReader : GraphicsResourceContentReaderBase<SpriteFont>
3031
{
31-
protected override SpriteFont ReadContent(IContentManager readerManager, GraphicsDevice device, string assetName, Stream stream, object options)
32+
protected override SpriteFont ReadContent(IContentManager readerManager, GraphicsDevice device, string assetName, Type type, Stream stream, object options = null)
3233
{
3334
SpriteFont spriteFont = null;
3435
var assetPath = Path.GetDirectoryName(assetName);

Source/Toolkit/SharpDX.Toolkit.Graphics/TextureContentReader.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1919
// THE SOFTWARE.
2020

21+
using System;
2122
using System.IO;
2223
using SharpDX.Toolkit.Content;
2324

@@ -28,7 +29,7 @@ namespace SharpDX.Toolkit.Graphics
2829
/// </summary>
2930
class TextureContentReader : GraphicsResourceContentReaderBase<Texture>
3031
{
31-
protected override Texture ReadContent(IContentManager readerManager, GraphicsDevice device, string assetName, Stream stream, object options)
32+
protected override Texture ReadContent(IContentManager readerManager, GraphicsDevice device, string assetName, Type type, Stream stream, object options = null)
3233
{
3334
var texture = Texture.Load(device, stream);
3435
if (texture != null)

Source/Toolkit/SharpDX.Toolkit/Content/ContentManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ private object LoadAssetWithDynamicContentReader<T>(string assetNameWithExtensio
308308

309309
// Rewind position everytime we try to load an asset
310310
stream.Position = startPosition;
311-
result = contentReader.ReadContent(this, assetNameWithExtension, stream, out keepStreamOpen);
311+
result = contentReader.ReadContent(this, assetNameWithExtension, typeof(T), stream, out keepStreamOpen);
312312
stream.Position = startPosition;
313313
}
314314
else
@@ -322,7 +322,7 @@ private object LoadAssetWithDynamicContentReader<T>(string assetNameWithExtensio
322322
foreach (IContentReader registeredContentReader in readers)
323323
{
324324
// Rewind position everytime we try to load an asset
325-
result = registeredContentReader.ReadContent(this, assetNameWithExtension, stream, out keepStreamOpen);
325+
result = registeredContentReader.ReadContent(this, assetNameWithExtension, typeof(T), stream, out keepStreamOpen);
326326
stream.Position = startPosition;
327327
if (result != null) break;
328328
}

Source/Toolkit/SharpDX.Toolkit/Content/IContentReader.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1919
// THE SOFTWARE.
2020

21+
using System;
2122
using System.IO;
2223

2324
namespace SharpDX.Toolkit.Content
@@ -32,10 +33,11 @@ public interface IContentReader
3233
/// </summary>
3334
/// <param name="contentManager">The content manager.</param>
3435
/// <param name="assetName">The name of the asset associated with the stream.</param>
36+
/// <param name="type"> </param>
3537
/// <param name="stream">The steam of the asset to load data from.</param>
3638
/// <param name="keepStreamOpen"><c>true</c> to keep the stream opened after the content was read, otherwise the stream will be closed after if this content reader succeeded to read the data.</param>
3739
/// <param name="options">The options passed to the content manager.</param>
3840
/// <returns>The data decoded from the stream, or null if the kind of asset is not supported by this content reader.</returns>
39-
object ReadContent(IContentManager contentManager, string assetName, Stream stream, out bool keepStreamOpen, object options = null);
41+
object ReadContent(IContentManager contentManager, string assetName, Type type, Stream stream, out bool keepStreamOpen, object options = null);
4042
}
4143
}

Source/Toolkit/SharpDX.Toolkit/Graphics/EffectDataContentReader.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1919
// THE SOFTWARE.
2020

21+
using System;
2122
using System.IO;
2223
using SharpDX.Toolkit.Content;
2324

@@ -28,7 +29,7 @@ namespace SharpDX.Toolkit.Graphics
2829
/// </summary>
2930
class EffectDataContentReader : IContentReader
3031
{
31-
public object ReadContent(IContentManager readerManager, string assetName, Stream stream, out bool keepStreamOpen, object options)
32+
public object ReadContent(IContentManager readerManager, string assetName, Type type, Stream stream, out bool keepStreamOpen, object options = null)
3233
{
3334
keepStreamOpen = false;
3435
return EffectData.Load(stream);

Source/Toolkit/SharpDX.Toolkit/Graphics/SpriteFontDataContentReader.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1919
// THE SOFTWARE.
2020

21+
using System;
2122
using System.IO;
2223
using SharpDX.Toolkit.Content;
2324

@@ -28,7 +29,7 @@ namespace SharpDX.Toolkit.Graphics
2829
/// </summary>
2930
class SpriteFontDataContentReader : IContentReader
3031
{
31-
public object ReadContent(IContentManager readerManager, string assetName, Stream stream, out bool keepStreamOpen, object options)
32+
public object ReadContent(IContentManager readerManager, string assetName, Type type, Stream stream, out bool keepStreamOpen, object options = null)
3233
{
3334
keepStreamOpen = false;
3435
return SpriteFontData.Load(stream);

0 commit comments

Comments
 (0)