|
openwallpaper
|
Textures are images stored in GPU memory.
To create a texture from pixel data, call ow_create_texture and fill the texture data with ow_update_texture in a copy pass:
The most common scenario is to load texture from an image stored in a scene and archive. To do so, there is a shortcut function ow_create_texture_from_image. As this function does update under the hood, it can be called only in a copy pass.
It supports PNG and WEBP image formats. Image loaded from a file is converted to a pixel format specified in ow_texture_info format field. width and height fields are filled automatically.
You can use ow_update_texture to update only specific sub-rectangle of a texture, see ow_texture_update_destination.
After the texture is created, you can bind it to a draw call for use in shaders. To do so, you will need a sampler that tells the GPU how to read pixels from a texture. To create sampler, use ow_create_sampler:
Here we specify that we want to use linear filtering and clamp texture by X and Y when trying to sample texture outside of its bounds.
After we have sampler, we can bind texture to draw call. To do so, specify both texture and sampler in ow_bindings_info that is passed to ow_render_geometry:
First we need to declare a texture with uniform sampler2D. Then we can use texture function to sample texture at given coordinates. In texture coordinate system, up left corner has (0, 0) coordinates, and down right corner has (1, 1) coordinates. Here is an example of a fragment shader that uses a texture:
What to specify in layout(set = ...)? By convention:
You may see a complete texture usage example in image example.
By default, render pass renders to the screen, but you can also set it up to use texture as a target. To do this, specify target texture ID in ow_render_pass_info color_target field.
All the draw calls in this pass must also specify format of target texture in their pipeline objects. To do so, specify ow_pipeline_info color_target_format field:
By default, this field is set to OW_TEXTURE_SWAPCHAIN, which means the screen texture format. You may also OW_TEXTURE_SWAPCHAIN as a format in ow_create_texture to create textures of the same format as screen texture.
You can't render to a texture and bind the same texture in uniform sampler2D in the same draw call. If you want to apply some shader effects to the texture one after another, a typical pattern is ping-pong: create two temporary textures and render from first to second, then from second to first, etc.
TODO
TODO