Skip to content

Commit

Permalink
split IRuntime.CreateTexture into CreateTexture and CreateTextureArray;
Browse files Browse the repository at this point in the history
fixed operations on TextureArrays with Count=1 (FrameBuffer, Copy, Blit);
added TextureExtensionsCSharp for access to F# Context extensions;
fixed Download of texture region;
  • Loading branch information
luithefirst committed May 17, 2017
1 parent 3ae38e2 commit 35ceeac
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 23 deletions.
8 changes: 4 additions & 4 deletions src/Aardvark.Base.Rendering/RenderTask.fs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ module ChangeableResources =
c
else
runtime.DeleteTexture c
let n = runtime.CreateTexture(size, format, 1, samples, 1)
let n = runtime.CreateTexture(size, format, 1, samples)
current <- Some (samples, size, format, n)
n
| None ->
let n = runtime.CreateTexture(size, format, 1, samples, 1)
let n = runtime.CreateTexture(size, format, 1, samples)
current <- Some (samples, size, format, n)
n
)
Expand Down Expand Up @@ -195,13 +195,13 @@ module private RefCountedResources =
| Some h ->
t.ReplacedResource(ResourceKind.Texture)
runtime.DeleteTexture(h)
let tex = runtime.CreateTexture(size, format, 1, samples, 1)
let tex = runtime.CreateTexture(size, format, 1, samples)
handle <- Some tex
tex :> ITexture

| None ->
t.CreatedResource(ResourceKind.Texture)
let tex = runtime.CreateTexture(size, format, 1, samples, 1)
let tex = runtime.CreateTexture(size, format, 1, samples)
handle <- Some tex
tex :> ITexture

Expand Down
4 changes: 3 additions & 1 deletion src/Aardvark.Base.Rendering/Runtime.fs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@ and IRuntime =


abstract member CreateStreamingTexture : mipMaps : bool -> IStreamingTexture
abstract member CreateTexture : size : V2i * format : TextureFormat * levels : int * samples : int * count : int -> IBackendTexture
abstract member CreateTexture : size : V2i * format : TextureFormat * levels : int * samples : int -> IBackendTexture
abstract member CreateTextureArray : size : V2i * format : TextureFormat * levels : int * samples : int * count : int -> IBackendTexture
abstract member CreateTextureCube : size : V2i * format : TextureFormat * levels : int * samples : int -> IBackendTexture
//abstract member CreateTextureCubeArray : size : V2i * format : TextureFormat * levels : int * samples : int -> IBackendTexture
abstract member CreateRenderbuffer : size : V2i * format : RenderbufferFormat * samples : int -> IRenderbuffer
abstract member CreateFramebuffer : signature : IFramebufferSignature * attachments : Map<Symbol, IFramebufferOutput> -> IFramebuffer
abstract member CreateMappedBuffer : unit -> IMappedBuffer
Expand Down
4 changes: 2 additions & 2 deletions src/Aardvark.Rendering.GL/Resources/Framebuffer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ module FramebufferExtensions =
match o.Dimension with
| TextureDimension.TextureCube ->
let (_,target) = TextureExtensions.cubeSides.[slice]
if o.Count > 1 then
if o.IsArray then
failwith "cubemaparray currently not implemented"
else
GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, attachment, target, o.Handle, level)
GL.Check "could not attach texture"
| _ ->
if o.Count > 1 then
if o.IsArray then
GL.FramebufferTextureLayer(FramebufferTarget.Framebuffer, attachment, o.Handle, level, slice)
else
GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, attachment, TextureTarget.Texture2D, o.Handle, level)
Expand Down
40 changes: 30 additions & 10 deletions src/Aardvark.Rendering.GL/Resources/Texture.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ open OpenTK.Graphics.OpenGL4
open Aardvark.Rendering.GL
open Aardvark.Base.NativeTensors
open Microsoft.FSharp.NativeInterop
open System.Runtime.CompilerServices

#nowarn "9"

Expand Down Expand Up @@ -461,7 +462,7 @@ module TextureCreationExtensions =

GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, fSrc)
GL.Check "could not bind framebuffer"
if src.Count > 1 then GL.FramebufferTextureLayer(FramebufferTarget.ReadFramebuffer, FramebufferAttachment.ColorAttachment0, src.Handle, srcLevel, srcSlice)
if src.IsArray then GL.FramebufferTextureLayer(FramebufferTarget.ReadFramebuffer, FramebufferAttachment.ColorAttachment0, src.Handle, srcLevel, srcSlice)
else GL.FramebufferTexture(FramebufferTarget.ReadFramebuffer, FramebufferAttachment.ColorAttachment0, src.Handle, srcLevel)
GL.Check "could not attach texture to framebuffer"

Expand All @@ -472,7 +473,7 @@ module TextureCreationExtensions =
GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, fDst)
GL.Check "could not bind framebuffer"

