Path: blob/main_old/src/tests/gl_tests/D3D11EmulatedIndexedBufferTest.cpp
1693 views
//1// Copyright 2015 The ANGLE Project Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file.4//5// D3D11EmulatedIndexedBufferTest:6// Tests to validate our D3D11 support for emulating an indexed7// vertex buffer.8//910#include "libANGLE/Context.h"11#include "libANGLE/angletypes.h"12#include "libANGLE/renderer/d3d/IndexDataManager.h"13#include "libANGLE/renderer/d3d/d3d11/Buffer11.h"14#include "libANGLE/renderer/d3d/d3d11/Context11.h"15#include "libANGLE/renderer/d3d/d3d11/Renderer11.h"16#include "test_utils/ANGLETest.h"17#include "test_utils/angle_test_instantiate.h"18#include "util/EGLWindow.h"1920using namespace angle;2122namespace23{2425class D3D11EmulatedIndexedBufferTest : public ANGLETest26{27protected:28void testSetUp() override29{30ASSERT_EQ(EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE, GetParam().getRenderer());3132mContext = static_cast<gl::Context *>(getEGLWindow()->getContext());33rx::Context11 *context11 = rx::GetImplAs<rx::Context11>(mContext);34mRenderer = context11->getRenderer();3536mSourceBuffer = new rx::Buffer11(mBufferState, mRenderer);37GLfloat testData[] = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f};38angle::Result error = mSourceBuffer->setData(nullptr, gl::BufferBinding::Array, testData,39sizeof(testData), gl::BufferUsage::StaticDraw);40ASSERT_EQ(angle::Result::Continue, error);4142mTranslatedAttribute.baseOffset = 0;43mTranslatedAttribute.usesFirstVertexOffset = false;44mTranslatedAttribute.stride = sizeof(GLfloat);4546GLubyte indices[] = {0, 0, 3, 4, 2, 1, 1};4748for (size_t i = 0; i < ArraySize(indices); i++)49{50mExpectedExpandedData.push_back(testData[indices[i]]);51mubyteIndices.push_back(indices[i]);52muintIndices.push_back(indices[i]);53mushortIndices.push_back(indices[i]);54}55}5657void testTearDown() override { SafeDelete(mSourceBuffer); }5859void createMappableCompareBufferFromEmulatedBuffer(ID3D11Buffer *sourceBuffer,60GLuint size,61ID3D11Buffer **mappableBuffer)62{63*mappableBuffer = nullptr;6465D3D11_BUFFER_DESC bufferDesc;66bufferDesc.ByteWidth = size;67bufferDesc.MiscFlags = 0;68bufferDesc.StructureByteStride = 0;69bufferDesc.Usage = D3D11_USAGE_STAGING;70bufferDesc.BindFlags = 0;71bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;7273HRESULT hr = mRenderer->getDevice()->CreateBuffer(&bufferDesc, nullptr, mappableBuffer);74ASSERT_TRUE(SUCCEEDED(hr));7576D3D11_BOX srcBox;77srcBox.left = 0;78srcBox.right = size;79srcBox.top = 0;80srcBox.bottom = 1;81srcBox.front = 0;82srcBox.back = 1;8384mRenderer->getDeviceContext()->CopySubresourceRegion(*mappableBuffer, 0, 0, 0, 0,85sourceBuffer, 0, &srcBox);86}8788void compareContents(ID3D11Buffer *actual)89{90ID3D11Buffer *compareBuffer = nullptr;91createMappableCompareBufferFromEmulatedBuffer(92actual, sizeof(GLfloat) * static_cast<GLuint>(mExpectedExpandedData.size()),93&compareBuffer);9495D3D11_MAPPED_SUBRESOURCE mappedResource;96HRESULT hr = mRenderer->getDeviceContext()->Map(compareBuffer, 0, D3D11_MAP_READ, 0,97&mappedResource);98ASSERT_TRUE(SUCCEEDED(hr));99100GLfloat *compareData = static_cast<GLfloat *>(mappedResource.pData);101for (size_t i = 0; i < mExpectedExpandedData.size(); i++)102{103EXPECT_EQ(mExpectedExpandedData[i], compareData[i]);104}105106mRenderer->getDeviceContext()->Unmap(compareBuffer, 0);107SafeRelease(compareBuffer);108}109110void emulateAndCompare(rx::SourceIndexData *srcData)111{112ID3D11Buffer *emulatedBuffer = nullptr;113angle::Result error = mSourceBuffer->getEmulatedIndexedBuffer(114mContext, srcData, mTranslatedAttribute, 0, &emulatedBuffer);115ASSERT_EQ(angle::Result::Continue, error);116ASSERT_TRUE(emulatedBuffer != nullptr);117compareContents(emulatedBuffer);118}119120protected:121gl::Context *mContext;122rx::Buffer11 *mSourceBuffer;123rx::Renderer11 *mRenderer;124rx::TranslatedAttribute mTranslatedAttribute;125std::vector<GLfloat> mExpectedExpandedData;126std::vector<GLubyte> mubyteIndices;127std::vector<GLuint> muintIndices;128std::vector<GLushort> mushortIndices;129gl::BufferState mBufferState;130};131132// This tests that a GL_UNSIGNED_BYTE indices list can be successfully expanded133// into a valid emulated indexed buffer.134TEST_P(D3D11EmulatedIndexedBufferTest, TestNativeToExpandedUsingGLubyteIndices)135{136rx::SourceIndexData srcData = {nullptr, mubyteIndices.data(),137static_cast<unsigned int>(mubyteIndices.size()),138gl::DrawElementsType::UnsignedByte, false};139emulateAndCompare(&srcData);140}141142// This tests that a GL_UNSIGNED_SHORT indices list can be successfully expanded143// into a valid emulated indexed buffer.144TEST_P(D3D11EmulatedIndexedBufferTest, TestNativeToExpandedUsingGLushortIndices)145{146rx::SourceIndexData srcData = {nullptr, mushortIndices.data(),147static_cast<unsigned int>(mushortIndices.size()),148gl::DrawElementsType::UnsignedShort, false};149emulateAndCompare(&srcData);150}151152// This tests that a GL_UNSIGNED_INT indices list can be successfully expanded153// into a valid emulated indexed buffer.154TEST_P(D3D11EmulatedIndexedBufferTest, TestNativeToExpandedUsingGLuintIndices)155{156rx::SourceIndexData srcData = {nullptr, muintIndices.data(),157static_cast<unsigned int>(muintIndices.size()),158gl::DrawElementsType::UnsignedInt, false};159emulateAndCompare(&srcData);160}161162// This tests verifies that a Buffer11 contents remain unchanged after calling163// getEmulatedIndexedBuffer164TEST_P(D3D11EmulatedIndexedBufferTest, TestSourceBufferRemainsUntouchedAfterExpandOperation)165{166// Copy the original source buffer before any expand calls have been made167gl::BufferState cleanSourceState;168rx::Buffer11 *cleanSourceBuffer = new rx::Buffer11(cleanSourceState, mRenderer);169ASSERT_EQ(angle::Result::Continue, cleanSourceBuffer->copySubData(nullptr, mSourceBuffer, 0, 0,170mSourceBuffer->getSize()));171172// Do a basic exanded and compare test.173rx::SourceIndexData srcData = {nullptr, muintIndices.data(),174static_cast<unsigned int>(muintIndices.size()),175gl::DrawElementsType::UnsignedInt, false};176emulateAndCompare(&srcData);177178const uint8_t *sourceBufferMem = nullptr;179const uint8_t *cleanBufferMem = nullptr;180181ASSERT_EQ(angle::Result::Continue, mSourceBuffer->getData(mContext, &sourceBufferMem));182ASSERT_EQ(angle::Result::Continue, cleanSourceBuffer->getData(mContext, &cleanBufferMem));183184ASSERT_EQ(0, memcmp(sourceBufferMem, cleanBufferMem, cleanSourceBuffer->getSize()));185186SafeDelete(cleanSourceBuffer);187}188189ANGLE_INSTANTIATE_TEST(D3D11EmulatedIndexedBufferTest, ES2_D3D11(), ES3_D3D11(), ES31_D3D11());190191} // anonymous namespace192193194