anchor.diff:              [<GLSLIntrinsic("atomicOr({0}, {1})")>]
               static member AtomicOr(r : 'a, v : 'a) : unit =
                   failwith ""
  -
  -        let encode (index : int) (leading : int) (value : float) : int * uint32 =
  -            if index = 0 then
  -                16, 0xDEAD0000u
  +            [<GLSLIntrinsic("atomicAdd({0}, {1})")>]
  +            static member AtomicAdd(r : 'a, v : 'a) : 'a =
  +                failwith ""
  +
  +
  +        let dcLumInv = Jpeg.Huffman.photoshop.dcLumInv |> Array.map (fun c -> (uint32 c.Length) ||| (uint32 c.Store <<< 16))
  +        let dcChromaInv = Jpeg.Huffman.photoshop.dcChromaInv |> Array.map (fun c -> (uint32 c.Length) ||| (uint32 c.Store <<< 16))
  +        let acLumInv = Jpeg.Huffman.photoshop.acLumInv |> Array.map (fun c -> (uint32 c.Length) ||| (uint32 c.Store <<< 16))
  +        let acChromaInv = Jpeg.Huffman.photoshop.acChromaInv |> Array.map (fun c -> (uint32 c.Length) ||| (uint32 c.Store <<< 16))
  +
  +        let lenMask = 0x000000FFu
  +        let huffMask = 0xFFFF0000u
  +
  +        let encode (index : int) (chroma : bool) (leading : int) (value : float) : int * uint32 =
  +            let value = int value
  +            if chroma then
  +                if index = 0 then
  +                    let dc = Ballot.MSB(uint32 (abs value)) + 1
  +                    let off = if value < 0 then (1 <<< dc) else 0
  +                    let v = uint32 (off + value)
  +                    let huff = dcChromaInv.[dc]
  +                    let len = huff &&& lenMask |> int
  +                    let huff = huff &&& huffMask
  +
  +                    len + dc, huff ||| (v <<< (32 - len + dc))
  +                else
  +                    let dc = Ballot.MSB(uint32 (abs value)) + 1
  +                    let off = if value < 0 then (1 <<< dc) else 0
  +                    let v = uint32 (off + value)
  +                    let huff = acChromaInv.[dc]
  +                    let len = huff &&& lenMask |> int
  +                    let huff = huff &&& huffMask
  +
  +                    len + dc, huff ||| (v <<< (32 - len + dc))
  +
               else
  -                //let value = (uint32 leading <<< 16) ||| (uint32 (32768.0 + value))
  -                8, 0xAB000000u
  +                if index = 0 then
  +                    let dc = Ballot.MSB(uint32 (abs value)) + 1
  +                    let off = if value < 0 then (1 <<< dc) else 0
  +                    let v = uint32 (off + value)
  +                    let huff = dcLumInv.[dc]
  +                    let len = huff &&& lenMask |> int
  +                    let huff = huff &&& huffMask
  +
  +                    len + dc, huff ||| (v <<< (32 - len + dc))
  +                else
  +                    let dc = Ballot.MSB(uint32 (abs value)) + 1
  +                    let off = if value < 0 then (1 <<< dc) else 0
  +                    let v = uint32 (off + value)
  +                    let huff = acLumInv.[dc]
  +                    let len = huff &&& lenMask |> int
  +                    let huff = huff &&& huffMask
  +
  +                    len + dc, huff ||| (v <<< (32 - len + dc))
   
           let takeHigh (cnt : int) (word : uint32) =
               word >>> (32 - cnt)
  
positive.code: 
  
                  if rnz then
                      let rSize, rc = encode rlid (rlz % 16) rv
                      rCode <- rc
                      rLength <- rSize
                  elif rlz > 0 && rlz % 16 = 0 then
  
negative.code:         use app = new HeadlessVulkanApplication(false)
          let device = app.Device
          
          let dct = device |> ComputeShader.ofFunction JpegGPU.dct
          let input = ComputeShader.newInputBinding dct app.Runtime.DescriptorPool
          
          let rand = RandomSystem()
  
          let image = PixImage<byte>(Col.Format.RGBA, V2i(32,32))
          image.GetMatrix<C4b>().SetByCoord(fun (c : V2l) ->
              rand.UniformC3f().ToC4b()
          ) |> ignore
  
          let alignedSize = V2i(next image.Size.X 8, next image.Size.Y 8)
  
          let sourceData = device.CreateImage(PixTexture2d(PixImageMipMap [| image :> PixImage|], TextureParams.empty))
          let targetBuffer = device.CreateBuffer<V4f>(int64 alignedSize.X * int64 alignedSize.Y)
  
          input.["size"] <- alignedSize
          input.["target"] <- targetBuffer
          input.["InputImage"] <- sourceData
          input.Flush()
  
          device.perform {
              do! Command.Bind dct
              do! Command.SetInputs input
              do! Command.Dispatch (alignedSize / V2i(8,8))
              do! Command.Sync(targetBuffer, VkAccessFlags.ShaderWriteBit, VkAccessFlags.TransferReadBit)
          }
  
  
          let encode = device |> ComputeShader.ofFunction JpegGPU.compact
          let input = ComputeShader.newInputBinding encode app.Runtime.DescriptorPool
  
  
          //let data = device.CreateBuffer<V4f>(Array.init 64 (fun i -> if i % 3 = 0 then V4f(i,i,i,i) else V4f.Zero))
          let blocks = (alignedSize.X * alignedSize.Y) / 64
          let ranges = device.CreateBuffer<V2i>(int64 blocks)
          let b = device.CreateBuffer<uint32>(64L * int64 blocks)
          let counter = device.CreateBuffer<int>(Array.zeroCreate 1)
  
          input.["channel"] <- 0
          input.["data"] <- targetBuffer
          input.["ranges"] <- ranges
          input.["counter"] <- counter
          input.["mask"] <- b
          input.Flush()
          
          device.perform {
              do! Command.Bind encode
              do! Command.SetInputs input
              do! Command.Dispatch blocks
          }
  
          let cnt = Array.zeroCreate 1
          counter.Download(cnt)
  
          let arr = Array.zeroCreate cnt.[0]
          b.Download(0L, arr, 0L, arr.LongLength)
  
          let r = Array.zeroCreate blocks
          ranges.Download(r)
     
  
          printfn "%A (%A) %A" arr.[0] r.[0] cnt.[0]
  
  
          
  
          let arr = Array.zeroCreate (alignedSize.X * alignedSize.Y)
          targetBuffer.Download(arr)
  