Advertisement
Guest User

glpp

a guest
Nov 1st, 2014
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.82 KB | None | 0 0
  1. #include "glcommon.h"
  2. #include "glcontext.h"
  3. #include "glbuffer.h"
  4. #include "glstatetracker.h"
  5. #include <iostream>
  6. #include <thread>
  7. #include <chrono>
  8. #include <fstream>
  9.  
  10. #include <chrono>
  11. #include <random>
  12. template <typename tContainer>
  13. void gen_random_values(std::uint32_t count, tContainer& dst, float low, float high, int seed)
  14. {
  15.     std::mt19937 rng;
  16.     rng.seed(seed);
  17.     std::uniform_real_distribution<float> distr(low,high);
  18.    
  19.     for(std::uint32_t i = 0; i < count; ++i)
  20.     {
  21.         dst.push_back(distr(rng));
  22.     }
  23. }
  24.  
  25. int main(int, char**)
  26. {
  27.     try
  28.     {
  29.         GLContextCreationInfo cci;
  30.         cci.glVersion = GLV_4_3; // Up to 4.5. Sadly, AMD drivers don't reach yet.
  31.         std::shared_ptr<GLContext> pContext = GLContext_New(cci);
  32.         if(!pContext)
  33.             return 1;
  34.        
  35.         const std::uint32_t iter_count = 1;
  36.         std::cout << "Running " << iter_count << " test iteration/s..." << std::endl;
  37.        
  38.         std::ifstream cshader("input.txt");
  39.         std::string str;
  40.         std::string l;
  41.         while(std::getline(cshader,l)) { str+=l; str += '\n'; l.clear(); }
  42.        
  43.         std::shared_ptr<GLFunctions> pFunctions = pContext->GetFunctions().lock();
  44.         GLStateTracker& Tracker = *pContext->GetStates();
  45.        
  46.         std::vector<GLfloat> src_a;
  47.         std::vector<GLfloat> src_b;
  48.         std::vector<GLfloat> dst;
  49.         std::vector<GLfloat> cpu_dst;
  50.        
  51.         gen_random_values(0xFFFF,src_a,0.f,500.f,42);
  52.         gen_random_values(0xFFFF,src_b,0.f,500.f,53);
  53.         GLuint length = std::min(src_a.size(),src_b.size());
  54.         {
  55.             dst.resize(length);
  56.             cpu_dst.resize(length);
  57.         }
  58.        
  59.         GLShader computeShader(glppH(pContext),ST_COMPUTE_SHADER);
  60.         {
  61.             computeShader.glppShaderSource(str);
  62.             std::string result;
  63.             bool rv = computeShader.glppCompile(&result);
  64.             if(!rv)
  65.             {
  66.                 std::cout << std::endl << "Shader Compilation Failed: "
  67.                     << std::endl << result << std::endl;
  68.                 return 1;
  69.             }
  70.         }
  71.            
  72.         GLBuffer destBuffer(glppH(pContext));
  73.         {
  74.             Tracker.BufferState().BindBuffer(destBuffer,BBT_SHADER_STORAGE_BUFFER);
  75.             Tracker.BufferState().BufferData(BBT_SHADER_STORAGE_BUFFER,
  76.                 sizeof(GLint)*length,0,MUH_STATIC_READ);
  77.         }
  78.        
  79.         GLBuffer srcA_Buffer(glppH(pContext));
  80.         {
  81.             Tracker.BufferState().BindBuffer(srcA_Buffer,BBT_SHADER_STORAGE_BUFFER);
  82.             Tracker.BufferState().BufferData(BBT_SHADER_STORAGE_BUFFER,
  83.                 sizeof(GLint)*length,src_a.data(),MUH_STREAM_READ);
  84.         }
  85.        
  86.         GLBuffer srcB_Buffer(glppH(pContext));
  87.         {
  88.             Tracker.BufferState().BindBuffer(srcB_Buffer,BBT_SHADER_STORAGE_BUFFER);
  89.             Tracker.BufferState().BufferData(BBT_SHADER_STORAGE_BUFFER,
  90.                 sizeof(GLint)*length,src_b.data(),MUH_STREAM_READ);
  91.         }
  92.            
  93.         GLProgram computeProgram(glppH(pContext));
  94.         {
  95.             computeProgram.AttachShader(computeShader);
  96.             std::string result;
  97.             bool rv = computeProgram.glppLinkProgram(&result);
  98.             if(!rv)
  99.             {
  100.                 std::cout << std::endl << "Program Linking Failed: "
  101.                     << std::endl << result << std::endl;
  102.                 return 1;
  103.             }
  104.         }
  105.        
  106.         // Get Ready
  107.         {
  108.             Tracker.BufferState().BindBufferBase(destBuffer,BBTI_SHADER_STORAGE_BUFFER,0);
  109.             Tracker.BufferState().BindBufferBase(srcA_Buffer,BBTI_SHADER_STORAGE_BUFFER,1);
  110.             Tracker.BufferState().BindBufferBase(srcB_Buffer,BBTI_SHADER_STORAGE_BUFFER,2);
  111.             computeProgram.ShaderStorageBlockBinding(0,0);
  112.             computeProgram.ShaderStorageBlockBinding(1,1);
  113.             computeProgram.ShaderStorageBlockBinding(2,2);
  114.             computeProgram.ProgramUniform1ui(computeProgram.GetUniformLocation("length"),length);
  115.             Tracker.ProgramState().UseProgram(computeProgram);
  116.         }
  117.        
  118.         auto compute_start = std::chrono::high_resolution_clock::now();
  119.         // Begin Compute
  120.         {
  121.             for(std::uint32_t iter = 0; iter < iter_count; ++iter)
  122.             {
  123.                 pFunctions->glDispatchCompute(1,1,1);
  124.                 GLContext::CheckErrors(*pFunctions,"DispatchCompute"); // Check for errors.
  125.                 // If an error occurs, it's "DispatchCompute" 's fault.
  126.                 // Errors are not checked automatically for raw function calls.
  127.             }
  128.         }
  129.        
  130.         // Read back
  131.         if(length)
  132.         {
  133.             Tracker.BufferState().BindBuffer(destBuffer,BBT_SHADER_STORAGE_BUFFER);
  134.             GLvoid* d = Tracker.BufferState().MapBuffer(BBT_SHADER_STORAGE_BUFFER,MMSB_READ_ONLY);
  135.             memcpy(dst.data(),d,sizeof(GLint)*dst.size());
  136.             Tracker.BufferState().UnmapBuffer(BBT_SHADER_STORAGE_BUFFER);
  137.         }
  138.         auto compute_end = std::chrono::high_resolution_clock::now(); // compute_end is taken now, since
  139.         // DispatchCompute may be delaying computation until later time.
  140.         // Alternatively, a glFinish call after DispatchCompute may work as well.
  141.        
  142.         auto cpu_compute_start = std::chrono::high_resolution_clock::now();
  143.         for(std::uint32_t iter = 0; iter < iter_count; ++iter)
  144.         {
  145.             for(std::uint32_t i = 0; i < cpu_dst.size(); ++i)
  146.             {
  147.                 cpu_dst[i] =
  148.                     std::sqrt((std::sqrt(src_a[i])*std::sqrt(src_b[i]))
  149.                         * std::sqrt(src_a[i]*src_b[i]));
  150.             }
  151.         }
  152.         auto cpu_compute_end = std::chrono::high_resolution_clock::now();
  153.        
  154.         std::cout << "GPU Computation Time: "
  155.             << std::chrono::duration_cast<std::chrono::microseconds>
  156.                 (compute_end-compute_start).count()
  157.             << "us" << std::endl;
  158.         std::cout << "CPU Computation Time: "
  159.             << std::chrono::duration_cast<std::chrono::microseconds>
  160.                 (cpu_compute_end-cpu_compute_start).count()
  161.             << "us" << std::endl;
  162.         std::cout << "Computed values: " << std::endl;
  163.         for(std::uint32_t i = 0; i < std::min<std::uint32_t>(dst.size(),16); ++i)
  164.         {
  165.             std::cout << "\t" << src_a[i] << " ~ " << src_b[i] << " = "
  166.                 << dst[i] << " (" << cpu_dst[i] << ")" << std::endl;
  167.             if(i && (i%5)==0)
  168.                 std::cout << std::endl;
  169.         }
  170.        
  171.         while(pContext->Update())
  172.         {
  173.             pContext->Begin();
  174.             pFunctions->glClearColor(1,1,1,1);
  175.             pFunctions->glClear(GL_COLOR_BUFFER_BIT);
  176.            
  177.             pContext->End();
  178.         }
  179.     }
  180.     catch(GlException& e)
  181.     {
  182.         std::cout << std::endl << "Unhandled GL Exception: " << e.what()
  183.             << " (Error ID: " << GLContext::ErrorName(e.error()) << ")" << std::endl;
  184.         return 1;
  185.     }
  186.     return 0;
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement