~baltix/+junk/irrlicht-test

« back to all changes in this revision

Viewing changes to tests/textureFeatures.cpp

  • Committer: Mantas Kriaučiūnas
  • Date: 2011-07-18 13:06:25 UTC
  • Revision ID: mantas@akl.lt-20110718130625-c5pvifp61e7kj1ol
Included whole irrlicht SVN libraries to work around launchpad recipe issue with quilt, see https://answers.launchpad.net/launchpad/+question/165193

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (C) 2008-2011 Christian Stehno, Colin MacDonald
 
2
// No rights reserved: this software is in the public domain.
 
3
 
 
4
#include "testUtils.h"
 
5
 
 
6
using namespace irr;
 
7
using namespace core;
 
8
 
 
9
namespace
 
10
{
 
11
//! check miplevels by visual test
 
12
bool renderMipLevels(video::E_DRIVER_TYPE driverType)
 
13
{
 
14
        IrrlichtDevice *device = createDevice( driverType, dimension2d<u32>(160, 120), 32);
 
15
        if (!device)
 
16
                return true; // Treat a failure to create a driver as benign; this saves a lot of #ifdefs
 
17
 
 
18
        video::IVideoDriver* driver = device->getVideoDriver();
 
19
        scene::ISceneManager * smgr = device->getSceneManager();
 
20
        if (!driver->queryFeature(video::EVDF_MIP_MAP))
 
21
        {
 
22
                device->closeDevice();
 
23
                device->run();
 
24
                device->drop();
 
25
                return true;
 
26
        }
 
27
 
 
28
        logTestString("Testing driver %ls\n", driver->getName());
 
29
 
 
30
        scene::ISceneNode* n = smgr->addCubeSceneNode();
 
31
        scene::ISceneNode* n2 = smgr->addCubeSceneNode(10, 0, -1, core::vector3df(20,0,30), core::vector3df(0,45,0));
 
32
 
 
33
        // we use a main texture with blue on top and red below
 
34
        // and mipmap with pink on top and cyan below
 
35
        if (n && n2)
 
36
        {
 
37
                // create the texture and miplevels with distinct colors
 
38
                u32 texData[16*16];
 
39
                for (u32 i=0; i<16*16; ++i)
 
40
                        texData[i]=(i<8*16?0xff0000ff:0xffff0000);
 
41
                video::IImage* image = driver->createImageFromData(video::ECF_A8R8G8B8, core::dimension2du(16,16), texData, false);
 
42
                u32 mipdata[8*16];
 
43
                u32 index=0;
 
44
                for (u32 j=8; j>0; j/=2)
 
45
                {
 
46
                        for (u32 i=0; i<j; ++i)
 
47
                        {
 
48
                                u32 val=(i<j/2?0xffff00ff:0xff00ffff);
 
49
                                for (u32 k=0; k<j; ++k)
 
50
                                        mipdata[index++]=val;
 
51
                        }
 
52
                }
 
53
 
 
54
                video::ITexture* tex = driver->addTexture("miptest", image, mipdata);
 
55
                if (!tex)
 
56
                        // is probably an error in the mipdata handling
 
57
                        return false;
 
58
                else
 
59
                {
 
60
                        n->setMaterialFlag(video::EMF_LIGHTING, false);
 
61
                        n->setMaterialTexture(0, tex);
 
62
                        n2->setMaterialFlag(video::EMF_LIGHTING, false);
 
63
                        n2->setMaterialTexture(0, tex);
 
64
                }
 
65
                image->drop();
 
66
        }
 
67
 
 
68
        (void)smgr->addCameraSceneNode(0, core::vector3df(10,0,-30));
 
69
 
 
70
        driver->beginScene(true, true, video::SColor(255,100,101,140));
 
71
        smgr->drawAll();
 
72
        driver->endScene();
 
73
 
 
74
        bool result = takeScreenshotAndCompareAgainstReference(driver, "-renderMipmap.png");
 
75
 
 
76
        if (!result)
 
77
                logTestString("mipmap render failed.\n", driver->getName());
 
78
        else
 
79
                logTestString("Passed\n");
 
80
 
 
81
        device->closeDevice();
 
82
        device->run();
 
83
        device->drop();
 
84
 
 
85
        return result;
 
86
}
 
87
 
 
88
 
 
89
//! Tests locking miplevels
 
90
bool lockAllMipLevels(video::E_DRIVER_TYPE driverType)
 
91
{
 
92
        IrrlichtDevice *device = createDevice( driverType, dimension2d<u32>(160, 120), 32);
 
93
        if (!device)
 
94
                return true; // Treat a failure to create a driver as benign; this saves a lot of #ifdefs
 
95
 
 
96
        video::IVideoDriver* driver = device->getVideoDriver();
 
97
        scene::ISceneManager * smgr = device->getSceneManager();
 
98
 
 
99
        if (!driver->queryFeature(video::EVDF_MIP_MAP))
 
100
        {
 
101
                device->closeDevice();
 
102
                device->run();
 
103
                device->drop();
 
104
                return true;
 
105
        }
 
106
 
 
107
        logTestString("Testing driver %ls\n", driver->getName());
 
108
 
 
109
        scene::ISceneNode* n = smgr->addCubeSceneNode();
 
110
 
 
111
        if (n)
 
112
        {
 
113
                // create the texture and miplevels with distinct colors
 
114
                u32 texData[16*16];
 
115
                for (u32 i=0; i<16*16; ++i)
 
116
                        texData[i]=0xff0000ff-i;
 
117
                video::IImage* image = driver->createImageFromData(video::ECF_A8R8G8B8, core::dimension2du(16,16), texData, false);
 
118
                u32 mipdata[8*16];
 
119
                u32 index=0;
 
120
                for (u32 j=8; j>0; j/=2)
 
121
                {
 
122
                        u32 val=(j==8?0x00ff00ff:(j==4?0x0000ffff:(j==2?0xc2c200ff:0x001212ff)));
 
123
                        for (u32 i=0; i<j; ++i)
 
124
                        {
 
125
                                for (u32 k=0; k<j; ++k)
 
126
                                        mipdata[index++]=val-i;
 
127
                        }
 
128
                }
 
129
 
 
130
                video::ITexture* tex = driver->addTexture("miptest", image, mipdata);
 
131
                if (!tex)
 
132
                        // is probably an error in the mipdata handling
 
133
                        return false;
 
134
                else
 
135
                        n->setMaterialTexture(0, tex);
 
136
                image->drop();
 
137
        }
 
138
 
 
139
        (void)smgr->addCameraSceneNode();
 
140
 
 
141
        driver->beginScene(true, true, video::SColor(255,100,101,140));
 
142
        smgr->drawAll();
 
143
        driver->endScene();
 
144
 
 
145
        video::ITexture* tex = driver->findTexture("miptest");
 
146
        video::SColor* bits = (video::SColor*)tex->lock(video::ETLM_READ_ONLY, 0);
 
147
        bool result = (bits[0].color==0xff0000ff);
 
148
        tex->unlock();
 
149
        bits = (video::SColor*)tex->lock(video::ETLM_READ_ONLY, 1);
 
150
        result &= (bits[0].color==0x00ff00ff);
 
151
        tex->unlock();
 
152
        bits = (video::SColor*)tex->lock(video::ETLM_READ_ONLY, 2);
 
153
        result &= (bits[0].color==0x0000ffff);
 
154
        tex->unlock();
 
155
        bits = (video::SColor*)tex->lock(video::ETLM_READ_ONLY, 3);
 
156
        result &= (bits[0].color==0xc2c200ff);
 
157
        tex->unlock();
 
158
        bits = (video::SColor*)tex->lock(video::ETLM_READ_ONLY, 4);
 
159
        result &= (bits[0].color==0x001212ff);
 
160
        tex->unlock();
 
161
 
 
162
        if (!result)
 
163
                logTestString("mipmap lock after init with driver %ls failed.\n", driver->getName());
 
164
 
 
165
        // test with updating a lower level, and reading upper and lower
 
166
        bits = (video::SColor*)tex->lock(video::ETLM_READ_WRITE, 3);
 
167
        bits[0]=0xff00ff00;
 
168
        bits[1]=0xff00ff00;
 
169
        tex->unlock();
 
170
        bits = (video::SColor*)tex->lock(video::ETLM_READ_WRITE, 4);
 
171
        result &= (bits[0].color==0x001212ff);
 
172
        tex->unlock();
 
173
        bits = (video::SColor*)tex->lock(video::ETLM_READ_ONLY, 3);
 
174
        result &= ((bits[0].color==0xff00ff00)&&(bits[2].color==0xc2c200fe));
 
175
        tex->unlock();
 
176
 
 
177
        if (!result)
 
178
                logTestString("mipmap lock after mipmap write with driver %ls failed.\n", driver->getName());
 
179
 
 
180
        // now test locking level 0
 
181
        bits = (video::SColor*)tex->lock(video::ETLM_READ_WRITE, 0);
 
182
        bits[0]=0xff00ff00;
 
183
        bits[1]=0xff00ff00;
 
184
        tex->unlock();
 
185
        bits = (video::SColor*)tex->lock(video::ETLM_READ_WRITE, 4);
 
186
        result &= (bits[0].color==0x001212ff);
 
187
        tex->unlock();
 
188
        bits = (video::SColor*)tex->lock(video::ETLM_READ_ONLY, 0);
 
189
        result &= ((bits[0].color==0xff00ff00)&&(bits[2].color==0xff0000fd));
 
190
        tex->unlock();
 
191
 
 
192
        if (!result)
 
193
                logTestString("mipmap lock at level 0 after mipmap write with driver %ls failed.\n", driver->getName());
 
194
        else
 
195
                logTestString("Passed\n");
 
196
 
 
197
        device->closeDevice();
 
198
        device->run();
 
199
        device->drop();
 
200
 
 
201
        return result;
 
202
}
 
203
 
 
204
 
 
205
//! Tests locking miplevels after texture was created with auto mipmap update
 
206
bool lockWithAutoMipmap(video::E_DRIVER_TYPE driverType)
 
207
{
 
208
        IrrlichtDevice *device = createDevice( driverType, dimension2d<u32>(160, 120), 32);
 
209
        if (!device)
 
210
                return true; // Treat a failure to create a driver as benign; this saves a lot of #ifdefs
 
211
 
 
212
        video::IVideoDriver* driver = device->getVideoDriver();
 
213
        scene::ISceneManager * smgr = device->getSceneManager();
 
214
 
 
215
        if (!driver->queryFeature(video::EVDF_MIP_MAP))
 
216
        {
 
217
                device->closeDevice();
 
218
                device->run();
 
219
                device->drop();
 
220
                return true;
 
221
        }
 
222
 
 
223
        logTestString("Testing driver %ls\n", driver->getName());
 
224
 
 
225
        scene::ISceneNode* n = smgr->addCubeSceneNode();
 
226
 
 
227
        if (n)
 
228
        {
 
229
                // create the texture
 
230
                u32 texData[16*16];
 
231
                for (u32 i=0; i<16*16; ++i)
 
232
                        texData[i]=0xff0000ff-i;
 
233
                video::IImage* image = driver->createImageFromData(video::ECF_A8R8G8B8, core::dimension2du(16,16), texData, false);
 
234
 
 
235
                video::ITexture* tex = driver->addTexture("miptest", image);
 
236
                if (!tex)
 
237
                        return false;
 
238
                else
 
239
                        n->setMaterialTexture(0, tex);
 
240
                image->drop();
 
241
        }
 
242
        (void)smgr->addCameraSceneNode();
 
243
 
 
244
        driver->beginScene(true, true, video::SColor(255,100,101,140));
 
245
        smgr->drawAll();
 
246
        driver->endScene();
 
247
 
 
248
        video::ITexture* tex = driver->findTexture("miptest");
 
249
        video::SColor* bits = (video::SColor*)tex->lock(video::ETLM_READ_ONLY, 0);
 
250
        bool result = (bits[0].color==0xff0000ff);
 
251
        tex->unlock();
 
252
        if (!result)
 
253
                logTestString("mipmap lock after init with driver %ls failed.\n", driver->getName());
 
254
 
 
255
        // test with updating a lower level, and reading upper and lower
 
256
        bits = (video::SColor*)tex->lock(video::ETLM_READ_WRITE, 3);
 
257
        bits[0]=0xff00ff00;
 
258
        bits[1]=0xff00ff00;
 
259
        tex->unlock();
 
260
        // lock another texture just to invalidate caches in the driver
 
261
        bits = (video::SColor*)tex->lock(video::ETLM_READ_WRITE, 4);
 
262
        tex->unlock();
 
263
        bits = (video::SColor*)tex->lock(video::ETLM_READ_ONLY, 3);
 
264
        result &= ((bits[0].color==0xff00ff00)&&(bits[2].color!=0xff00ff00));
 
265
        tex->unlock();
 
266
 
 
267
        if (!result)
 
268
                logTestString("mipmap lock after mipmap write with driver %ls failed.\n", driver->getName());
 
269
 
 
270
        // now test locking level 0
 
271
        bits = (video::SColor*)tex->lock(video::ETLM_READ_WRITE, 0);
 
272
        bits[0]=0x00ff00ff;
 
273
        bits[1]=0x00ff00ff;
 
274
        tex->unlock();
 
275
        bits = (video::SColor*)tex->lock(video::ETLM_READ_ONLY, 3);
 
276
        result &= ((bits[0].color==0xff00ff00)&&(bits[2].color!=0xff00ff00));
 
277
        tex->unlock();
 
278
 
 
279
        if (!result)
 
280
                logTestString("mipmap lock at level 0 after mipmap write with driver %ls failed.\n", driver->getName());
 
281
        else
 
282
                logTestString("Passed\n");
 
283
 
 
284
        device->closeDevice();
 
285
        device->run();
 
286
        device->drop();
 
287
 
 
288
        return result;
 
289
}
 
290
}
 
291
 
 
292
 
 
293
bool textureFeatures(void)
 
294
{
 
295
        bool result = true;
 
296
 
 
297
        TestWithAllDrivers(renderMipLevels);
 
298
        TestWithAllDrivers(lockAllMipLevels);
 
299
        TestWithAllDrivers(lockWithAutoMipmap);
 
300
 
 
301
        return result;
 
302
}