Advertisement
Guest User

intel.cpp

a guest
Oct 29th, 2023
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.24 KB | None | 0 0
  1. #include "intel.h"
  2. using namespace sycl;
  3.  
  4. size_t num_repetitions = 1;
  5.  
  6. typedef std::vector<vec3d> vertVector;
  7.  
  8. static auto exception_handler = [](sycl::exception_list e_list) {
  9.     for (std::exception_ptr const& e : e_list) {
  10.         try {
  11.             std::rethrow_exception(e);
  12.         }
  13.         catch (std::exception const& e) {
  14. #if _DEBUG
  15.             std::cout << "Failure" << std::endl;
  16. #endif
  17.         }
  18.     }
  19.     };
  20.  
  21. void calculateIntel(queue& q, const vertVector& vert, vertVector& outvert, mat4x4& World, mat4x4& matrix, mat4x4& projection, unsigned int& width, unsigned int& height) {
  22.     range<1> num_items{ vert.size() };
  23.  
  24.     vertVector temp; temp.resize(2);
  25.  
  26.     buffer v_buf(vert);
  27.     buffer v_temp(temp);
  28.     buffer out_buf(outvert.data(), num_items);
  29.  
  30.     for (size_t i = 0; i < num_repetitions; i++) {
  31.         q.submit([&](handler& h) {
  32.             accessor vaccess(v_buf, h, read_only);
  33.             accessor temprw(v_temp, h, read_write, no_init);
  34.             accessor outaccess(out_buf, h, write_only, no_init);
  35.  
  36.             h.parallel_for(num_items, [=](auto i) {
  37.                 temprw[1].x = temprw[0].x * World.m[0][0] + temprw[0].y * World.m[1][0] + temprw[0].z * World.m[2][0] + temprw[0].w * World.m[3][0];
  38.                 temprw[1].y = temprw[0].x * World.m[0][1] + temprw[0].y * World.m[1][1] + temprw[0].z * World.m[2][1] + temprw[0].w * World.m[3][1];
  39.                 temprw[1].z = temprw[0].x * World.m[0][2] + temprw[0].y * World.m[1][2] + temprw[0].z * World.m[2][2] + temprw[0].w * World.m[3][2];
  40.                 temprw[1].w = temprw[0].x * World.m[0][3] + temprw[0].y * World.m[1][3] + temprw[0].z * World.m[2][3] + temprw[0].w * World.m[3][3];
  41.  
  42.                 temprw[0] = temprw[1];
  43.  
  44.                 temprw[1].x = temprw[0].x * matrix.m[0][0] + temprw[0].y * matrix.m[1][0] + temprw[0].z * matrix.m[2][0] + temprw[0].w * matrix.m[3][0];
  45.                 temprw[1].y = temprw[0].x * matrix.m[0][1] + temprw[0].y * matrix.m[1][1] + temprw[0].z * matrix.m[2][1] + temprw[0].w * matrix.m[3][1];
  46.                 temprw[1].z = temprw[0].x * matrix.m[0][2] + temprw[0].y * matrix.m[1][2] + temprw[0].z * matrix.m[2][2] + temprw[0].w * matrix.m[3][2];
  47.                 temprw[1].w = temprw[0].x * matrix.m[0][3] + temprw[0].y * matrix.m[1][3] + temprw[0].z * matrix.m[2][3] + temprw[0].w * matrix.m[3][3];
  48.  
  49.                 temprw[0] = temprw[1];
  50.  
  51.                 outaccess[i].x = temprw[0].x * matrix.m[0][0] + temprw[0].y * matrix.m[1][0] + temprw[0].z * matrix.m[2][0] + temprw[0].w * matrix.m[3][0];
  52.                 outaccess[i].y = temprw[0].x * matrix.m[0][1] + temprw[0].y * matrix.m[1][1] + temprw[0].z * matrix.m[2][1] + temprw[0].w * matrix.m[3][1];
  53.                 outaccess[i].z = temprw[0].x * matrix.m[0][2] + temprw[0].y * matrix.m[1][2] + temprw[0].z * matrix.m[2][2] + temprw[0].w * matrix.m[3][2];
  54.                 outaccess[i].w = temprw[0].x * matrix.m[0][3] + temprw[0].y * matrix.m[1][3] + temprw[0].z * matrix.m[2][3] + temprw[0].w * matrix.m[3][3];
  55.  
  56.                 outaccess[i].x = vaccess[i].x / vaccess[i].w;
  57.                 outaccess[i].y = vaccess[i].y / vaccess[i].w;
  58.                 outaccess[i].z = vaccess[i].z / vaccess[i].w;
  59.                 outaccess[i].x += 1.0f;
  60.                 outaccess[i].y += 1.0f;
  61.                 outaccess[i].x *= 0.5f * (float)width;
  62.                 outaccess[i].y *= 0.5f * (float)height;
  63.                 });
  64.             });
  65.     };
  66.     q.wait();
  67.     temp.clear();
  68. }
  69.  
  70. void calculatePolygonsIntel(__parameters _param, void* _this) {
  71.  
  72.     vertVector* _verts = _param.verts;
  73.     mesh* _mesh = _param._mesh;
  74.     camera* _camera = _param._camera;
  75.     light* _light = _param._light;
  76.  
  77.     mat4x4 World, Rx, Ry, Rz, t;
  78.  
  79.  
  80.     size_t vector_size = _mesh->vertices.size();
  81.  
  82.     _param.Rx = &Rx;
  83.     _param.Ry = &Ry;
  84.     _param.Rz = &Rz;
  85.     _param.t = &t;
  86.     _param.World = &World;
  87.  
  88.     _verts->resize(vector_size);
  89.  
  90.     ((SDLGameEngine*)_this)->_meshApplyRotations(_param);
  91.     ((SDLGameEngine*)_this)->_meshApplyTransations(_param);
  92.  
  93.     World = ((SDLGameEngine*)_this)->_matrixMakeIdentity();
  94.     World = Matrix_MultiplyMatrix(Ry, Rz);
  95.     World = Matrix_MultiplyMatrix(World, Rx);
  96.     World = Matrix_MultiplyMatrix(World, t);
  97.  
  98.     mat4x4 _cameraRotationX;
  99.     mat4x4 _cameraRotationY;
  100.     mat4x4 _cameraRotationZ;
  101.     mat4x4 _cameraRotation;
  102.  
  103.     _param.Rx = &_cameraRotationX;
  104.     _param.Ry = &_cameraRotationY;
  105.     _param.Rz = &_cameraRotationZ;
  106.  
  107.     ((SDLGameEngine*)_this)->_cameraApplyRotations(_param);
  108.  
  109.     _cameraRotation = Matrix_MultiplyMatrix(_cameraRotationZ, _cameraRotationY);
  110.     _cameraRotation = Matrix_MultiplyMatrix(_cameraRotation, _cameraRotationZ);
  111.  
  112.     _camera->_up = { 0, 1, 0 };
  113.     _camera->_target = { 0, 0, 1 };
  114.     _camera->_pointAt = MultiplyMatrixVector(_cameraRotation, _camera->_target);
  115.  
  116.     _camera->_target = Vector_Add(_camera->position, _camera->_pointAt);
  117.  
  118.     mat4x4 __matrix = Matrix_PointAt(_camera->position, _camera->_target, _camera->_up);
  119.     _camera->_matrix = __matrix; Matrix_QuickInverse(__matrix);
  120.  
  121.     size_t size = _mesh->vertices.size() - 1;
  122.  
  123.     try {
  124.         queue q;//cpu_selector_v, exception_handler);
  125.  
  126.         // Print out the device information used for the kernel code.
  127.         std::cout << "Running on device: "
  128.             << q.get_device().get_info<info::device::name>() << "\n";
  129.         std::cout << "Vector size: " << _mesh->vertices.size() << "\n";
  130.  
  131.         // Vector addition in SYCL
  132.         calculateIntel(q, _mesh->vertices, *_verts, World, _camera->_matrix, _camera->projection, ((SDLGameEngine*)_this)->screen.SCREEN_WIDTH, ((SDLGameEngine*)_this)->screen.SCREEN_HEIGHT);
  133.     }
  134.     catch (sycl::exception const& e) {
  135.         cout << e.what();
  136.         ((SDLGameEngine*)_this)->throwException(__FILE__, __FUNCTION__, e.what(), false);
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement