Post

Replies

Boosts

Views

Activity

NSButton Failing to Reflect a Change of its Image
[object Object]In a project I am developing with Xcode a 2d layout of the faces of Rubik's cube is displayed (see the attached Screenshot). In the center of each face's representation is a NSColorWell. The other eight cubes on that face are represented as NSButtons displaying an NSImage of the appropriate color. In response to the user selecting a new color the program resets the eight Button's image to one of the new color. The problem is that this change is not reflected on the display even if the NSButton is sent a setNeedsDisplay message. Sending the NSButton a display message also has no effect. The change is not seen until the Button receives a mouse click or interestingly if the window is dragged from the main screen to a second screen. I believe this a bug in AppKit.
4
1
1.2k
Nov ’22
GCD dispatch_apply()
In the code snippet below, I am trying to have a routine run concurrently with GCD. However, the queue runs the code serially. How would one force the queue to run concurrently? The WWDC 2017 VIDEO shows: dispatch_apply( DISPATCH_APPLY_AUTO , count , ^(size_t i) { ...} ); but Xcode doesn't seem to recognize this syntax. Would there be a value for the flag parameter of dispatch_get_global_queue that would force concurrency. code-block dispatch_queue_t aQueue = dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0); dispatch_apply( turnsMax , aQueue , ^( size_t t ) {
3
0
1.2k
Dec ’21
Random Numbers
What is the approved method of getting a random number in macOS in Objective C? Presently I'm using: srandomdev(); randomDevice = fopen("/dev/random", "r"); // // // -(uint64)random64 { uint64_t value = 0; int i; for (i = 0 ; i < sizeof(value); i++) { value <<= 8; value |= fgetc(randomDevice); } return value; } However, this method no longer appears in the documentation. Is there a more modern implementation?
3
0
1.2k
Nov ’21
Help Books not working
After upgrading to Monterey I found that the Help system wasn't displaying my Help Books. After wasting an hour or so I figured out the the Help system will not display a Help Book unless the app was launched from the Applications folder. So, if you're launching the app from Xcode the Help Book won't work. If you want to see if the Help Book is properly configured you have to dig into the project's DerivedData folder, drag the app to the Applications folder and launch it from there.
1
0
672
Nov ’21
NSKeyedArchiver
I am having trouble with NSKeyedArchiver. I create an archive with archive = [NSDictionary dictionaryWithObjectsAndKeys: pruneTableP2, @"depth table", nil]; archiveData = [NSKeyedArchiver archivedDataWithRootObject: archive requiringSecureCoding: NO error: nil]; I then get a "The data isn’t in the correct format" error when I go to unarchive it with unarchivedObjectOfClass:. The subsequent call to a depreciated method works however. archive = [NSKeyedUnarchiver unarchivedObjectOfClass: [NSDictionary class] fromData: fileData error: &error]; if( archive == nil ) { archive = [NSKeyedUnarchiver unarchiveObjectWithData: fileData]; NSLog( @"%@", [error localizedFailureReason]); } What am I doing wrong?
7
0
1.5k
Nov ’21
Texture Pixel layout in Metal
I'm working on an app in MacOS in which I need to do mouse picking in a MTKView. To do this I render a Hit Triangle map of the mouse active areas into a MTLTexture. Since these areas do not change at run time the map only has to be rendered once. The metal debugger noticed this and suggested the texture resources be made purgeable. So I copied the pixel data out of the texture and marked the the texture as purgeable. I then read the pixel data from the C array and the mouse picking was screwed up. The Hit Triangles were not where they were supposed to be. I fussed around a bit and by trial and error I found out the pixels were copied out into the C array in column major order rather than row major order. I looked in the Metal documentation and couldn't find anywhere where this was pointed out. Is there anywhere in the Metal docs where it specifies how the pixels are laid out in a texture and how that relates to the viewable coordinates? This is not first time I've had problems related to this. Little details like this can really hang you up. // Since the hit map only has to be made once the data is copied out // and the texture is made purgeable. This to stop the debugger // from nagging about it. [pickTexture getBytes: hitTriangleMap bytesPerRow: PICK_TEXTURE_SIZE * sizeof(uint8 [4]) fromRegion: MTLRegionMake2D( 0 , 0 , PICK_TEXTURE_SIZE, PICK_TEXTURE_SIZE) mipmapLevel: 0]; [pickTexture setPurgeableState: MTLPurgeableStateVolatile]; [pickDepth setPurgeableState: MTLPurgeableStateVolatile]; // // * // * // * // // Previous inefficient version // // [pickTexture getBytes: hit // bytesPerRow: PICK_TEXTURE_SIZE * sizeof(uint8 [4]) // fromRegion: MTLRegionMake2D( hitPoint.x , hitPoint.y , 1, 1) // mipmapLevel: 0]; // New Version Column major arrays?? In any event it works this way. x = hitPoint.x; y = hitPoint.y; hit = hitTriangleMap[y][x]; //Inverted subscripts!!! if( hit[0] <= Zs) { cursorHit = hit[0]; if( cursorOverHitArea == NO) [[NSCursor pointingHandCursor] set]; cursorOverHitArea = YES; }```
2
0
799
Jun ’21
Metal Demo: Hit Test , Mouse Pick , Shadows
I am a hobbyist programmer and I have been fooling around with Metal. In getting my feet wet in metal I have found that there is a pretty big gap between the HelloTriangle and DeferredLighting sample code from Apple and a more intermediate sample would be helpful. I have stumbled along through the brush and put together a little app that I thought that other novice metal programmers might find interesting. Features: Vertex buffer creation in Object Space. Composing objects into a scene in Model Space. Transforming a scene in Model Space to Clip Space Hit Testing / Mouse Picking Two pass rendering to produce shadows. I have put the project on GitHub for others to peruse: github.com/MackenzieBD/Sample-Code/blob/main/MetalDemo.zip
2
0
1.1k
Dec ’20
Shadows: Using a shadow map
I am trying to render shadows in a Metal view. I am using two render passes. In the first pass the view is rendered into a 2d depth texture from the point of view of a point light source. This writes into the depth texture a shadow map of the minimum depths in the view . In the second pass the view is rendered to the viewport. In this second pass the vertex shader transposes the input position to the point light POV giving a shadowSpacePosition. In the fragment shader the depth of a point in the shadow map is compared to the depth of the shadowSpacePosition and the result is used to modify color of that point. The first pass seems to be working as desired. The shadow map shown in a frame capture looks good. The problem I am having is that I don't know how the coordinates used in shadowSpacePosition relate the to coordinates in the shadow map. How do I transpose the x,y,z of the shadowPosition so that the comparison function gives the proper result? cmp = shadowTexture.sample_compare(shadowSampler, shadowPosition.xy , shadowPosition.z); The shadowPosition coordinates are in world space relative to the light position frustum with the origin at the light. I don't know what the above comparison function expects. Texture coordinates clamped 0 to 1? Any insights would be appreciated. Below are the relevant shaders: // Vertex shader outputs and fragment shader inputs typedef struct { &#9;&#9;float4 clipSpacePosition [[position]]; &#9;&#9;float4 eyeSpacePosition; &#9;&#9;float4 shadowSpacePosition; &#9;&#9;float2 textureCoordinate; &#9;&#9;float4 normal; &#9;&#9;float4 color; } RasterizerData; // ******************************************************* // ******** Shaders for Textured Models ************ // ******************************************************* // Vertex function vertex RasterizerData vertexTexShader ( &#9;&#9;uint&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; vertexID [[vertex_id]], &#9;&#9;constant HT_Vertex&#9;&#9; *vertices [[buffer(HT_Vertex_Index)]], &#9;&#9;constant HT_Uniform&#9;&#9;*param [[buffer(HT_Uniform_Index)]] ) { &#9;&#9;RasterizerData&#9;out; &#9;&#9;vector_float4&#9;newPosition = float4( vertices[vertexID].position , 1 ); &#9;&#9;newPosition = param->nodeTransform[ vertices[vertexID].pickID ] * newPosition; &#9;&#9;out.eyeSpacePosition = newPosition; &#9;&#9;out.clipSpacePosition = param->perspectiveTransform * newPosition; &#9;&#9;out.shadowSpacePosition = param->toLampPOV * newPosition; &#9;&#9;out.shadowSpacePosition = param->shadowPerspective * newPosition; &#9;&#9;// Apply rotations about the origin to the normals &#9;&#9;vector_float4 newNormal = float4( vertices[vertexID].normal , 1); &#9;&#9;out.normal = param->normalsTransform[vertices[vertexID].pickID] * newNormal; &#9;&#9;out.textureCoordinate = vertices[vertexID].textCoor; &#9;&#9;return out; } // Fragment function fragment float4 fragmentTexShader ( &#9;&#9;RasterizerData&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;in [[stage_in]], &#9;&#9;constant HT_Uniform&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; *param [[buffer(HT_Uniform_Index)]], &#9;&#9;texture2d<half>&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; colorTexture [[ texture(HT_Texture_Index) ]], &#9;&#9;depth2d<float , access::sample>&#9;&#9; shadowTexture [[ texture(HT_Shadow_Index)]], &#9;&#9;sampler&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; textureSampler [[sampler(HT_Texture_Index)]] ) { &#9;&#9;vector_float4&#9; outColor; &#9;&#9;vector_float3&#9; lightDirection; &#9;&#9;float&#9;&#9;&#9;&#9;&#9; lightDistance; &#9;&#9;float&#9;&#9;&#9;&#9;&#9; attenuation; &#9;&#9;vector_float3&#9; halfVector; &#9;&#9;float&#9;&#9;&#9;&#9;&#9; diffuse; &#9;&#9;float&#9;&#9;&#9;&#9;&#9; specular; &#9;&#9;float&#9;&#9;&#9;&#9;&#9; cmp; &#9;&#9;float&#9;&#9;&#9;&#9;&#9; coorRatio; &#9;&#9;vector_float3&#9; shadowPosition; &#9;&#9;vector_float3&#9; scatteredLight; &#9;&#9;vector_float3&#9; reflectedLight; &#9;&#9;vector_float3&#9; rgb; &#9;&#9;half4&#9;&#9;&#9;&#9;&#9; colorSample; &#9;&#9;bool&#9;&#9;&#9;&#9;&#9;&#9;isShadow; &#9;&#9; constexpr sampler shadowSampler(coord::normalized, &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; filter::linear, &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; address::clamp_to_edge, &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; compare_func:: less); &#9;&#9;shadowPosition = in.shadowSpacePosition.xyz; &#9;&#9;cmp = shadowTexture.sample_compare(shadowSampler, shadowPosition.xy , shadowPosition.z); &#9; if(cmp < 0.0001) &#9;&#9;&#9; isShadow = true; &#9;&#9;else &#9;&#9;&#9;&#9;isShadow = false; &#9;&#9;// Sample the texture to obtain a color &#9;&#9;colorSample = colorTexture.sample(textureSampler, in.textureCoordinate); // ********* // Apply Lighting Model // ******* &#9;&#9; return outColor;&#9;&#9; } // ***************************************************** // ******** Shaders: Make Shadow Map ************ // ******************************************************* typedef struct { &#9;&#9;float4 clipSpacePosition [[position]]; } ShadowData; // Vertex function vertex ShadowData shadowVertexShader ( uint&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; vertexID [[vertex_id]], constant HT_Vertex&#9;&#9; *vertices [[buffer(HT_Vertex_Index)]], constant HT_Uniform&#9;&#9;*param [[buffer(HT_Uniform_Index)]] ) { &#9;&#9;ShadowData out; &#9;&#9;vector_float4&#9;newPosition = float4( vertices[vertexID].position , 1 ); &#9;&#9;newPosition = param->nodeTransform[ vertices[vertexID].pickID ] * newPosition; &#9;&#9;newPosition = param->toLampPOV * newPosition; &#9;&#9;newPosition = param->shadowPerspective * newPosition; &#9;&#9;out.clipSpacePosition = newPosition; &#9;&#9;return out; }
1
0
1.2k
Nov ’20
Hit Test / Mouse Pick in Metal. Sample Code
I have been getting my feet wet in Metal programming. Most recently I have given myself the task of learning how to do hit testing in a MTKView. I looked around for sample code but what I found involved ray tracing which I didn't find attractive. I ended up implementing a technique I had used in OpenGL. Here one renders a scene off-screen using psuedo colors unique to each of the objects one wishes pick. One then reads the "color" at the click point to determine what was hit. I wrote a little proof of concept program and figured out how to do this in Metal. It works quite well I think. I have uploaded the project files to GitHub: github.com/MackenzieBD/Sample-Code Anyone interested in how I solved this problem may download the XCode project in the file HitTest.zip. My hope is to help other novice Metal programmers like me. Also, I would welcome any criticisms from more experienced coders. Have I done anything really asinine? What would be the advantages of a different approach?
3
1
1.7k
Nov ’20
CFBundleIdentifier Collision
I am trying to submit a new App to the Mac AppStore. In App Store Connect, I create a new and enter the app's bundle identifier in the proper field. Then when I use Xcode to submit the application I get the following error codes:ERROR ITMS-90511: "CFBundleIdentifier Collision. The Info.plist CFBundleIdentifier value '$(PRODUCT_BUNDLE_IDENTIFIER)' of 'Mad Octahedron.app/Contents/Resources/MadOctahedron' is already in use by another application."An unknown error occurred.ERROR ITMS-90277: "Invalid Bundle Identifier. The application bundle contains a tool or framework $(PRODUCT_NAME) [com.Ambraw.MadOctahedron.pkg/Payload/Mad Octahedron.app/Contents/Resources/MadOctahedron] using the bundle identifier '$(PRODUCT_BUNDLE_IDENTIFIER)', which is not a valid bundle identifier."An unknown error occurred.ERROR ITMS-90261: "Bad CFBundleExecutable. Cannot find executable file that matches the value of CFBundleExecutable in the nested bundle $(PRODUCT_NAME) [com.Ambraw.MadOctahedron.pkg/Payload/Mad Octahedron.app/Contents/Resources/MadOctahedron] property list file."An unknown error occurred.I've tried changing the identifier and resubmitting but get the same errors. Would anyone have any ideas?
4
0
14k
Jun ’18