GlistEngine
Loading...
Searching...
No Matches
gVKTexture.h
Go to the documentation of this file.
1/*
2 * gVKTexture.h
3 *
4 * A GPU texture for the Vulkan backend: a sampled image plus the view, sampler
5 * and descriptor set the image pipeline binds. gVKRenderEngine keeps a registry
6 * mapping the GLuint ids gTexture/gImage hand out to these objects, so the
7 * existing load path (createTextures -> texImage2D) allocates a real Vulkan
8 * texture without gTexture knowing anything about Vulkan.
9 */
10
11#pragma once
12
13#ifndef CORE_GVKTEXTURE_H
14#define CORE_GVKTEXTURE_H
15
16#include "gVKContext.h"
17
18#ifdef GVK_VULKAN
19
20#include <vector>
21
22struct gVKTexture {
23 VkImage image = VK_NULL_HANDLE;
24 VkDeviceMemory memory = VK_NULL_HANDLE;
25 VkImageView view = VK_NULL_HANDLE;
26 // One view per mip level, for a render target whose levels are drawn into
27 // individually - a bloom pyramid does exactly that. A framebuffer attachment
28 // has to name a single level, which `view` (spanning the whole chain, so it can
29 // be sampled with textureLod) cannot do. Empty for a single-level texture.
30 std::vector<VkImageView> levelviews;
31 VkSampler sampler = VK_NULL_HANDLE;
32 uint64_t samplerkey = 0;
33 VkDescriptorSet descriptorset = VK_NULL_HANDLE;
34 int width = 0;
35 int height = 0;
36 // Levels in the mip chain, 1 meaning no chain. Built at upload time, the same
37 // way the OpenGL path calls glGenerateMipmap after its upload.
38 uint32_t miplevels = 1;
39 // Tracked so a render target can be moved between being written as an
40 // attachment and being sampled. An uploaded texture never leaves
41 // SHADER_READ_ONLY, which is why that is the default.
42 VkImageLayout layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
43 VkImageAspectFlags aspect = VK_IMAGE_ASPECT_COLOR_BIT;
44 VkFormat format = VK_FORMAT_R8G8B8A8_UNORM;
45 // True for a texture created as an FBO attachment rather than uploaded from
46 // pixels. Those carry no mip chain and are cleared by the render pass.
47 bool isattachment = false;
48 // Whether any texel in the upload is transparent enough for color_frag.glsl's cutout
49 // test to discard it. Found while the pixels are being expanded to RGBA anyway,
50 // so it costs nothing, and it lets a mesh drawn with this as its diffuse map take
51 // the pipeline that has the discard compiled out - which is what keeps early
52 // depth rejection alive on mobile. Left false for an attachment, which is never a
53 // material map.
54 bool hascutout = false;
55 // Set after the descriptor has been recorded by a draw. Replacing a texture
56 // that has never been sampled needs no device-wide wait (for example the
57 // duplicate upload performed while gTexture initially sets itself up).
58 bool sampled = false;
59 // What the current sampler was built with, so a filtering or wrapping change
60 // can be detected and only then rebuild it. The defaults match what gTexture
61 // starts from.
62 VkFilter minfilter = VK_FILTER_LINEAR;
63 VkFilter magfilter = VK_FILTER_LINEAR;
64 // Whether the sampler may walk the mip chain at all. OpenGL says so through the
65 // minification filter: GL_LINEAR samples level 0 only, GL_LINEAR_MIPMAP_LINEAR
66 // is trilinear. Collapsing both onto "always mip" softened textures the
67 // application had asked to be sampled flat.
68 bool usemipmaps = true;
69 VkSamplerMipmapMode mipmapmode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
70 VkSamplerAddressMode addressu = VK_SAMPLER_ADDRESS_MODE_REPEAT;
71 VkSamplerAddressMode addressv = VK_SAMPLER_ADDRESS_MODE_REPEAT;
72};
73
74// Uploads tightly-packed RGBA8 pixels (4 bytes per pixel, top row first) into a
75// new device-local sampled image and allocates its descriptor set from the
76// context's pool. Returns nullptr on failure.
77gVKTexture* gvkCreateTextureRGBA8(gVKContext& ctx, const void* rgbaPixels, int width, int height);
78
79// Allocates the texture's descriptor set from the context's pool and points it at
80// the image. Called once at creation, and again after a shader reload, which
81// destroys the pool and with it every set allocated from it.
82bool gvkWriteTextureDescriptorSet(gVKContext& ctx, gVKTexture* tex);
83
84// Rebuilds the texture's sampler when the requested filtering or wrapping differs
85// from what it already has, and repoints the descriptor set at the new one. This is
86// how gTexture::setFiltering / setWrapping reach Vulkan. Returns false when nothing
87// had to change.
88bool gvkSetTextureSampler(gVKContext& ctx, gVKTexture* tex, VkFilter minFilter, VkFilter magFilter,
89 VkSamplerAddressMode addressU, VkSamplerAddressMode addressV,
90 bool useMipmaps = true, VkSamplerMipmapMode mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR);
91
92// Creates an empty texture to be rendered into, for gFbo's colour and depth
93// attachments. Unlike an uploaded texture this carries no pixels and no mip chain:
94// the render pass clears it, the draws fill it, and it is transitioned back to
95// SHADER_READ_ONLY afterwards so the same object can be sampled.
96// mipLevels above 1 allocates the whole chain and gives every level a view of its
97// own, so each can be rendered into while the texture as a whole stays sampleable.
98// transferDst additionally makes the image a legal destination for a copy, which is
99// what the screen restore texture needs - it is filled by copying the resolved
100// swapchain image into it rather than by being drawn into.
101gVKTexture* gvkCreateAttachmentTexture(gVKContext& ctx, int width, int height, bool depth,
102 int mipLevels = 1, bool transferDst = false);
103
104void gvkDestroyTexture(gVKContext& ctx, gVKTexture* tex);
105
106#endif /* GVK_VULKAN */
107
108#endif /* CORE_GVKTEXTURE_H */