Saturday, September 30, 2017

Metal Swift usage of Command Buffer with complex Kernel(s) computations and more

Advice for working with complex kernel encoding computations:

-Account for all commandBuffer instances and .commit() s its important since any added instance without a .commit on the commandBuffer in a draw loop is a memory leak.  May not show on XCode Instruments stack trace but will show on Debug Memory Graph while running in Debug mode (see https://medium.com/@xcadaverx/locating-the-source-of-a-memory-leak-712667bf8cd5)

Important to enable malloc stack tracing to see the code source of memory leaks.  Inevitably if you have an instanced commandBuffer that isn't committed in loop will source back to its instance creation as a culprit.  The remedy is easily in loop committing this instanced commandBuffer.

Why re instance commandBuffer?

As it turns out with kernel computations if you are doing complex kernel computation work with multiple pass encodings sequentially.  You can singly instance any device buffers that need be passed to the GPU on kernel instancing (usually at the outside of your viewcontrollers initialization) and then re write to these buffers with memcpy() functions

for instance:

        let hillbufferptr = hilllockbuffer?.contents()
        memcpy(hillbufferptr, &(hilllockLabelMap!),hilllockByteLength)
        let relabelbufferptr = relabelbuffer?.contents()
        memcpy(relabelbufferptr, &(relabelMap!), relabelByteLength)
        let maxhbufferptr = maxhbuffer?.contents()
        memcpy(maxhbufferptr, &(maxHMap!), maxhByteLength)
        plist = [Float32](repeating: -1.0, count: (hillockmatrixtexture?.width)!*(hillockmatrixtexture?.height)!)
        let plistbufferptr = plistbuffer?.contents()
        memcpy(plistbufferptr, &(plist!), plistByteLength)

Then buffers need not be instanced in loop.  Also important instancing textures outside of draw loop otherwise these can translate into memory leaks if not properly dealt with in terms of deallocation.

Anytime data retrieval occurs from a kernel encoding  requires a commandBuffer.commit() and waitUntilCompleted() method...this translates into a new instancing (as far as I can tell) of a commandBuffer.  The memory on the old command Buffer is freed otherwise.

Strategy for complex kernel pass encoding with multiple kernels passing a given data buffer from one kernel to the next.  My advice (as per Apple's direct advice) avoid running a series of commandBuffer.commit() and waitUntilCompleted() method calls for buffer to array call backs only to in turn re write these back to the buffers.  Instead use a single buffer and pass that same pointer buffer from one encoding kernel to the next.  Callback instancing memory with byte copies to an array is slow and will likely cause GPU to hang error messages...or as an engineer describes this is merely serializing data flow from the CPU to the GPU.  It is slow and cumbersome.   I personally found only one instance where CPU processing data was used...my instance was CPU processing array data needed to sort and create set instancing of array data: sorting an array and eliminating duplicate values.  This data would in turn determine the iterative structure of added encoding necessary to the algorithm.

I haven't found a recipe nor have been able to construct an adequate recipe in passing something like float array data into structs and pointing to the struct with such objects since working with instanced struct data (unless it is in pointer form) in the struct on the GPU side has a discretely defined array container size...instead I've just passed array pointers directly to the kernel functions as needed.  Plenty of code recipes for this.

Monday, September 25, 2017

Guild Wars 2 Path of Fire -The Departing Eater of Souls strategy

Early spoiler beta.  The eater of souls will regenerate health after leap attack with tractor beam health siphon.  The strategy to this fight is to run opposite the beam and do a double evade on it at the same time when he launches the health drain.  When far enough away the siphon will do no damage and the Eater of Souls regenerates no health. 

Thursday, September 14, 2017

Dynamic map storage on a texture map in Metal 2(Apple)

   The idea is pretty straightforward here.  Namely, to implement this form of storage, you will probably want a texture that has at least 2 channels, but this can also work for 4 channels with 2 channels reserved for the key value map storage.  

It would be noted that in fact the 'dynamic' storage portion of this storage is actually reserved storage space containing texture.width*texture.height possible indices for storage.  Keying without any layer of lexical enumeration between indices would be an added implementation beyond the scope of what I'd like to cover in this article.  So it is assumed that the indices given by the total range on the set, will serve directly as the key value, and thus the mapped key will have such value.  Thus the key 1 is mapped to index 1 on the texture storage set.  

To map these hash values means then to have some storage value likely a boolean value 1 indicating the hash bucket is reserved,  or 0 that it isn't reserved.

The formulation for converting from a 2d array to 1d array is pretty simple.  

y = indexkey/ texture.width
x = indexkey% texture.width

Thus in metal if your texture point is set with a half4 vector

You can say reserve at such index by accessing (via sample or write) the position:
where 
samp = sampler
uint2 indexval = (indexkey/texture.width, indexkey%texture.width);
half4 color = currenttexture.sample(samp, indexval);
color.zw = half2(1.0, value);
desttexture.write(color,indexval);

Thus mapping to latter two channels on destination texture transposing first 2 channel values from the original texture.  

Lookups then are pretty straight forward if the key is known just plug into x,y formulation above and control check for boolean positive (storage allocation) and then its corresponding value pair.  Noted this is for half format numbers.

Oblivion

 Between the fascination of an upcoming pandemic ridden college football season, Taylor Swift, and Kim Kardashian, wildfires, crazier weathe...