1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program Tester Core
3 * ----------------------------------------
5 * Copyright 2014 The Android Open Source Project
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 * \brief Test Log C++ Wrapper.
22 *//*--------------------------------------------------------------------*/
24 #include "tcuTestLog.hpp"
25 #include "tcuTextureUtil.hpp"
26 #include "tcuSurface.hpp"
34 class LogWriteFailedError : public ResourceError
37 LogWriteFailedError (void) : ResourceError("Writing to test log failed") {}
42 MAX_IMAGE_SIZE_2D = 4096,
43 MAX_IMAGE_SIZE_3D = 128
48 LogImage::LogImage (const std::string& name, const std::string& description, const Surface& surface, qpImageCompressionMode compression)
50 , m_description (description)
51 , m_access (surface.getAccess())
52 , m_scale (1.0f, 1.0f, 1.0f, 1.0f)
53 , m_bias (0.0f, 0.0f, 0.0f, 0.0f)
54 , m_compression (compression)
58 LogImage::LogImage (const std::string& name, const std::string& description, const ConstPixelBufferAccess& access, qpImageCompressionMode compression)
60 , m_description (description)
62 , m_scale (1.0f, 1.0f, 1.0f, 1.0f)
63 , m_bias (0.0f, 0.0f, 0.0f, 0.0f)
64 , m_compression (compression)
66 // Simplify combined formats that only use a single channel
67 if (tcu::isCombinedDepthStencilType(m_access.getFormat().type))
69 if (m_access.getFormat().order == tcu::TextureFormat::D)
70 m_access = tcu::getEffectiveDepthStencilAccess(m_access, tcu::Sampler::MODE_DEPTH);
71 else if (m_access.getFormat().order == tcu::TextureFormat::S)
72 m_access = tcu::getEffectiveDepthStencilAccess(m_access, tcu::Sampler::MODE_STENCIL);
75 // Implicit scale and bias
76 if (m_access.getFormat().order != tcu::TextureFormat::DS)
77 computePixelScaleBias(m_access, m_scale, m_bias);
80 // Pack D and S bias and scale to R and G
81 const ConstPixelBufferAccess depthAccess = tcu::getEffectiveDepthStencilAccess(m_access, tcu::Sampler::MODE_DEPTH);
82 const ConstPixelBufferAccess stencilAccess = tcu::getEffectiveDepthStencilAccess(m_access, tcu::Sampler::MODE_STENCIL);
85 tcu::Vec4 stencilScale;
86 tcu::Vec4 stencilBias;
88 computePixelScaleBias(depthAccess, depthScale, depthBias);
89 computePixelScaleBias(stencilAccess, stencilScale, stencilBias);
91 m_scale = tcu::Vec4(depthScale.x(), stencilScale.x(), 0.0f, 0.0f);
92 m_bias = tcu::Vec4(depthBias.x(), stencilBias.x(), 0.0f, 0.0f);
96 LogImage::LogImage (const std::string& name, const std::string& description, const ConstPixelBufferAccess& access, const Vec4& scale, const Vec4& bias, qpImageCompressionMode compression)
98 , m_description (description)
102 , m_compression (compression)
104 // Cannot set scale and bias of combined formats
105 DE_ASSERT(access.getFormat().order != tcu::TextureFormat::DS);
108 if (tcu::isCombinedDepthStencilType(access.getFormat().type))
110 if (access.getFormat().order == tcu::TextureFormat::D)
111 m_access = tcu::getEffectiveDepthStencilAccess(access, tcu::Sampler::MODE_DEPTH);
112 if (access.getFormat().order == tcu::TextureFormat::S)
113 m_access = tcu::getEffectiveDepthStencilAccess(access, tcu::Sampler::MODE_STENCIL);
116 // Cannot log a DS format
123 void LogImage::write (TestLog& log) const
125 if (m_access.getFormat().order != tcu::TextureFormat::DS)
126 log.writeImage(m_name.c_str(), m_description.c_str(), m_access, m_scale, m_bias, m_compression);
129 const ConstPixelBufferAccess depthAccess = tcu::getEffectiveDepthStencilAccess(m_access, tcu::Sampler::MODE_DEPTH);
130 const ConstPixelBufferAccess stencilAccess = tcu::getEffectiveDepthStencilAccess(m_access, tcu::Sampler::MODE_STENCIL);
132 log.startImageSet(m_name.c_str(), m_description.c_str());
133 log.writeImage("Depth", "Depth channel", depthAccess, m_scale.swizzle(0, 0, 0, 0), m_bias.swizzle(0, 0, 0, 0), m_compression);
134 log.writeImage("Stencil", "Stencil channel", stencilAccess, m_scale.swizzle(1, 1, 1, 1), m_bias.swizzle(1, 1, 1, 1), m_compression);
141 MessageBuilder::MessageBuilder (const MessageBuilder& other)
144 m_str.str(other.m_str.str());
147 MessageBuilder& MessageBuilder::operator= (const MessageBuilder& other)
150 m_str.str(other.m_str.str());
154 TestLog& MessageBuilder::operator<< (const TestLog::EndMessageToken&)
156 m_log->writeMessage(m_str.str().c_str());
162 TestLog& SampleBuilder::operator<< (const TestLog::EndSampleToken&)
164 m_log->startSample();
166 for (std::vector<Value>::const_iterator val = m_values.begin(); val != m_values.end(); ++val)
168 if (val->type == Value::TYPE_FLOAT64)
169 m_log->writeSampleValue(val->value.float64);
170 else if (val->type == Value::TYPE_INT64)
171 m_log->writeSampleValue(val->value.int64);
183 TestLog::TestLog (const char* fileName, deUint32 flags)
184 : m_log(qpTestLog_createFileLog(fileName, flags))
187 throw ResourceError(std::string("Failed to open test log file '") + fileName + "'");
190 TestLog::~TestLog (void)
192 qpTestLog_destroy(m_log);
195 void TestLog::writeMessage (const char* msgStr)
197 if (qpTestLog_writeText(m_log, DE_NULL, DE_NULL, QP_KEY_TAG_LAST, msgStr) == DE_FALSE)
198 throw LogWriteFailedError();
201 void TestLog::startImageSet (const char* name, const char* description)
203 if (qpTestLog_startImageSet(m_log, name, description) == DE_FALSE)
204 throw LogWriteFailedError();
207 void TestLog::endImageSet (void)
209 if (qpTestLog_endImageSet(m_log) == DE_FALSE)
210 throw LogWriteFailedError();
214 static Vector<int, Size> computeScaledSize (const Vector<int, Size>& imageSize, int maxSize)
216 bool allInRange = true;
217 for (int i = 0; i < Size; i++)
218 allInRange = allInRange && (imageSize[i] <= maxSize);
225 for (int i = 0; i < Size; i++)
226 d = de::max(d, (float)imageSize[i] / (float)maxSize);
228 Vector<int, Size> res;
229 for (int i = 0; i < Size; i++)
230 res[i] = de::max(1, deRoundFloatToInt32((float)imageSize[i] / d));
236 void TestLog::writeImage (const char* name, const char* description, const ConstPixelBufferAccess& access, const Vec4& pixelScale, const Vec4& pixelBias, qpImageCompressionMode compressionMode)
238 const TextureFormat& format = access.getFormat();
239 int width = access.getWidth();
240 int height = access.getHeight();
241 int depth = access.getDepth();
243 // Writing a combined image does not make sense
244 DE_ASSERT(!tcu::isCombinedDepthStencilType(access.getFormat().type));
246 // Do not bother with preprocessing if images are not stored
247 if ((qpTestLog_getLogFlags(m_log) & QP_TEST_LOG_EXCLUDE_IMAGES) != 0)
250 if (depth == 1 && format.type == TextureFormat::UNORM_INT8
251 && width <= MAX_IMAGE_SIZE_2D && height <= MAX_IMAGE_SIZE_2D
252 && (format.order == TextureFormat::RGB || format.order == TextureFormat::RGBA)
253 && access.getPixelPitch() == access.getFormat().getPixelSize()
254 && pixelBias[0] == 0.0f && pixelBias[1] == 0.0f && pixelBias[2] == 0.0f && pixelBias[3] == 0.0f
255 && pixelScale[0] == 1.0f && pixelScale[1] == 1.0f && pixelScale[2] == 1.0f && pixelScale[3] == 1.0f)
258 bool isRGBA = format.order == TextureFormat::RGBA;
260 writeImage(name, description, compressionMode,
261 isRGBA ? QP_IMAGE_FORMAT_RGBA8888 : QP_IMAGE_FORMAT_RGB888,
262 width, height, access.getRowPitch(), access.getDataPtr());
266 Sampler sampler (Sampler::CLAMP_TO_EDGE, Sampler::CLAMP_TO_EDGE, Sampler::CLAMP_TO_EDGE, Sampler::LINEAR, Sampler::NEAREST);
267 IVec2 logImageSize = computeScaledSize(IVec2(width, height), MAX_IMAGE_SIZE_2D);
268 tcu::TextureLevel logImage (TextureFormat(TextureFormat::RGBA, TextureFormat::UNORM_INT8), logImageSize.x(), logImageSize.y(), 1);
269 PixelBufferAccess logImageAccess = logImage.getAccess();
270 std::ostringstream longDesc;
272 longDesc << description << " (p' = p * " << pixelScale << " + " << pixelBias << ")";
274 for (int y = 0; y < logImage.getHeight(); y++)
276 for (int x = 0; x < logImage.getWidth(); x++)
278 float yf = ((float)y + 0.5f) / (float)logImage.getHeight();
279 float xf = ((float)x + 0.5f) / (float)logImage.getWidth();
280 Vec4 s = access.sample2D(sampler, sampler.minFilter, xf, yf, 0)*pixelScale + pixelBias;
282 logImageAccess.setPixel(s, x, y);
286 writeImage(name, longDesc.str().c_str(), compressionMode, QP_IMAGE_FORMAT_RGBA8888,
287 logImageAccess.getWidth(), logImageAccess.getHeight(), logImageAccess.getRowPitch(),
288 logImageAccess.getDataPtr());
292 // Isometric splat volume rendering.
293 const float blendFactor = 0.85f;
294 IVec3 scaledSize = computeScaledSize(IVec3(width, height, depth), MAX_IMAGE_SIZE_3D);
295 int w = scaledSize.x();
296 int h = scaledSize.y();
297 int d = scaledSize.z();
298 int logImageW = w+d - 1;
299 int logImageH = w+d+h;
300 std::vector<float> blendImage (logImageW*logImageH*4, 0.0f);
301 PixelBufferAccess blendImageAccess (TextureFormat(TextureFormat::RGBA, TextureFormat::FLOAT), logImageW, logImageH, 1, &blendImage[0]);
302 tcu::TextureLevel logImage (TextureFormat(TextureFormat::RGBA, TextureFormat::UNORM_INT8), logImageW, logImageH, 1);
303 PixelBufferAccess logImageAccess = logImage.getAccess();
304 Sampler sampler (Sampler::CLAMP_TO_EDGE, Sampler::CLAMP_TO_EDGE, Sampler::CLAMP_TO_EDGE, Sampler::NEAREST, Sampler::NEAREST);
305 std::ostringstream longDesc;
307 // \note Back-to-front.
308 for (int z = d-1; z >= 0; z--)
310 for (int y = 0; y < h; y++)
312 for (int x = 0; x < w; x++)
314 int px = w - (x + 1) + z;
315 int py = (w + d + h) - (x + y + z + 1);
317 float xf = ((float)x + 0.5f) / (float)w;
318 float yf = ((float)y + 0.5f) / (float)h;
319 float zf = ((float)z + 0.5f) / (float)d;
321 Vec4 p = blendImageAccess.getPixel(px, py);
322 Vec4 s = access.sample3D(sampler, sampler.minFilter, xf, yf, zf);
323 Vec4 b = s + p*blendFactor;
325 blendImageAccess.setPixel(b, px, py);
330 // Scale blend image nicely.
331 longDesc << description << " (p' = p * " << pixelScale << " + " << pixelBias << ")";
333 // Write to final image.
334 tcu::clear(logImageAccess, tcu::IVec4(0x33, 0x66, 0x99, 0xff));
336 for (int z = 0; z < d; z++)
338 for (int y = 0; y < h; y++)
340 for (int x = 0; x < w; x++)
342 if (z != 0 && !(x == 0 || y == h-1 || y == h-2))
345 int px = w - (x + 1) + z;
346 int py = (w + d + h) - (x + y + z + 1);
347 Vec4 s = blendImageAccess.getPixel(px, py)*pixelScale + pixelBias;
349 logImageAccess.setPixel(s, px, py);
354 writeImage(name, longDesc.str().c_str(), compressionMode, QP_IMAGE_FORMAT_RGBA8888,
355 logImageAccess.getWidth(), logImageAccess.getHeight(), logImageAccess.getRowPitch(),
356 logImageAccess.getDataPtr());
360 void TestLog::writeImage (const char* name, const char* description, qpImageCompressionMode compressionMode, qpImageFormat format, int width, int height, int stride, const void* data)
362 if (qpTestLog_writeImage(m_log, name, description, compressionMode, format, width, height, stride, data) == DE_FALSE)
363 throw LogWriteFailedError();
366 void TestLog::startSection (const char* name, const char* description)
368 if (qpTestLog_startSection(m_log, name, description) == DE_FALSE)
369 throw LogWriteFailedError();
372 void TestLog::endSection (void)
374 if (qpTestLog_endSection(m_log) == DE_FALSE)
375 throw LogWriteFailedError();
378 void TestLog::startShaderProgram (bool linkOk, const char* linkInfoLog)
380 if (qpTestLog_startShaderProgram(m_log, linkOk?DE_TRUE:DE_FALSE, linkInfoLog) == DE_FALSE)
381 throw LogWriteFailedError();
384 void TestLog::endShaderProgram (void)
386 if (qpTestLog_endShaderProgram(m_log) == DE_FALSE)
387 throw LogWriteFailedError();
390 void TestLog::writeShader (qpShaderType type, const char* source, bool compileOk, const char* infoLog)
392 if (qpTestLog_writeShader(m_log, type, source, compileOk?DE_TRUE:DE_FALSE, infoLog) == DE_FALSE)
393 throw LogWriteFailedError();
396 void TestLog::writeSpirVAssemblySource (const char* source)
398 if (qpTestLog_writeSpirVAssemblySource(m_log, source) == DE_FALSE)
399 throw LogWriteFailedError();
402 void TestLog::writeKernelSource (const char* source)
404 if (qpTestLog_writeKernelSource(m_log, source) == DE_FALSE)
405 throw LogWriteFailedError();
408 void TestLog::writeCompileInfo (const char* name, const char* description, bool compileOk, const char* infoLog)
410 if (qpTestLog_writeCompileInfo(m_log, name, description, compileOk ? DE_TRUE : DE_FALSE, infoLog) == DE_FALSE)
411 throw LogWriteFailedError();
414 void TestLog::writeFloat (const char* name, const char* description, const char* unit, qpKeyValueTag tag, float value)
416 if (qpTestLog_writeFloat(m_log, name, description, unit, tag, value) == DE_FALSE)
417 throw LogWriteFailedError();
420 void TestLog::writeInteger (const char* name, const char* description, const char* unit, qpKeyValueTag tag, deInt64 value)
422 if (qpTestLog_writeInteger(m_log, name, description, unit, tag, value) == DE_FALSE)
423 throw LogWriteFailedError();
426 void TestLog::startEglConfigSet (const char* name, const char* description)
428 if (qpTestLog_startEglConfigSet(m_log, name, description) == DE_FALSE)
429 throw LogWriteFailedError();
432 void TestLog::writeEglConfig (const qpEglConfigInfo* config)
434 if (qpTestLog_writeEglConfig(m_log, config) == DE_FALSE)
435 throw LogWriteFailedError();
438 void TestLog::endEglConfigSet (void)
440 if (qpTestLog_endEglConfigSet(m_log) == DE_FALSE)
441 throw LogWriteFailedError();
444 void TestLog::startCase (const char* testCasePath, qpTestCaseType testCaseType)
446 if (qpTestLog_startCase(m_log, testCasePath, testCaseType) == DE_FALSE)
447 throw LogWriteFailedError();
450 void TestLog::endCase (qpTestResult result, const char* description)
452 if (qpTestLog_endCase(m_log, result, description) == DE_FALSE)
453 throw LogWriteFailedError();
456 void TestLog::terminateCase (qpTestResult result)
458 if (qpTestLog_terminateCase(m_log, result) == DE_FALSE)
459 throw LogWriteFailedError();
462 void TestLog::startSampleList (const std::string& name, const std::string& description)
464 if (qpTestLog_startSampleList(m_log, name.c_str(), description.c_str()) == DE_FALSE)
465 throw LogWriteFailedError();
468 void TestLog::startSampleInfo (void)
470 if (qpTestLog_startSampleInfo(m_log) == DE_FALSE)
471 throw LogWriteFailedError();
474 void TestLog::writeValueInfo (const std::string& name, const std::string& description, const std::string& unit, qpSampleValueTag tag)
476 if (qpTestLog_writeValueInfo(m_log, name.c_str(), description.c_str(), unit.empty() ? DE_NULL : unit.c_str(), tag) == DE_FALSE)
477 throw LogWriteFailedError();
480 void TestLog::endSampleInfo (void)
482 if (qpTestLog_endSampleInfo(m_log) == DE_FALSE)
483 throw LogWriteFailedError();
486 void TestLog::startSample (void)
488 if (qpTestLog_startSample(m_log) == DE_FALSE)
489 throw LogWriteFailedError();
492 void TestLog::writeSampleValue (double value)
494 if (qpTestLog_writeValueFloat(m_log, value) == DE_FALSE)
495 throw LogWriteFailedError();
498 void TestLog::writeSampleValue (deInt64 value)
500 if (qpTestLog_writeValueInteger(m_log, value) == DE_FALSE)
501 throw LogWriteFailedError();
504 void TestLog::endSample (void)
506 if (qpTestLog_endSample(m_log) == DE_FALSE)
507 throw LogWriteFailedError();
510 void TestLog::endSampleList (void)
512 if (qpTestLog_endSampleList(m_log) == DE_FALSE)
513 throw LogWriteFailedError();
516 const TestLog::BeginMessageToken TestLog::Message = TestLog::BeginMessageToken();
517 const TestLog::EndMessageToken TestLog::EndMessage = TestLog::EndMessageToken();
518 const TestLog::EndImageSetToken TestLog::EndImageSet = TestLog::EndImageSetToken();
519 const TestLog::EndSectionToken TestLog::EndSection = TestLog::EndSectionToken();
520 const TestLog::EndShaderProgramToken TestLog::EndShaderProgram = TestLog::EndShaderProgramToken();
521 const TestLog::SampleInfoToken TestLog::SampleInfo = TestLog::SampleInfoToken();
522 const TestLog::EndSampleInfoToken TestLog::EndSampleInfo = TestLog::EndSampleInfoToken();
523 const TestLog::BeginSampleToken TestLog::Sample = TestLog::BeginSampleToken();
524 const TestLog::EndSampleToken TestLog::EndSample = TestLog::EndSampleToken();
525 const TestLog::EndSampleListToken TestLog::EndSampleList = TestLog::EndSampleListToken();