Overte C++ Documentation
TextureProcessing.h
1 //
2 // TextureProcessing.h
3 // image/src/TextureProcessing
4 //
5 // Created by Clement Brisset on 4/5/2017.
6 // Copyright 2017 High Fidelity, Inc.
7 // Copyright 2021 Vircadia contributors.
8 //
9 // Distributed under the Apache License, Version 2.0.
10 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
11 //
12 
13 #ifndef hifi_image_TextureProcessing_h
14 #define hifi_image_TextureProcessing_h
15 
16 #include <QVariant>
17 
18 #include <gpu/Texture.h>
19 
20 #include "Image.h"
21 #include <nvtt/nvtt.h>
22 
23 namespace image {
24 
25  std::function<gpu::uint32(const glm::vec3&)> getHDRPackingFunction();
26  std::function<glm::vec3(gpu::uint32)> getHDRUnpackingFunction();
27  void convertToFloatFromPacked(const unsigned char* source, int width, int height, size_t srcLineByteStride, gpu::Element sourceFormat,
28  glm::vec4* output, size_t outputLinePixelStride);
29  void convertToPackedFromFloat(unsigned char* output, int width, int height, size_t outputLineByteStride, gpu::Element outputFormat,
30  const glm::vec4* source, size_t srcLinePixelStride);
31 
32 namespace TextureUsage {
33 
34 /*@jsdoc
35  * <p>Describes the type of texture.</p>
36  * <p>See also: {@link Material} and
37  * {@link https://docs.overte.org/create/3d-models/pbr-materials-guide.html|PBR Materials Guide}.</p>
38  * <table>
39  * <thead>
40  * <tr><th>Value</th><th>Name</th><th>Description</th></tr>
41  * </thead>
42  * <tbody>
43  * <tr><td><code>0</code></td><td>Default</td><td>Basic color.</td></tr>
44  * <tr><td><code>1</code></td><td>Strict</td><td>Basic color. Quality never downgraded.</td></tr>
45  * <tr><td><code>2</code></td><td>Albedo</td><td>Color for PBR.</td></tr>
46  * <tr><td><code>3</code></td><td>Normal</td><td>Normal map.</td></tr>
47  * <tr><td><code>4</code></td><td>Bump</td><td>Bump map.</td></tr>
48  * <tr><td><code>5</code></td><td>Specular or metallic</td><td>Metallic or not.</td></tr>
49  * <tr><td><code>6</code></td><td>Roughness</td><td>Rough or matte.</td></tr>
50  * <tr><td><code>7</code></td><td>Gloss</td><td>Gloss or shine.</td></tr>
51  * <tr><td><code>8</code></td><td>Emissive</td><td>The amount of light reflected.</td></tr>
52  * <tr><td><code>9</code></td><td>Cube</td><td>Cubic image for sky boxes.</td></tr>
53  * <tr><td><code>10</code></td><td>Occlusion or scattering</td><td>How objects or human skin interact with light.</td></tr>
54  * <tr><td><code>11</code></td><td>Lightmap</td><td>Light map.</td></tr>
55  * <tr><td><code>12</code></td><td>Unused</td><td>Texture is not currently used.</td></tr>
56  * </tbody>
57  * </table>
58  * @typedef {number} TextureCache.TextureType
59  */
60 enum Type {
61  // NOTE: add new texture types at the bottom here
62  DEFAULT_TEXTURE,
63  STRICT_TEXTURE,
64  ALBEDO_TEXTURE,
65  NORMAL_TEXTURE,
66  BUMP_TEXTURE,
67  SPECULAR_TEXTURE,
68  METALLIC_TEXTURE = SPECULAR_TEXTURE, // for now spec and metallic texture are the same, converted to grey
69  ROUGHNESS_TEXTURE,
70  GLOSS_TEXTURE,
71  EMISSIVE_TEXTURE,
72  SKY_TEXTURE,
73  AMBIENT_TEXTURE,
74  OCCLUSION_TEXTURE,
75  SCATTERING_TEXTURE = OCCLUSION_TEXTURE,
76  LIGHTMAP_TEXTURE,
77  UNUSED_TEXTURE
78 };
79 
80 using TextureLoader = std::function<gpu::TexturePointer(Image&&, const std::string&, bool, gpu::BackendTarget, const std::atomic<bool>&)>;
81 TextureLoader getTextureLoaderForType(Type type);
82 
83 gpu::TexturePointer create2DTextureFromImage(Image&& image, const std::string& srcImageName,
84  bool compress, gpu::BackendTarget target, const std::atomic<bool>& abortProcessing);
85 gpu::TexturePointer createStrict2DTextureFromImage(Image&& image, const std::string& srcImageName,
86  bool compress, gpu::BackendTarget target, const std::atomic<bool>& abortProcessing);
87 gpu::TexturePointer createAlbedoTextureFromImage(Image&& image, const std::string& srcImageName,
88  bool compress, gpu::BackendTarget target, const std::atomic<bool>& abortProcessing);
89 gpu::TexturePointer createEmissiveTextureFromImage(Image&& image, const std::string& srcImageName,
90  bool compress, gpu::BackendTarget target, const std::atomic<bool>& abortProcessing);
91 gpu::TexturePointer createNormalTextureFromNormalImage(Image&& image, const std::string& srcImageName,
92  bool compress, gpu::BackendTarget target, const std::atomic<bool>& abortProcessing);
93 gpu::TexturePointer createNormalTextureFromBumpImage(Image&& image, const std::string& srcImageName,
94  bool compress, gpu::BackendTarget target, const std::atomic<bool>& abortProcessing);
95 gpu::TexturePointer createRoughnessTextureFromImage(Image&& image, const std::string& srcImageName,
96  bool compress, gpu::BackendTarget target, const std::atomic<bool>& abortProcessing);
97 gpu::TexturePointer createRoughnessTextureFromGlossImage(Image&& image, const std::string& srcImageName,
98  bool compress, gpu::BackendTarget target, const std::atomic<bool>& abortProcessing);
99 gpu::TexturePointer createMetallicTextureFromImage(Image&& image, const std::string& srcImageName,
100  bool compress, gpu::BackendTarget target, const std::atomic<bool>& abortProcessing);
101 gpu::TexturePointer createCubeTextureFromImage(Image&& image, const std::string& srcImageName,
102  bool compress, gpu::BackendTarget target, const std::atomic<bool>& abortProcessing);
103 gpu::TexturePointer createAmbientCubeTextureAndIrradianceFromImage(Image&& image, const std::string& srcImageName,
104  bool compress, gpu::BackendTarget target, const std::atomic<bool>& abortProcessing);
105 gpu::TexturePointer createLightmapTextureFromImage(Image&& image, const std::string& srcImageName,
106  bool compress, gpu::BackendTarget target, const std::atomic<bool>& abortProcessing);
107 gpu::TexturePointer process2DTextureColorFromImage(Image&& srcImage, const std::string& srcImageName, bool compress,
108  gpu::BackendTarget target, bool isStrict, const std::atomic<bool>& abortProcessing);
109 gpu::TexturePointer process2DHDRTextureColorFromImage(Image&& srcImage, const std::string& srcImageName, bool compress,
110  gpu::BackendTarget target, bool isStrict, const std::atomic<bool>& abortProcessing);
111 gpu::TexturePointer process2DTextureNormalMapFromImage(Image&& srcImage, const std::string& srcImageName, bool compress,
112  gpu::BackendTarget target, bool isBumpMap, const std::atomic<bool>& abortProcessing);
113 gpu::TexturePointer process2DTextureGrayscaleFromImage(Image&& srcImage, const std::string& srcImageName, bool compress,
114  gpu::BackendTarget target, bool isInvertedPixels, const std::atomic<bool>& abortProcessing);
115 
116 enum CubeTextureOptions {
117  CUBE_DEFAULT = 0x0,
118  CUBE_GENERATE_IRRADIANCE = 0x1,
119  CUBE_GGX_CONVOLVE = 0x2
120 };
121 gpu::TexturePointer processCubeTextureColorFromImage(Image&& srcImage, const std::string& srcImageName, bool compress,
122  gpu::BackendTarget target, int option, const std::atomic<bool>& abortProcessing);
123 } // namespace TextureUsage
124 
125 const QStringList getSupportedFormats();
126 
127 std::pair<gpu::TexturePointer, glm::ivec2> processImage(std::shared_ptr<QIODevice> content, const std::string& url, ColorChannel sourceChannel,
128  int maxNumPixels, TextureUsage::Type textureType,
129  bool compress, gpu::BackendTarget target, const std::atomic<bool>& abortProcessing = false);
130 
131 void convertToTextureWithMips(gpu::Texture* texture, Image&& image, gpu::BackendTarget target, const std::atomic<bool>& abortProcessing = false, int face = -1);
132 void convertToTexture(gpu::Texture* texture, Image&& image, gpu::BackendTarget target, const std::atomic<bool>& abortProcessing = false, int face = -1, int mipLevel = 0);
133 Image convertToHDRFormat(Image&& srcImage, gpu::Element format);
134 Image convertToLDRFormat(Image&& srcImage, Image::Format format);
135 } // namespace image
136 
137 #endif // hifi_image_TextureProcessing_h