1#include "Engine/Data/Texture.hpp"
3#include <catch2/catch_test_macros.hpp>
5#include <Core/CoreMacros.hpp>
6#include <Engine/Data/BlinnPhongMaterial.hpp>
7#include <Engine/Data/LambertianMaterial.hpp>
8#include <Engine/Data/PlainMaterial.hpp>
9#include <Engine/Data/VolumetricMaterial.hpp>
10#include <Engine/OpenGL.hpp>
11#include <Engine/RadiumEngine.hpp>
13#include <Headless/CLIViewer.hpp>
14#include <glbinding/gl/enum.h>
15#include <globjects/Texture.h>
17#ifdef HEADLESS_HAS_EGL
18# include <Headless/OpenGLContext/EglOpenGLContext.hpp>
20# include <Headless/OpenGLContext/GlfwOpenGLContext.hpp>
23using namespace Ra::Headless;
25using namespace Ra::Core::Utils;
27using namespace gl45core;
29TEST_CASE(
"Engine/Data/Texture",
"[unittests][Engine][Engine/Data][Textures]" ) {
31 glbinding::Version glVersion { 4, 4 };
32#ifdef HEADLESS_HAS_EGL
33 CLIViewer viewer { std::make_unique<EglOpenGLContext>( glVersion ) };
35 CLIViewer viewer { std::make_unique<GlfwOpenGLContext>( glVersion ) };
37 auto dummy_name =
"unittest_textures";
38 auto code = viewer.init( 1, &dummy_name );
41 SECTION(
"Texture Init Now" ) {
43 viewer.bindOpenGLContext(
true );
50 texture1.initializeNow();
51 texture2.initializeNow();
53 auto gpuTexture1 = texture1.getGpuTexture();
54 auto gpuTexture2 = texture2.getGpuTexture();
56 REQUIRE( gpuTexture1 !=
nullptr );
57 REQUIRE( gpuTexture2 !=
nullptr );
59 id1 = gpuTexture1->id();
60 id2 = gpuTexture2->id();
62 REQUIRE( id1 != id2 );
64 REQUIRE( glIsTexture( id1 ) );
65 REQUIRE( glIsTexture( id2 ) );
67 texture1.destroyNow();
69 REQUIRE( !glIsTexture( id1 ) );
70 REQUIRE( texture1.getGpuTexture() ==
nullptr );
71 REQUIRE( glIsTexture( id2 ) );
72 REQUIRE( texture2.getGpuTexture() == gpuTexture2 );
75 viewer.bindOpenGLContext(
true );
78 REQUIRE( !glIsTexture( id1 ) );
79 REQUIRE( !glIsTexture( id2 ) );
82 SECTION(
"Texture Init Delayed" ) {
84 viewer.bindOpenGLContext(
true );
91 texture1.initialize();
92 texture2.initialize();
94 auto gpuTexture1 = texture1.getGpuTexture();
95 auto gpuTexture2 = texture2.getGpuTexture();
97 REQUIRE( gpuTexture1 ==
nullptr );
98 REQUIRE( gpuTexture2 ==
nullptr );
102 gpuTexture1 = texture1.getGpuTexture();
103 gpuTexture2 = texture2.getGpuTexture();
105 REQUIRE( gpuTexture1 !=
nullptr );
106 REQUIRE( gpuTexture2 !=
nullptr );
108 id1 = gpuTexture1->id();
109 id2 = gpuTexture2->id();
111 REQUIRE( id1 != id2 );
113 REQUIRE( glIsTexture( id1 ) );
114 REQUIRE( glIsTexture( id2 ) );
119 REQUIRE( glIsTexture( id1 ) );
121 REQUIRE( texture1.getGpuTexture() ==
nullptr );
125 REQUIRE( !glIsTexture( id1 ) );
126 REQUIRE( texture1.getGpuTexture() ==
nullptr );
128 REQUIRE( glIsTexture( id2 ) );
129 REQUIRE( texture2.getGpuTexture() == gpuTexture2 );
131 texture2.setParameters( { {}, {} } );
134 viewer.bindOpenGLContext(
true );
137 REQUIRE( !glIsTexture( id1 ) );
138 REQUIRE( !glIsTexture( id2 ) );
141 SECTION(
"Texture Update and Resize" ) {
142 REQUIRE( code == 0 );
158 { data2[0], data2[1], data2[2], data2[3], data2[4], data2[5] } };
163 params1.image.texels = data1;
164 params2.image.target = GL_TEXTURE_CUBE_MAP;
165 params2.image.texels = data2void;
170 texture1.initialize();
171 texture2.initialize();
172 viewer.bindOpenGLContext(
true );
176 REQUIRE( texture1.getTexels() !=
nullptr );
177 REQUIRE(
static_cast<const uchar*
>( texture1.getTexels() )[0] == 0 );
178 REQUIRE(
static_cast<const uchar*
>( texture1.getTexels() )[1] == 1 );
179 REQUIRE(
static_cast<const uchar*
>( texture1.getTexels() )[2] == 2 );
181 auto gpuTexture1 = texture1.getGpuTexture();
182 REQUIRE( gpuTexture1 !=
nullptr );
183 auto id1 = gpuTexture1->id();
185 REQUIRE( glIsTexture( id1 ) );
187 auto readData1 = gpuTexture1->getImage( 0, params1.image.format, params1.image.type );
189 REQUIRE( readData1[0] == data1[0] );
190 REQUIRE( readData1[1] == data1[1] );
191 REQUIRE( readData1[2] == data1[2] );
197 texture1.updateData( data1 );
199 readData1 = gpuTexture1->getImage( 0, params1.image.format, params1.image.type );
201 REQUIRE( readData1[0] == 0 );
202 REQUIRE( readData1[1] == 1 );
203 REQUIRE( readData1[2] == 2 );
207 readData1 = gpuTexture1->getImage( 0, params1.image.format, params1.image.type );
209 REQUIRE( readData1[0] == data1[0] );
210 REQUIRE( readData1[1] == data1[1] );
211 REQUIRE( readData1[2] == data1[2] );
215 for ( uchar i = 0; i < 3 * 4; ++i )
217 texture1.resize( 2, 2, 1, data1Resized );
218 readData1 = gpuTexture1->getImage( 0, params1.image.format, params1.image.type );
222 glGetIntegerv( GL_PACK_ALIGNMENT, &a );
226 int k = s >= a ? n * l : a / s * (int)
std::ceil(
float( s * n * l ) / a );
228 for (
size_t i = 0; i < 2; ++i ) {
229 for (
size_t j = 0; j < l; ++j ) {
230 for (
size_t c = 0; c < n; ++c ) {
231 size_t readIndex = i * k + j * n + c;
232 size_t dataIndex = i * ( l * n ) + j * n + c;
233 REQUIRE( readData1[readIndex] == data1Resized[dataIndex] );
Represent a Texture of the engine.
(GPU) Data representation, along with manager
Describes the sampler and image of a texture.