How to create a solid color texture in Metal?

199 views Asked by At

I’m trying to use the basic texture loader to create a single-color texture, but it’s failing. This is as minimal as the example can get. What might be wrong?

Why might this might be failing?

            NSError* texture_loader_error = nil;
            // default metalkit texture loader
            _texture_loader = [[MTKTextureLoader alloc] initWithDevice:_device];
            
            NSDictionary* texture_loader_options = @{MTKTextureLoaderOptionGenerateMipmaps: @(NO), MTKTextureLoaderOptionSRGB: @(YES)};
            
            usize texture_index = 0;
            
            // load default textures
            {
                simd_float4 color_white = simd_make_float4(1.0, 1.0, 1.0, 1.0);
                NSData* color_data_white = [[NSData alloc] initWithBytes:&color_white length:sizeof(color_white)];
                
                id<MTLTexture> texture = [_texture_loader newTextureWithData:color_data_white options: texture_loader_options error:&texture_loader_error];
                if (!texture) {
                    NSLog(@"Failed to create texture, error %@", error);
                } else {
                    texture_index += 1;
                }
                
            }

Is it that the textureLoader expects the data to be in a common format like png rather than just bytes?

0

There are 0 answers