Post

Replies

Boosts

Views

Activity

Reply to in Metal, how to sync calculate result in kernel function?
I define my histogram buffer by using a Int32 array, and convert it to a MTLBuffer. I found some atomic code to slove my problem, some code like this. kernel void myCalculateHistogram( texture2dhalf, access::read texture [[texture(0)]],                 uint2 coordinate [[thread_position_in_grid]],                 device  atomic_uint* histogram [[buffer(1)]]) {   if (coordinate.x = texture.get_width() || coordinate.y = texture.get_height()) {     return;   }       half4 colorValue = texture.read(coordinate);   if (configs[14] == 1) {     half gray = grayScale(colorValue);     int histogramIndex = int(clamp(gray, half(0.0), half(1.0)) * 255);     atomic_fetch_add_explicit(&histogram[histogramIndex], 1, memory_order_relaxed);   } } That works like charm. :D
Mar ’21
Reply to UIImage.draw(in:) has a aliasing problem
@Claude31, thank you for your reply, but is not work. I found the solution in UIView.draw function create a CGImageContext with final image pixel size draw UIImage into CGImageContext get CGImage from CGImageContext draw this CGImage into CGContext of UIView.draw it works well. there is my code: override func draw(_ rect: CGRect) {   guard let ctx = UIGraphicsGetCurrentContext() else { return } // fill background with black color   ctx.addRect(bounds)   ctx.setFillColor(UIColor.black.cgColor)   ctx.fillPath()       if var im = image { // image pixel size is 4032 * 4032, so aspect is 1:1=1, // bounds size is logical size (384 * 512), so I need found a max size in bounds and keep my image aspect // so 'size' is (384 * 384)     let size = Math.getMaxSizeWithAspect(size: CGSize(width: bounds.width, height: bounds.height), radioWidthToHeight: im.size.width / im.size.height) // pixelSize = (384 * 3, 384 * 3) = (1152, 1152)     let pixelSize = CGSize(width: size.width * layer.contentsScale, height: size.height * layer.contentsScale) // I create a pixelSize ImageContext     UIGraphicsBeginImageContextWithOptions(pixelSize, true, 1)     guard let imgCtx = UIGraphicsGetCurrentContext() else { return } // draw UIImage into ImageContext     im.draw(in: CGRect(x: 0, y: 0, width: pixelSize.width, height: pixelSize.height)) // get cgImage     guard let cgImg = imgCtx.makeImage() else { return } // there is another strange thing, CGContext draw cgImg will show a vertical flip result // so I have to vertical flip first     ctx.scaleBy(x: 1, y: -1)     ctx.translateBy(x: 0, y: -bounds.height) // draw cgImage into UIView's context     ctx.draw(cgImg, in: CGRect(x: (bounds.width - size.width) / 2, y: (bounds.height - size.height) / 2, width: size.width, height: size.height)) // it works like charm :D   } }
Mar ’21