if src.Count > 1 then GL.FramebufferTextureLayer(FramebufferTarget.DrawFramebuffer, FramebufferAttachment.ColorAttachment0, dst.Handle, dstLevel, dstSlice)
if src.IsArray then GL.FramebufferTextureLayer(FramebufferTarget.DrawFramebuffer, FramebufferAttachment.ColorAttachment0, dst.Handle, dstLevel, dstSlice)
else GL.FramebufferTexture(FramebufferTarget.DrawFramebuffer, FramebufferAttachment.ColorAttachment0, dst.Handle, dstLevel)
GL.Check "could not attach texture to framebuffer"

Expand Down Expand Up @@ -514,7 +515,7 @@ module TextureCreationExtensions =
GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, fSrc)
GL.Check "could not bind framebuffer"

if src.Count > 1 then
if src.IsArray then
GL.FramebufferTextureLayer(FramebufferTarget.ReadFramebuffer, FramebufferAttachment.ColorAttachment0, src.Handle, srcLevel, srcSlice)
else
GL.FramebufferTexture(FramebufferTarget.ReadFramebuffer, FramebufferAttachment.ColorAttachment0, src.Handle, srcLevel)
Expand Down Expand Up @@ -2636,7 +2637,7 @@ module TextureExtensions =

GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, fSrc)
GL.Check "could not bind framebuffer"
if src.Count > 1 then GL.FramebufferTextureLayer(FramebufferTarget.ReadFramebuffer, FramebufferAttachment.ColorAttachment0, src.Handle, srcLevel, srcSlice)
if src.IsArray then GL.FramebufferTextureLayer(FramebufferTarget.ReadFramebuffer, FramebufferAttachment.ColorAttachment0, src.Handle, srcLevel, srcSlice)
else GL.FramebufferTexture(FramebufferTarget.ReadFramebuffer, FramebufferAttachment.ColorAttachment0, src.Handle, srcLevel)
GL.Check "could not attach texture to framebuffer"

Expand All @@ -2647,7 +2648,7 @@ module TextureExtensions =
GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, fDst)
GL.Check "could not bind framebuffer"

if src.Count > 1 then GL.FramebufferTextureLayer(FramebufferTarget.DrawFramebuffer, FramebufferAttachment.ColorAttachment0, dst.Handle, dstLevel, dstSlice)
if src.IsArray then GL.FramebufferTextureLayer(FramebufferTarget.DrawFramebuffer, FramebufferAttachment.ColorAttachment0, dst.Handle, dstLevel, dstSlice)
else GL.FramebufferTexture(FramebufferTarget.DrawFramebuffer, FramebufferAttachment.ColorAttachment0, dst.Handle, dstLevel)
GL.Check "could not attach texture to framebuffer"

Expand Down Expand Up @@ -2689,7 +2690,7 @@ module TextureExtensions =
GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, fSrc)
GL.Check "could not bind framebuffer"

if src.Count > 1 then
if src.IsArray then
GL.FramebufferTextureLayer(FramebufferTarget.ReadFramebuffer, FramebufferAttachment.ColorAttachment0, src.Handle, srcLevel, srcSlice)
else
GL.FramebufferTexture(FramebufferTarget.ReadFramebuffer, FramebufferAttachment.ColorAttachment0, src.Handle, srcLevel)
Expand Down Expand Up @@ -2789,15 +2790,16 @@ module TextureExtensions =
member x.Download(t : Texture, level : int, slice : int, offset : V2i, target : PixImage) =
using x.ResourceLock (fun _ ->
let levelSize = t.Size2D / (1 <<< level)
let offset = V2i(offset.X, levelSize.Y - offset.Y - target.Size.Y) // flip y-offset
if offset = V2i.Zero && target.Size = levelSize then
x.Download(t, level, slice, target)
else
let temp = x.CreateTexture2D(target.Size, 1, t.Format, 1)

if target.Size = levelSize then
x.Copy(t, level, slice, offset, temp, 0, 0, V2i.Zero, target.Size)
else
if t.IsMultisampled then // resolve multisamples
x.Blit(t, level, slice, Box2i.FromMinAndSize(offset, levelSize), temp, 0, 0, Box2i(V2i.Zero, target.Size), true)
else
x.Copy(t, level, slice, offset, temp, 0, 0, V2i.Zero, target.Size)

x.Download(temp, 0, 0, target)
x.Delete(temp)
Expand Down Expand Up @@ -2907,4 +2909,22 @@ module Texture =

let pi = PixImage.Create(format, int64 size.Y, int64 size.Y)
tex.Context.Download(tex, level, 0, pi)
[|pi|]
[|pi|]

[<Extension; AbstractClass; Sealed>]
type TextureExtensionsCSharp =
[<Extension>]
static member Download(ctx : Context, t : Texture, level : int, slice : int, offset : V2i, target : PixImage) =
ctx.Download(t, level, slice, offset, target)

[<Extension>]
static member Upload(ctx : Context, t : Texture, level : int, slice : int, offset : V2i, source : PixImage) =
ctx.Upload(t, level, slice, offset, source)

[<Extension>]
static member Copy(ctx : Context, src : Texture, srcLevel : int, srcSlice : int, srcOffset : V2i, dst : Texture, dstLevel : int, dstSlice : int, dstOffset : V2i, size : V2i) =
ctx.Copy(src, srcLevel, srcSlice, srcOffset, dst, dstLevel, dstSlice, dstOffset, size)

[<Extension>]
static member Blit(ctx : Context, src : Texture, srcLevel : int, srcSlice : int, srcRegion : Box2i, dst : Texture, dstLevel : int, dstSlice : int, dstRegion : Box2i, linear : bool) =
ctx.Blit(src, srcLevel, srcSlice, srcRegion, dst, dstLevel, dstSlice, dstRegion, linear)
7 changes: 5 additions & 2 deletions src/Aardvark.Rendering.GL/Runtime/Runtime.fs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,11 @@ type Runtime(ctx : Context, shareTextures : bool, shareBuffers : bool) =
member x.CreateFramebuffer(signature : IFramebufferSignature, bindings : Map<Symbol, IFramebufferOutput>) : IFramebuffer =
x.CreateFramebuffer(signature, bindings) :> _

member x.CreateTexture(size : V2i, format : TextureFormat, levels : int, samples : int, count : int) : IBackendTexture =
x.CreateTexture(size, format, levels, samples, count) :> _
member x.CreateTexture(size : V2i, format : TextureFormat, levels : int, samples : int) : IBackendTexture =
ctx.CreateTexture2D(size, levels, format, samples) :> _

member x.CreateTextureArray(size : V2i, format : TextureFormat, levels : int, samples : int, count : int) : IBackendTexture =
ctx.CreateTexture2DArray(size, count, levels, format, samples) :> _

member x.CreateTextureCube(size : V2i, format : TextureFormat, levels : int, samples : int) : IBackendTexture =
x.CreateTextureCube(size, format, levels, samples) :> _
Expand Down
3 changes: 2 additions & 1 deletion src/Aardvark.Rendering.Vulkan2/Runtime/Runtime.fs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,8 @@ type Runtime(device : Device, shareTextures : bool, shareBuffers : bool, debug :
member x.DeleteStreamingTexture(t) = x.DeleteStreamingTexture(t)

member x.CreateFramebuffer(signature, bindings) = x.CreateFramebuffer(signature, bindings)
member x.CreateTexture(size, format, levels, samples, count) = x.CreateTexture(size, format, levels, samples, count)
member x.CreateTexture(size, format, levels, samples) = x.CreateTexture(size, format, levels, samples, 1)
member x.CreateTextureArray(size, format, levels, samples, count) = x.CreateTexture(size, format, levels, samples, count)
member x.CreateTextureCube(size, format, levels, samples) = x.CreateTextureCube(size, format, levels, samples)
member x.CreateRenderbuffer(size, format, samples) = x.CreateRenderbuffer(size, format, samples)
member x.CreateMappedBuffer() = x.CreateMappedBuffer()
Expand Down
2 changes: 1 addition & 1 deletion src/Application/Aardvark.Application/Screenshots.fs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module Screenshot =
task.Run(null, RenderToken.Empty, desc) |> ignore


let colorTexture = runtime.CreateTexture(size, TextureFormat.Rgba8, 1, 1, 1)
let colorTexture = runtime.CreateTexture(size, TextureFormat.Rgba8, 1, 1)
runtime.ResolveMultisamples(color, colorTexture, ImageTrafo.Rot0)

runtime.Download(colorTexture, PixFormat.ByteRGBA)
Expand Down
2 changes: 1 addition & 1 deletion src/Demo/Examples/Render2TexturePrimitive.fs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module Render2TexturePrimitive =
let runtime = win.Runtime // the runtime instance provides functions for creating resources (lower abstraction than sg)

let size = V2i(1024,768)
let color = runtime.CreateTexture(size, TextureFormat.Rgba8, 1, 1, 1)
let color = runtime.CreateTexture(size, TextureFormat.Rgba8, 1, 1)
let depth = runtime.CreateRenderbuffer(size, RenderbufferFormat.Depth24Stencil8, 1)

// Signatures are required to compile render tasks. Signatures can be seen as the `type` of a framebuffer
Expand Down
2 changes: 1 addition & 1 deletion src/Demo/Examples/Render2TexturePrimitiveFloat.fs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module Render2TexturePrimitiveFloat =
let runtime = win.Runtime // the runtime instance provides functions for creating resources (lower abstraction than sg)

let size = V2i(256,256)
let color = runtime.CreateTexture(size, TextureFormat.Rgba32f, 1, 1, 1)
let color = runtime.CreateTexture(size, TextureFormat.Rgba32f, 1, 1)
let depth = runtime.CreateRenderbuffer(size, RenderbufferFormat.Depth24Stencil8, 1)

// Signatures are required to compile render tasks. Signatures can be seen as the `type` of a framebuffer
Expand Down

0 comments on commit 35ceeac

Please sign in to comment.