File: test_sharedusm_data.cpp

package info (click to toggle)
ospray 3.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,048 kB
  • sloc: cpp: 80,569; ansic: 951; sh: 805; makefile: 170; python: 69
file content (129 lines) | stat: -rw-r--r-- 3,595 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2023 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

#ifdef SYCL_LANGUAGE_VERSION

#include <ospray/ospray_util.h>
#include <cmath>
#include <random>
#include <vector>
#include "test_fixture.h"

extern OSPRayEnvironment *ospEnv;
#include "sycl/sycl.hpp"

namespace OSPRayTestScenes {

// Fixture class for a unit test of gpu capable, data sharing
class TestUSMSharing
    : public Base,
      public ::testing::TestWithParam<const char * /*memory type*/>
{
 public:
  TestUSMSharing();
  ~TestUSMSharing();
  void SetUp() override;

 protected:
  std::string memoryType; // host, shared, device
 private:
  std::vector<float> voxels;
  float *deviceVoxels = nullptr;
};

TestUSMSharing::TestUSMSharing()
{
  memoryType = GetParam();
}

TestUSMSharing::~TestUSMSharing()
{
  sycl::queue *appsSyclQueue = ospEnv->GetAppsSyclQueue();
  if (appsSyclQueue)
    free(deviceVoxels, *appsSyclQueue);
}

void TestUSMSharing::SetUp()
{
  Base::SetUp();

  vec3i dims{100, 100, 100};
  size_t numVoxels = dims.long_product();
  voxels = std::vector<float>(numVoxels);
  for (int x = 0; x < dims.x; ++x) {
    double dx = (double)x / (double)dims.x - 0.5;
    for (int y = 0; y < dims.y; ++y) {
      double dy = (double)y / (double)dims.y - 0.5;
      for (int z = 0; z < dims.z; ++z) {
        double dz = (double)z / (double)dims.z - 0.5;
        voxels[x * dims.y * dims.z + y * dims.z + z] =
            1.0 - sqrt(dx * dx + dy * dy + dz * dz);
      }
    }
  }

  deviceVoxels = voxels.data();
  sycl::queue *appsSyclQueue = ospEnv->GetAppsSyclQueue();
  if (appsSyclQueue) {
    if (memoryType == "host") {
      deviceVoxels = sycl::malloc_host<float>(numVoxels, *appsSyclQueue);
    } else if (memoryType == "device") {
      deviceVoxels = sycl::malloc_device<float>(numVoxels, *appsSyclQueue);
    } else {
      deviceVoxels = sycl::malloc_shared<float>(numVoxels, *appsSyclQueue);
    }
    appsSyclQueue->memcpy(
        deviceVoxels, voxels.data(), numVoxels * sizeof(float));
    appsSyclQueue->wait();
  }

  // create and setup camera
  camera.setParam("position", vec3f(200.f, 50.f, 50.f));
  camera.setParam("up", vec3f(0.f, 1.f, 0.f));
  camera.setParam("direction", vec3f(-1.f, 0.f, 0.f));

  // create and setup model and mesh
  ospray::cpp::Volume volume("structuredRegular");
  volume.setParam("data", cpp::SharedData(deviceVoxels, dims));
  volume.commit();

  ospray::cpp::TransferFunction tf("piecewiseLinear");
  std::vector<vec3f> color = {vec3f(0.5f, 0.0f, 0.0f),
      vec3f(0.0f, 0.5f, 0.0f),
      vec3f(0.0f, 0.0f, 1.0f)};
  std::vector<float> opacity = {0.05f, 0.0f, 0.1f, 0.0f, 0.2f, 0.0f, 1.0f};

  tf.setParam("value", box1f(0.14f, 1.0f));
  tf.setParam("color", ospray::cpp::CopiedData(color));
  tf.setParam("opacity", ospray::cpp::CopiedData(opacity));
  tf.commit();

  ospray::cpp::VolumetricModel vm(volume);
  vm.setParam("transferFunction", tf);
  vm.commit();

  AddModel(vm);

  cpp::Light distant("distant");
  distant.setParam("intensity", 3.0f);
  distant.setParam("direction", vec3f(0.3f, -4.0f, 0.8f));
  distant.setParam("color", vec3f(1.0f, 0.5f, 0.5f));
  distant.setParam("angularDiameter", 1.0f);
  AddLight(distant);

  cpp::Light ambient = ospNewLight("ambient");
  ambient.setParam("intensity", 0.1f);
  AddLight(ambient);
}

// Test Instantiations //////////////////////////////////////////////////////

TEST_P(TestUSMSharing, structured_regular)
{
  PerformRenderTest();
}

INSTANTIATE_TEST_SUITE_P(
    SharedData, TestUSMSharing, ::testing::Values("host", "shared", "device"));
} // namespace OSPRayTestScenes
#endif