Skip to content

Commit 46a0c87

Browse files
test: add sampler with 1D image level zero black box test
Related-To: NEO-12742 Signed-off-by: Kamil Kopryk <[email protected]>
1 parent 3be52fa commit 46a0c87

File tree

1 file changed

+134
-1
lines changed

1 file changed

+134
-1
lines changed

level_zero/core/test/black_box_tests/zello_bindless_kernel.cpp

Lines changed: 134 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,21 @@ kernel void image_query_3d(global int *dst, image3d_t img) {
179179
}
180180
)===";
181181

182+
const char *source1DSampler = R"===(
183+
__kernel void image_read_sampler(__global float4 *dst, image1d_t img, sampler_t sampler) {
184+
int id = get_global_id(0);
185+
float coord = (float)(id+1);
186+
dst[id] = read_imagef(img, sampler, coord);
187+
printf( "gid[%zu], coord=%.2f, dst.x=%.2f , dst.y=%.2f , dst.z=%.2f , dst.w=%.2f \n", get_global_id(0), coord, dst[id].x, dst[id].y, dst[id].z, dst[id].w);
188+
}
189+
)===";
190+
182191
static std::string kernelName = "kernel_copy";
183192
static std::string kernelName2 = "kernel_fill";
184193
static std::string kernelName3 = "image_copy";
185194
static std::string kernelName4 = "image_read_sampler";
186195
static std::string kernelName4a = "image_read_sampler_oob";
196+
static std::string kernelName1DSampler = "image_read_sampler";
187197

