Skip to content

Commit

Permalink
Updating offscreen code
Browse files Browse the repository at this point in the history
Offscreen code was not updated after API changes.
  • Loading branch information
arhik committed Oct 28, 2023
1 parent 8aa8588 commit b911ef8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
21 changes: 12 additions & 9 deletions examples/triangleOffscreen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,20 @@ cshader =
bindingLayouts = []
bindings = []

(bindGroupLayouts, bindGroup) =
WGPUCore.makeBindGroupAndLayout(gpuDevice, bindingLayouts, bindings)
pipelineLayout = WGPUCore.createPipelineLayout(
gpuDevice,
"PipeLineLayout",
bindingLayouts,
bindings
)

pipelineLayout = WGPUCore.createPipelineLayout(gpuDevice, "PipeLineLayout", bindGroupLayouts)
# swapChainFormat = wgpuSurfaceGetPreferredFormat(canvas.surface[], gpuDevice.adapter.internal[])
swapChainFormat = WGPUCore.getPreferredFormat(canvas)
@info swapChainFormat
presentContext = WGPUCore.getContext(canvas)
ctxtSize = WGPUCore.determineSize(presentContext[]) .|> Int
ctxtSize = WGPUCore.determineSize(presentContext) .|> Int

WGPUCore.config(presentContext, device = gpuDevice, format = swapChainFormat)
WGPUCore.config(presentContext |> Ref, device = gpuDevice, format = swapChainFormat)

renderpipelineOptions = [
WGPUCore.GPUVertexState =>
Expand Down Expand Up @@ -109,14 +112,14 @@ try
[ "CopyDst", "CopySrc"],
false,
)
nextTexture = WGPUCore.getCurrentTexture(presentContext[]) |> Ref
nextTexture = WGPUCore.getCurrentTexture(presentContext)
cmdEncoder = WGPUCore.createCommandEncoder(gpuDevice, "cmdEncoder")
renderPassOptions =
[
WGPUCore.GPUColorAttachments => [
:attachments => [
WGPUCore.GPUColorAttachment => [
:view => nextTexture[],
:view => nextTexture,
:resolveTarget => C_NULL,
:clearValue => (0.0, 0.0, 0.0, 1.0),
:loadOp => WGPULoadOp_Clear,
Expand All @@ -134,7 +137,7 @@ try
WGPUCore.copyTextureToBuffer(
cmdEncoder,
[
:texture => nextTexture[],
:texture => nextTexture,
:mipLevel => 0,
:origin => [:x => 0, :y => 0, :z => 0] |> Dict,
] |> Dict,
Expand All @@ -154,7 +157,7 @@ try
] |> Dict,
)
WGPUCore.submit(gpuDevice.queue, [WGPUCore.finish(cmdEncoder)])
WGPUCore.present(presentContext[])
WGPUCore.present(presentContext)
data = WGPUCore.readBuffer(gpuDevice, outputBuffer, 0, bufferSize |> Int)
datareshaped = reshape(data, (4, ((512, 500) |> reverse)...) .|> Int) #hardcoded
img = reinterpret(RGBA{N0f8}, datareshaped) |> (x) -> reshape(x, (512, 500)) #hardcoded
Expand Down
18 changes: 9 additions & 9 deletions src/offscreen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function defaultCanvas(::Type{OffscreenCanvas})
nothing,
false,
false,
backend.device,
device,
nothing,
)

Expand All @@ -54,7 +54,7 @@ mutable struct GPUCanvasContextOffscreen
device::Any
currentTexture::Any
currentTextureView::Any
format::WGPUTextureFormat
format::Union{Nothing, WGPUTextureFormat}
usage::WGPUTextureUsage
compositingAlphaMode::Any
size::Any
Expand Down Expand Up @@ -104,7 +104,7 @@ function unconfig(a::T) where {T}
end

function configure(
canvasContext::GPUCanvasContextOffline;
canvasContext::GPUCanvasContextOffscreen;
device,
format,
usage,
Expand All @@ -120,40 +120,40 @@ function configure(
canvasContext.size = size
end

function unconfigure(canvasContext::GPUCanvasContextOffline)
function unconfigure(canvasContext::GPUCanvasContextOffscreen)
canvasContext.device = nothing
canvasContext.format = nothing
canvasContext.usage = nothing
canvasContext.compositingAlphaMode = nothing
canvasContext.size = nothing
end

function determineSize(cntxt::GPUCanvasContextOffline)
function determineSize(cntxt::GPUCanvasContextOffscreen)
psize = cntxt.physicalSize
cntxt.logicalSize = psize ./ cntxt.pixelRatio
end

function getPreferredFormat(canvasContext::GPUCanvasContextOffline)
function getPreferredFormat(canvasContext::GPUCanvasContextOffscreen)
canvas = canvasCntxt.canvasRef[]
if canvas != nothing
return getPreferredFormat(canvas)
end
return getEnum(WGPUTextureFormat, "RGBA8Unorm")
end

function getCurrentTexture(cntxt::GPUCanvasContextOffline)
function getCurrentTexture(cntxt::GPUCanvasContextOffscreen)
createNewTextureMaybe(cntxt)
return cntxt.currentTextureView
end

function present(cntxt::GPUCanvasContextOffline)
function present(cntxt::GPUCanvasContextOffscreen)
if cntxt.currentTexture != nothing && cntxt.currentTexture.internal[] != C_NULL
canvas = cntxt.canvasRef[]
return present(canvas, cntxt.currentTextureView)
end
end

function createNewTextureMaybe(canvasCntxt::GPUCanvasContextOffline)
function createNewTextureMaybe(canvasCntxt::GPUCanvasContextOffscreen)
canvas = canvasCntxt.canvasRef[]
pSize = canvasCntxt.physicalSize
if pSize == canvasCntxt.surfaceSize
Expand Down

0 comments on commit b911ef8

Please sign in to comment.