1.) You access a mapped buffer pointer after it has been unmapped!
That is illegal and could just crash the app with an access violation.
=> Line 19 belongs to line 23.
2.) Texture buffers cannot be RT_BUFFER_INPUT_OUTPUT. They must be RT_BUFFER_INPUT.
Then I would not recommend to use those OptiX SDK example framework functions for your case.
The loadTexture() creates a complete texture sampler and texture buffer from scratch.
You neither use that, nor does your the code clean up any of the unused resource.
How I would implement this is like this:
Create a single buffer to hold the texture data. Maybe start off with a 1x1 white RGBA8 buffer.
Then create a single texture sampler and assign that one buffer to it. Rendering with that should result in a white texture.
If that works continue like this:
Implement your own function which loads some image from disk.
The loadTexture() code should contain that inside.
Don’t put the image data into OptiX buffers at that point, just keep the image data in an abstract host image format.
For the texture you want to put into that single texture buffer, load the desired image, then resize the texture buffer to the matching size, map the buffer, copy the image data over from the host, unmap the texture buffer.
Basically take PPMLoader::loadTexture() apart and split it into these two steps (texture sampler creation, texture buffer filling).
Since the texture sampler is not changed and just the buffer data has been updated, that’s all!
The next launch should grab the new data for that texture sampler.
Generally I recommend to get used to the native OptiX API instead of trying to reuse code from the SDK examples which are often using convenience functions for exactly one specific purpose.
If you have another purpose you’re better off to implement a more dedicated function for that which can be simpler and more efficient.
The code inside the PPMLoader::loadTexture() function shows that correctly:
optix::Buffer buffer = context->createBuffer( RT_BUFFER_INPUT, RT_FORMAT_UNSIGNED_BYTE4, 1u, 1u );