Skip to content

Commit

Permalink
Add support for binding textures directly from a TextureSequence
Browse files Browse the repository at this point in the history
Fixes #1645
  • Loading branch information
PathogenDavid committed May 8, 2024
1 parent ced27db commit ac742fa
Showing 1 changed file with 25 additions and 32 deletions.
57 changes: 25 additions & 32 deletions Bonsai.Shaders/BindTexture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,23 @@ public bool IndexSpecified
get { return Index.HasValue; }
}

IObservable<TSource> Process<TSource>(IObservable<TSource> source, Action<TSource> update)
/// <summary>
/// Binds the specified texture buffer to the specified texture unit for
/// each notification in an observable sequence.
/// </summary>
/// <typeparam name="TSource">
/// The type of the elements in the <paramref name="source"/> sequence.
/// </typeparam>
/// <param name="source">
/// The sequence containing the notifications indicating when to bind
/// the texture buffer to the specified texture unit.
/// </param>
/// <returns>
/// An observable sequence that is identical to the source sequence but where
/// there is an additional side effect of binding the texture buffer to the
/// specified texture unit.
/// </returns>
public IObservable<TSource> Process<TSource>(IObservable<TSource> source)
{
return Observable.Defer(() =>
{
Expand All @@ -76,43 +92,25 @@ IObservable<TSource> Process<TSource>(IObservable<TSource> source, Action<TSourc
: null;
}

texture ??= input as Texture;

if (texture != null)
{
var index = Index;
var textureId = index.HasValue ? ((TextureSequence)texture).Textures[index.Value] : texture.Id;
var textureId = Index is int index
? ((TextureSequence)texture).Textures[index]
: texture.Id;
shader.Update(() =>
{
GL.ActiveTexture(TextureSlot);
GL.BindTexture(TextureTarget, textureId);
});
}
else if (update != null) shader.Update(() => update(input));

return input;
});
});
}

/// <summary>
/// Binds the specified texture buffer to the specified texture unit for
/// each notification in an observable sequence.
/// </summary>
/// <typeparam name="TSource">
/// The type of the elements in the <paramref name="source"/> sequence.
/// </typeparam>
/// <param name="source">
/// The sequence containing the notifications indicating when to bind
/// the texture buffer to the specified texture unit.
/// </param>
/// <returns>
/// An observable sequence that is identical to the source sequence but where
/// there is an additional side effect of binding the texture buffer to the
/// specified texture unit.
/// </returns>
public IObservable<TSource> Process<TSource>(IObservable<TSource> source)
{
return Process(source, update: null);
}

/// <summary>
/// Binds each texture buffer in an observable sequence to the specified
/// texture unit for each notification in an observable sequence.
Expand All @@ -132,12 +130,7 @@ public IObservable<TSource> Process<TSource>(IObservable<TSource> source)
/// in the sequence to the specified texture unit.
/// </returns>
public IObservable<Texture> Process(IObservable<Texture> source)
{
return Process(source, input =>
{
GL.ActiveTexture(TextureSlot);
GL.BindTexture(TextureTarget, input != null ? input.Id : 0);
});
}
// This overload only exists for documentation reasons
=> Process<Texture>(source);
}
}

0 comments on commit ac742fa

Please sign in to comment.