metal

Take a snapshot of current screen with Metal in swift

孤街浪徒 提交于 2019-12-18 05:08:08
问题 I tried: let scale = UIScreen.mainScreen().scale UIGraphicsBeginImageContextWithOptions(metalLayer.bounds.size,false,scale) // metalLayer.renderInContext(UIGraphicsGetCurrentContext()!) // self.view.layer ... metalLayer.presentationLayer()!.renderInContext(UIGraphicsGetCurrentContext()!) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext(); UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil) But the result is a empty screenshot. Any help would be nice! Please

Capture Metal MTKView as Movie in realtime?

时光总嘲笑我的痴心妄想 提交于 2019-12-18 02:48:27
问题 What is the most efficient way to capture frames from a MTKView ? If possible, I would like to save a .mov file from the frames in realtime. Is it possible to render into an AVPlayer frame or something? It is currently drawing with this code (based on @warrenm PerformanceShaders project): func draw(in view: MTKView) { _ = inflightSemaphore.wait(timeout: DispatchTime.distantFuture) updateBuffers() let commandBuffer = commandQueue.makeCommandBuffer() commandBuffer.addCompletedHandler{ [weak

Add alpha value when create image from MTLTexture

南笙酒味 提交于 2019-12-13 03:48:22
问题 I am creating UIImage from current drawable texture as follows. func createImageFromCurrentDrawable() ->(UIImage){ let context = CIContext() let texture = metalView.currentDrawable!.texture let kciOptions = [kCIContextWorkingColorSpace: CGColorSpace(name: CGColorSpace.sRGB)!, kCIContextOutputPremultiplied: true, kCIContextUseSoftwareRenderer: false] as [String : Any] let cImg = CIImage(mtlTexture: texture, options: kciOptions)! let cgImg = context.createCGImage(cImg, from: cImg.extent)! let

Metal texture - Draw and Erase

时光总嘲笑我的痴心妄想 提交于 2019-12-13 03:46:43
问题 I am trying to modify Apple's sample GLPaint (paint app using OpenGL) to use Metal instead of OpenGL. I am able to render a brush stroke to screen using Metal, but am having difficulties "erasing" it. In Metal, I am using the following blending parameters: renderPipelineDescriptor.colorAttachments[0].isBlendingEnabled = true renderPipelineDescriptor.colorAttachments[0].rgbBlendOperation = .add renderPipelineDescriptor.colorAttachments[0].alphaBlendOperation = .add renderPipelineDescriptor

Repitition of texture on pan gesture in Metal Shading Language

若如初见. 提交于 2019-12-13 03:41:31
问题 I am new to metal shading language.So your kind guidance would be so helpful. I just repeated texture using metal as shown on below image (Image sequence). But it seems not smoothy.How can I make it to be appeared as smoothy line as done in following link for metal points.(overlapping Metal Point primitives and blending) At the moment what I do is allocating memory for vertex buffer (MTLBuffer) at each touch point in my gesture. code: func allocateMemoryForVetexBuffer(vertices: Array<Vertex>)

How to use a 3x3 2D transformation in a vertex/fragment shader (Metal)

梦想的初衷 提交于 2019-12-13 02:37:43
问题 I have a supposedly simple task, but apparently I still don't understand how projections work in shaders. I need to do a 2D perspective transformation on a texture quad (2 triangles), but visually it doesn't look correct (e.g. trapezoid is slightly higher or more stretched than what it is in the CPU version). I have this struct: struct VertexInOut { float4 position [[position]]; float3 warp0; float3 warp1; float3 warp2; float3 warp3; }; And in the vertex shader I do something like ( texCoords

Creating globally accessible constant buffer in Metal Shading Language

谁都会走 提交于 2019-12-12 18:25:35
问题 I've got a question about constant buffers in Metal. Let's assume, that I've got something like: ...list of includes goes here... using namespace metal; struct ConstantBuffer { float ANY_VALUE; }; struct VS_INPUTS { float4 i_pos_ms [ [ attribute ( 0 ) ] ] ; } ; struct V2P_STRUCT { float4 v_pos_out [ [ position ] ] ; } ; float3 CalcSomething() { return float3(ANY_VALUE, ANY_VALUE, ANY_VALUE); // !!!!!!!! } vertex V2P_STRUCT VertexFunc(VS_INPUTS vs_inputs [ [ stage_in ] ] , constant

In Metal, does setting vertex and fragment buffer to same MTLBuffer copies it to GPU only once?

て烟熏妆下的殇ゞ 提交于 2019-12-12 17:07:34
问题 I am passing a uniform buffer to both vertex and fragment shaders. let uniformBuffer = device.makeBuffer(length: 4096, options: []) renderEncoder.setVertexBuffer(uniformBuffer, offset: 0, at: 1) renderEncoder.setFragmentBuffer(uniformBuffer,offset:0, at: 1) Does this copy the uniformBuffer from CPU to GPU twice? Then I will instead pass the buffer from vertex shader to fragment shader which happens just inside the GPU. 回答1: There are no copies involved specifically with setVertexBuffer() or

GLKit vs. Metal perspective matrix difference

坚强是说给别人听的谎言 提交于 2019-12-12 08:55:56
问题 I'm reading a Metal tutorial on raywenderlich.com, where it introduces a pure Swift float4x4 helper class. 99% it's just wrapper around GLKit functions, except one function which really puzzles me: static func makePerspectiveViewAngle(_ fovyRadians: Float, aspectRatio: Float, nearZ: Float, farZ: Float) -> float4x4 { var q = unsafeBitCast(GLKMatrix4MakePerspective(fovyRadians, aspectRatio, nearZ, farZ), to: float4x4.self) let zs = farZ / (nearZ - farZ) q[2][2] = zs q[3][2] = zs * nearZ return

Crop and scale MTLTexture

北城余情 提交于 2019-12-12 08:50:02
问题 Can I create a new MTLTexture of dimensions w2/h2 of an existing MTLTexture region x1/y1/w1/h1 ? PS: I thought about using MTLTexture.buffer?.makeTexture but the offset needs to be 64 bytes. Why? 回答1: Here's an example of how you might do this with MPSImageLanczosScale . Note that sourceRegion is expressed in the pixel coordinate system of the source texture, and destRegion should be equal to the full area of the destination texture (note that it specifically doesn't account for the origin of