@@ -247,96 +247,66 @@ final class CameraVideoLayer: MetalEngineSubjectLayer, MetalEngineSubject {
247247 computeEncoder. endEncoding ( )
248248 } )
249249
250- if !self . blurredLayer. isHidden {
251- guard let downscaledTexture = self . downscaledTexture? . get ( context: context) , let blurredHorizontalTexture = self . blurredHorizontalTexture? . get ( context: context) , let blurredVerticalTexture = self . blurredVerticalTexture? . get ( context: context) else {
252- return
250+ guard let downscaledTexture = self . downscaledTexture? . get ( context: context) , let blurredHorizontalTexture = self . blurredHorizontalTexture? . get ( context: context) , let blurredVerticalTexture = self . blurredVerticalTexture? . get ( context: context) else {
251+ return
252+ }
253+
254+ let blurredTexture = context. compute ( state: BlurState . self, inputs: rgbaTexture. placeholer, downscaledTexture. placeholer, blurredHorizontalTexture. placeholer, blurredVerticalTexture. placeholer, commands: { commandBuffer, blurState, rgbaTexture, downscaledTexture, blurredHorizontalTexture, blurredVerticalTexture -> MTLTexture ? in
255+ guard let rgbaTexture, let downscaledTexture, let blurredHorizontalTexture, let blurredVerticalTexture else {
256+ return nil
253257 }
254258
255- let blurredTexture = context. compute ( state: BlurState . self, inputs: rgbaTexture. placeholer, downscaledTexture. placeholer, blurredHorizontalTexture. placeholer, blurredVerticalTexture. placeholer, commands: { commandBuffer, blurState, rgbaTexture, downscaledTexture, blurredHorizontalTexture, blurredVerticalTexture -> MTLTexture ? in
256- guard let rgbaTexture, let downscaledTexture, let blurredHorizontalTexture, let blurredVerticalTexture else {
257- return nil
258- }
259-
260- blurState. downscaleKernel. encode ( commandBuffer: commandBuffer, sourceTexture: rgbaTexture, destinationTexture: downscaledTexture)
261-
262- do {
263- guard let computeEncoder = commandBuffer. makeComputeCommandEncoder ( ) else {
264- return nil
265- }
266-
267- let threadgroupSize = MTLSize ( width: 16 , height: 16 , depth: 1 )
268- let threadgroupCount = MTLSize ( width: ( downscaledTexture. width + threadgroupSize. width - 1 ) / threadgroupSize. width, height: ( downscaledTexture. height + threadgroupSize. height - 1 ) / threadgroupSize. height, depth: 1 )
269-
270- computeEncoder. setComputePipelineState ( blurState. computePipelineStateHorizontal)
271- computeEncoder. setTexture ( downscaledTexture, index: 0 )
272- computeEncoder. setTexture ( blurredHorizontalTexture, index: 1 )
273- computeEncoder. dispatchThreadgroups ( threadgroupCount, threadsPerThreadgroup: threadgroupSize)
274-
275- computeEncoder. setComputePipelineState ( blurState. computePipelineStateVertical)
276- computeEncoder. setTexture ( blurredHorizontalTexture, index: 0 )
277- computeEncoder. setTexture ( blurredVerticalTexture, index: 1 )
278- computeEncoder. dispatchThreadgroups ( threadgroupCount, threadsPerThreadgroup: threadgroupSize)
279-
280- computeEncoder. endEncoding ( )
281- }
282-
283- return blurredVerticalTexture
284- } )
259+ blurState. downscaleKernel. encode ( commandBuffer: commandBuffer, sourceTexture: rgbaTexture, destinationTexture: downscaledTexture)
285260
286- context . renderToLayer ( spec : renderSpec , state : RenderState . self , layer : self . blurredLayer , inputs : blurredTexture , commands : { encoder , placement , blurredTexture in
287- guard let blurredTexture else {
288- return
261+ do {
262+ guard let computeEncoder = commandBuffer . makeComputeCommandEncoder ( ) else {
263+ return nil
289264 }
290- let effectiveRect = placement. effectiveRect
291-
292- var rect = SIMD4 < Float > ( Float ( effectiveRect. minX) , Float ( effectiveRect. minY) , Float ( effectiveRect. width) , Float ( effectiveRect. height) )
293- encoder. setVertexBytes ( & rect, length: 4 * 4 , index: 0 )
294265
295- var mirror = SIMD2 < UInt32 > (
296- videoTextures. mirrorDirection. contains ( . horizontal) ? 1 : 0 ,
297- videoTextures. mirrorDirection. contains ( . vertical) ? 1 : 0
298- )
299- encoder. setVertexBytes ( & mirror, length: 2 * 4 , index: 1 )
266+ let threadgroupSize = MTLSize ( width: 16 , height: 16 , depth: 1 )
267+ let threadgroupCount = MTLSize ( width: ( downscaledTexture. width + threadgroupSize. width - 1 ) / threadgroupSize. width, height: ( downscaledTexture. height + threadgroupSize. height - 1 ) / threadgroupSize. height, depth: 1 )
300268
301- encoder. setFragmentTexture ( blurredTexture, index: 0 )
269+ computeEncoder. setComputePipelineState ( blurState. computePipelineStateHorizontal)
270+ computeEncoder. setTexture ( downscaledTexture, index: 0 )
271+ computeEncoder. setTexture ( blurredHorizontalTexture, index: 1 )
272+ computeEncoder. dispatchThreadgroups ( threadgroupCount, threadsPerThreadgroup: threadgroupSize)
302273
303- var brightness : Float = 0.85
304- var saturation : Float = 1.3
305- var overlay : SIMD4 < Float > = SIMD4 < Float > ( )
306- encoder. setFragmentBytes ( & brightness, length: 4 , index: 0 )
307- encoder. setFragmentBytes ( & saturation, length: 4 , index: 1 )
308- encoder. setFragmentBytes ( & overlay, length: 4 * 4 , index: 2 )
274+ computeEncoder. setComputePipelineState ( blurState. computePipelineStateVertical)
275+ computeEncoder. setTexture ( blurredHorizontalTexture, index: 0 )
276+ computeEncoder. setTexture ( blurredVerticalTexture, index: 1 )
277+ computeEncoder. dispatchThreadgroups ( threadgroupCount, threadsPerThreadgroup: threadgroupSize)
309278
310- encoder. drawPrimitives ( type: . triangle, vertexStart: 0 , vertexCount: 6 )
311- } )
312- }
313-
314- // context.renderToLayer(spec: renderSpec, state: RenderState.self, layer: self, inputs: rgbaTexture.placeholer, commands: { encoder, placement, rgbaTexture in
315- // guard let rgbaTexture else {
316- // return
317- // }
318- //
319- // let effectiveRect = placement.effectiveRect
320- //
321- // var rect = SIMD4<Float>(Float(effectiveRect.minX), Float(effectiveRect.minY), Float(effectiveRect.width), Float(effectiveRect.height))
322- // encoder.setVertexBytes(&rect, length: 4 * 4, index: 0)
323- //
324- // var mirror = SIMD2<UInt32>(
325- // videoTextures.mirrorDirection.contains(.horizontal) ? 1 : 0,
326- // videoTextures.mirrorDirection.contains(.vertical) ? 1 : 0
327- // )
328- // encoder.setVertexBytes(&mirror, length: 2 * 4, index: 1)
329- //
330- // encoder.setFragmentTexture(rgbaTexture, index: 0)
331- //
332- // var brightness: Float = 1.0
333- // var saturation: Float = 1.0
334- // var overlay: SIMD4<Float> = SIMD4<Float>()
335- // encoder.setFragmentBytes(&brightness, length: 4, index: 0)
336- // encoder.setFragmentBytes(&saturation, length: 4, index: 1)
337- // encoder.setFragmentBytes(&overlay, length: 4 * 4, index: 2)
338- //
339- // encoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 6)
340- // })
279+ computeEncoder. endEncoding ( )
280+ }
281+
282+ return blurredVerticalTexture
283+ } )
284+
285+ context. renderToLayer ( spec: renderSpec, state: RenderState . self, layer: self . blurredLayer, inputs: blurredTexture, commands: { encoder, placement, blurredTexture in
286+ guard let blurredTexture else {
287+ return
288+ }
289+ let effectiveRect = placement. effectiveRect
290+
291+ var rect = SIMD4 < Float > ( Float ( effectiveRect. minX) , Float ( effectiveRect. minY) , Float ( effectiveRect. width) , Float ( effectiveRect. height) )
292+ encoder. setVertexBytes ( & rect, length: 4 * 4 , index: 0 )
293+
294+ var mirror = SIMD2 < UInt32 > (
295+ videoTextures. mirrorDirection. contains ( . horizontal) ? 1 : 0 ,
296+ videoTextures. mirrorDirection. contains ( . vertical) ? 1 : 0
297+ )
298+ encoder. setVertexBytes ( & mirror, length: 2 * 4 , index: 1 )
299+
300+ encoder. setFragmentTexture ( blurredTexture, index: 0 )
301+
302+ var brightness : Float = 0.85
303+ var saturation : Float = 1.3
304+ var overlay : SIMD4 < Float > = SIMD4 < Float > ( )
305+ encoder. setFragmentBytes ( & brightness, length: 4 , index: 0 )
306+ encoder. setFragmentBytes ( & saturation, length: 4 , index: 1 )
307+ encoder. setFragmentBytes ( & overlay, length: 4 * 4 , index: 2 )
308+
309+ encoder. drawPrimitives ( type: . triangle, vertexStart: 0 , vertexCount: 6 )
310+ } )
341311 }
342312}
0 commit comments