diff --git a/runtime/helpers/hw_helper.h b/runtime/helpers/hw_helper.h index 22f28a5c36..ac9bd81806 100644 --- a/runtime/helpers/hw_helper.h +++ b/runtime/helpers/hw_helper.h @@ -10,6 +10,7 @@ #include "runtime/command_stream/linear_stream.h" #include "runtime/gen_common/aub_mapper.h" #include "runtime/gen_common/hw_cmds.h" +#include "runtime/mem_obj/buffer.h" #include "CL/cl.h" @@ -42,6 +43,7 @@ class HwHelper { virtual const AubMemDump::LrcaHelper &getCsTraits(aub_stream::EngineType engineType) const = 0; virtual bool supportsYTiling() const = 0; virtual bool obtainRenderBufferCompressionPreference(const HardwareInfo &hwInfo) const = 0; + virtual void checkResourceCompatibility(Buffer *buffer, cl_int &errorCode) = 0; static bool renderCompressedBuffersSupported(const HardwareInfo &hwInfo); static bool renderCompressedImagesSupported(const HardwareInfo &hwInfo); static bool cacheFlushAfterWalkerSupported(const HardwareInfo &hwInfo); @@ -127,6 +129,8 @@ class HwHelperHw : public HwHelper { bool obtainRenderBufferCompressionPreference(const HardwareInfo &hwInfo) const override; + void checkResourceCompatibility(Buffer *buffer, cl_int &errorCode) override; + bool timestampPacketWriteSupported() const override; bool isPageTableManagerSupported(const HardwareInfo &hwInfo) const override; diff --git a/runtime/helpers/hw_helper_common.inl b/runtime/helpers/hw_helper_common.inl index 94cacd2942..da0f9b3201 100644 --- a/runtime/helpers/hw_helper_common.inl +++ b/runtime/helpers/hw_helper_common.inl @@ -97,6 +97,10 @@ bool HwHelperHw::timestampPacketWriteSupported() const { return false; } +template +inline void HwHelperHw::checkResourceCompatibility(Buffer *buffer, cl_int &errorCode) { +} + template void HwHelperHw::setRenderSurfaceStateForBuffer(ExecutionEnvironment &executionEnvironment, void *surfaceStateBuffer, diff --git a/runtime/mem_obj/image.cpp b/runtime/mem_obj/image.cpp index 6e6c7a5fd8..42cfa64297 100644 --- a/runtime/mem_obj/image.cpp +++ b/runtime/mem_obj/image.cpp @@ -17,6 +17,7 @@ #include "runtime/helpers/aligned_memory.h" #include "runtime/helpers/basic_math.h" #include "runtime/helpers/get_info.h" +#include "runtime/helpers/hw_helper.h" #include "runtime/helpers/hw_info.h" #include "runtime/helpers/mipmap.h" #include "runtime/helpers/ptr_math.h" @@ -186,6 +187,13 @@ Image *Image::create(Context *context, bool zeroCopy = false; bool transferNeeded = false; if (((imageDesc->image_type == CL_MEM_OBJECT_IMAGE1D_BUFFER) || (imageDesc->image_type == CL_MEM_OBJECT_IMAGE2D)) && (parentBuffer != nullptr)) { + + HwHelper::get(context->getDevice(0)->getHardwareInfo().pPlatform->eRenderCoreFamily).checkResourceCompatibility(parentBuffer, errcodeRet); + + if (errcodeRet != CL_SUCCESS) { + return nullptr; + } + memory = parentBuffer->getGraphicsAllocation(); // Image from buffer - we never allocate memory, we use what buffer provides zeroCopy = true; diff --git a/unit_tests/helpers/CMakeLists.txt b/unit_tests/helpers/CMakeLists.txt index 76a3ce83fd..10c9e8084d 100644 --- a/unit_tests/helpers/CMakeLists.txt +++ b/unit_tests/helpers/CMakeLists.txt @@ -41,6 +41,7 @@ set(IGDRCL_SRCS_tests_helpers ${CMAKE_CURRENT_SOURCE_DIR}/mipmap_tests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/per_thread_data_tests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/ptr_math_tests.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/raii_hw_helper.h ${CMAKE_CURRENT_SOURCE_DIR}/queue_helpers_tests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/sampler_helpers_tests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/string_to_hash_tests.cpp diff --git a/unit_tests/helpers/raii_hw_helper.h b/unit_tests/helpers/raii_hw_helper.h new file mode 100644 index 0000000000..4048fbac60 --- /dev/null +++ b/unit_tests/helpers/raii_hw_helper.h @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2019 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#pragma once + +#include "runtime/helpers/hw_helper.h" + +namespace NEO { +extern HwHelper *hwHelperFactory[IGFX_MAX_CORE]; + +template +class RAIIHwHelperFactory { + public: + GFXCORE_FAMILY gfxCoreFamily; + HwHelper *hwHelper; + MockHelper mockHwHelper; + + RAIIHwHelperFactory(GFXCORE_FAMILY gfxCoreFamily) { + this->gfxCoreFamily = gfxCoreFamily; + hwHelper = hwHelperFactory[this->gfxCoreFamily]; + hwHelperFactory[this->gfxCoreFamily] = &mockHwHelper; + } + + ~RAIIHwHelperFactory() { + hwHelperFactory[this->gfxCoreFamily] = hwHelper; + } +}; +} // namespace NEO \ No newline at end of file diff --git a/unit_tests/mem_obj/buffer_tests.cpp b/unit_tests/mem_obj/buffer_tests.cpp index 8927b0ec1e..ac937282d0 100644 --- a/unit_tests/mem_obj/buffer_tests.cpp +++ b/unit_tests/mem_obj/buffer_tests.cpp @@ -511,7 +511,7 @@ TEST_F(RenderCompressedBuffersTests, givenBufferNotCompressedAllocationAndNoHost EXPECT_EQ(buffer->getGraphicsAllocation()->getAllocationType(), GraphicsAllocation::AllocationType::BUFFER_COMPRESSED); } else { EXPECT_TRUE(buffer->isMemObjZeroCopy()); - EXPECT_EQ(buffer->getGraphicsAllocation()->getAllocationType(), GraphicsAllocation::AllocationType::BUFFER); + EXPECT_EQ(buffer->getGraphicsAllocation()->getAllocationType(), GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY); } } } diff --git a/unit_tests/mem_obj/image2d_from_buffer_tests.cpp b/unit_tests/mem_obj/image2d_from_buffer_tests.cpp index ef94cb55a2..ce5ef894ca 100644 --- a/unit_tests/mem_obj/image2d_from_buffer_tests.cpp +++ b/unit_tests/mem_obj/image2d_from_buffer_tests.cpp @@ -6,15 +6,21 @@ */ #include "runtime/helpers/aligned_memory.h" +#include "runtime/helpers/hw_helper.h" #include "runtime/mem_obj/buffer.h" #include "runtime/mem_obj/image.h" #include "test.h" #include "unit_tests/fixtures/device_fixture.h" +#include "unit_tests/helpers/raii_hw_helper.h" #include "unit_tests/mocks/mock_context.h" #include "unit_tests/mocks/mock_gmm.h" using namespace NEO; +namespace NEO { +extern HwHelper *hwHelperFactory[IGFX_MAX_CORE]; +} + // Tests for cl_khr_image2d_from_buffer class Image2dFromBufferTest : public DeviceFixture, public ::testing::Test { public: @@ -391,3 +397,28 @@ TEST_F(Image2dFromBufferTest, givenBufferWhenImageFromBufferThenIsImageFromBuffe buffer->release(); imageDesc.mem_object = memObj; } + +HWTEST_F(Image2dFromBufferTest, givenBufferWhenImageFromBufferThenIsImageFromBufferSetAndAllocationTypeIsBufferNullptr) { + class MockHwHelperHw : public HwHelperHw { + public: + void checkResourceCompatibility(Buffer *buffer, cl_int &errorCode) override { + errorCode = CL_INVALID_MEM_OBJECT; + } + }; + + auto raiiFactory = RAIIHwHelperFactory(context.getDevice(0)->getHardwareInfo().pPlatform->eRenderCoreFamily); + + cl_int errCode = CL_SUCCESS; + auto buffer = Buffer::create(&context, 0, 1, nullptr, errCode); + imageDesc.image_type = CL_MEM_OBJECT_IMAGE2D; + auto memObj = imageDesc.mem_object; + imageDesc.mem_object = buffer; + + Image *imageFromBuffer = createImage(); + EXPECT_EQ(CL_INVALID_MEM_OBJECT, retVal); + + EXPECT_EQ(imageFromBuffer, nullptr); + + buffer->release(); + imageDesc.mem_object = memObj; +}