188198
enum class ExecutionMode : uint32_t {
189199
commandQueue,
@@ -871,6 +881,120 @@ bool testBindlessImageSampledBorderColor(ze_context_handle_t context, ze_device_
871881
return outputValidated;
872882
}
873883

884+
bool testBindlessImage1DSampled(ze_context_handle_t context, ze_device_handle_t device, const std::string &deviceId,
885+
const std::string &revisionId, AddressingMode mode) {
886+
bool outputValidated = true;
887+
888+
ze_module_handle_t module = nullptr;
889+
ze_kernel_handle_t kernel = nullptr;
890+
891+
createModule(source1DSampler, mode, context, device, deviceId, revisionId, module, "", false);
892+
createKernel(module, kernel, kernelName1DSampler.c_str());
893+
894+
LevelZeroBlackBoxTests::CommandHandler commandHandler;
895+
bool isImmediateCmdList = false;
896+
897+
SUCCESS_OR_TERMINATE(commandHandler.create(context, device, isImmediateCmdList));
898+
899+
ze_host_mem_alloc_desc_t hostDesc = {ZE_STRUCTURE_TYPE_HOST_MEM_ALLOC_DESC};
900+
hostDesc.flags = ZE_HOST_MEM_ALLOC_FLAG_BIAS_UNCACHED;
901+
902+
bool normalized = false;
903+
ze_sampler_desc_t samplerDesc = {ZE_STRUCTURE_TYPE_SAMPLER_DESC,
904+
nullptr,
905+
ZE_SAMPLER_ADDRESS_MODE_CLAMP,
906+
ZE_SAMPLER_FILTER_MODE_NEAREST,
907+
normalized};
908+
ze_sampler_handle_t sampler;
909+
SUCCESS_OR_TERMINATE(zeSamplerCreate(context, device, &samplerDesc, &sampler));
910+
911+
ze_image_desc_t srcImgDesc = {ZE_STRUCTURE_TYPE_IMAGE_DESC,
912+
nullptr,
913+
0,
914+
ZE_IMAGE_TYPE_1D,
915+
{ZE_IMAGE_FORMAT_LAYOUT_32_32_32_32, ZE_IMAGE_FORMAT_TYPE_FLOAT,
916+
ZE_IMAGE_FORMAT_SWIZZLE_R, ZE_IMAGE_FORMAT_SWIZZLE_G,
917+
ZE_IMAGE_FORMAT_SWIZZLE_B, ZE_IMAGE_FORMAT_SWIZZLE_A},
918+
8,
919+
1,
920+
1,
921+
0,
922+
0};
923+
constexpr auto nChannels = 4u;
924+
constexpr auto bytesPerChannel = sizeof(float);
925+
constexpr auto bytesPerPixel = bytesPerChannel * nChannels;
926+
uint32_t xDim = static_cast<uint32_t>(srcImgDesc.width);
927+
uint32_t yDim = static_cast<uint32_t>(srcImgDesc.height);
928+
uint32_t zDim = static_cast<uint32_t>(srcImgDesc.depth);
929+
uint32_t nPixels = xDim * yDim * zDim;
930+
size_t allocSize = nPixels * bytesPerPixel;
931+
932+
// Create and initialize host memory
933+
void *dstBuffer;
934+
SUCCESS_OR_TERMINATE(zeMemAllocHost(context, &hostDesc, allocSize, 1, &dstBuffer));
935+
936+
float *dst = reinterpret_cast<float *>(dstBuffer);
937+
for (auto iPixel = 0u; iPixel < srcImgDesc.width; ++iPixel) {
938+
for (auto channel = 0u; channel < 4; ++channel) {
939+
dst[iPixel * bytesPerChannel + channel] = static_cast<float>(iPixel * 10);
940+
}
941+
}
942+
943+
ze_image_handle_t srcImg;
944+
ze_group_count_t dispatchTraits;
945+
dispatchTraits.groupCountX = 1u;
946+
dispatchTraits.groupCountY = 1u;
947+
dispatchTraits.groupCountZ = 1u;
948+
949+
SUCCESS_OR_TERMINATE(zeImageCreate(context, device, &srcImgDesc, &srcImg));
950+
951+
SUCCESS_OR_TERMINATE(zeKernelSetArgumentValue(kernel, 0, sizeof(dstBuffer), &dstBuffer));
952+
SUCCESS_OR_TERMINATE(zeKernelSetArgumentValue(kernel, 1, sizeof(srcImg), &srcImg));
953+
SUCCESS_OR_TERMINATE(zeKernelSetArgumentValue(kernel, 2, sizeof(sampler), &sampler));
954+
SUCCESS_OR_TERMINATE(zeKernelSetGroupSize(kernel, xDim, 1u, 1u));
955+
956+
ze_image_region_t srcRegion = {0, 0, 0, (uint32_t)srcImgDesc.width, (uint32_t)srcImgDesc.height, (uint32_t)srcImgDesc.depth};
957+
958+
std::vector<float> data(nPixels * nChannels);
959+
memcpy(data.data(), dstBuffer, allocSize);
960+
961+
SUCCESS_OR_TERMINATE(zeCommandListAppendImageCopyFromMemory(commandHandler.cmdList, srcImg, data.data(),
962+
&srcRegion, nullptr, 0, nullptr));
963+
SUCCESS_OR_TERMINATE(zeCommandListAppendBarrier(commandHandler.cmdList, nullptr, 0, nullptr));
964+
SUCCESS_OR_TERMINATE(commandHandler.appendKernel(kernel, dispatchTraits));
965+
SUCCESS_OR_TERMINATE(commandHandler.execute());
966+
SUCCESS_OR_TERMINATE(commandHandler.synchronize());
967+
968+
// Validate
969+
float *output = reinterpret_cast<float *>(dstBuffer);
970+
std::vector<float> expectedOutput = {10.f, 20.f, 30.f, 40.f, 50.f, 60.f, 70.f, 70.f};
971+
972+
for (auto i = 0u; i < nPixels; ++i) {
973+
for (auto j = 0u; j < nChannels; ++j) {
974+
975+
if (output[i * nChannels + j] != expectedOutput[i]) {
976+
std::cerr << "error: dstBuffer[" << i << "] channel[" << j << "] = " << output[i * nChannels + j] << " is not equal to " << expectedOutput[i] << "\n ";
977+
outputValidated = false;
978+
break;
979+
}
980+
}
981+
}
982+
983+
SUCCESS_OR_TERMINATE(zeMemFree(context, dstBuffer));
984+
SUCCESS_OR_TERMINATE(zeSamplerDestroy(sampler));
985+
SUCCESS_OR_TERMINATE(zeImageDestroy(srcImg));
986+
SUCCESS_OR_TERMINATE(zeKernelDestroy(kernel));
987+
SUCCESS_OR_TERMINATE(zeModuleDestroy(module));
988+
989+
if (outputValidated) {
990+
std::cout << "\nTest PASSED" << std::endl;
991+
} else {
992+
std::cout << "\nTest FAILED" << std::endl;
993+
}
994+
995+
return outputValidated;
996+
}
997+
874998
bool runImageQuery(ze_context_handle_t context, ze_device_handle_t device, ze_module_handle_t module,
875999
const char *kernelName, ze_image_desc_t &imgDesc, std::vector<uint32_t> &reference, bool imgIsSupported) {
8761000
if (!imgIsSupported) {
@@ -1313,7 +1437,7 @@ int main(int argc, char *argv[]) {
13131437
ze_device_uuid_t uuid = deviceProperties.uuid;
13141438
std::string revisionId = std::to_string(reinterpret_cast<uint16_t *>(uuid.id)[2]);
13151439

1316-
int numTests = 8;
1440+
int numTests = 9;
13171441
int testCase = -1;
13181442
testCase = LevelZeroBlackBoxTests::getParamValue(argc, argv, "", "--test-case", -1);
13191443
if (testCase < -1 || testCase >= numTests) {
@@ -1461,6 +1585,15 @@ int main(int argc, char *argv[]) {
14611585
}
14621586

14631587
break;
1588+
1589+
case 8:
1590+
if (is1dImageSupported) {
1591+
std::cout << "\ntest case: testBindlessImage1DSampled\n"
1592+
<< std::endl;
1593+
outputValidated &= testBindlessImage1DSampled(context, device, ss.str(), revisionId, mode);
1594+
} else {
1595+
std::cout << "Skipped. testBindlessImage1DSampled case not supported\n";
1596+
}
14641597
}
14651598

14661599
if (testCase != -1) {

0 commit comments

Comments
 (0)