Cleaned up some util items, fixed texture passing using unique_ptr and it's move semantics

master
MitchellHansen 8 years ago
parent 941754d4f0
commit 1e54243b99

@ -6,6 +6,7 @@
#include <iostream> #include <iostream>
#include "Vector4.hpp" #include "Vector4.hpp"
#include <string.h> #include <string.h>
#include <memory>
#ifdef linux #ifdef linux
#include <CL/cl.h> #include <CL/cl.h>
@ -121,7 +122,7 @@ private:
// Maps which contain a mapping from "name" to the host side CL memory object // Maps which contain a mapping from "name" to the host side CL memory object
std::unordered_map<std::string, cl_kernel> kernel_map; std::unordered_map<std::string, cl_kernel> kernel_map;
std::unordered_map<std::string, cl_mem> buffer_map; std::unordered_map<std::string, cl_mem> buffer_map;
std::unordered_map<std::string, std::pair<sf::Sprite, sf::Texture>> image_map; std::unordered_map<std::string, std::pair<sf::Sprite, std::unique_ptr<sf::Texture>>> image_map;
std::vector<device> device_list; std::vector<device> device_list;
// Query the hardware on this machine and store the devices // Query the hardware on this machine and store the devices

@ -16,88 +16,6 @@
const double PI = 3.141592653589793238463; const double PI = 3.141592653589793238463;
const float PI_F = 3.14159265358979f; const float PI_F = 3.14159265358979f;
struct fps_counter {
public:
fps_counter() :
backdrop(sf::Vector2f(200, 100)), vertex_array(sf::LinesStrip) {
backdrop.setFillColor(sf::Color(0x0000003F));
if(!f.loadFromFile("../assets/fonts/Arial.ttf")){
std::cout << "couldn't find the fall back Arial font in ../assets/fonts/" << std::endl;
} else {
t.setFont(f);
t.setCharacterSize(18);
t.setColor(sf::Color::White);
}
}
void frame(double delta_time){
// Apply 100 units of smoothing
if (frame_count == 100){
frame_count = 0;
fps_average = 0;
}
frame_count++;
fps_average += (delta_time - fps_average) / frame_count;
}
void flip_units() {
if (milliseconds)
milliseconds = false;
else
milliseconds = true;
}
void draw(sf::RenderWindow *r){
r->draw(backdrop);
if (vertex_position == 200)
vertex_position = 0;
sf::Vector2f origin = backdrop.getPosition();
sf::Vector2f point = origin + sf::Vector2f(vertex_position, backdrop.getSize().y - (1.0/fps_average));
if (vertex_array.getVertexCount() < 200)
vertex_array.append(sf::Vertex(point, sf::Color::Red));
else
vertex_array[vertex_position] = sf::Vertex(point, sf::Color::Red);
r->draw(vertex_array);
vertex_position++;
std::string out;
if (milliseconds)
out = std::to_string(fps_average);
else
out = std::to_string(floor(1 / fps_average));
t.setString(out);
r->draw(t);
}
private:
sf::RectangleShape backdrop;
sf::VertexArray vertex_array;
sf::Font f;
sf::Text t;
int frame_count = 0;
double fps_average = 0;
bool milliseconds = false;
int vertex_position = 0;
};
inline sf::Vector3f SphereToCart(sf::Vector2f i) { inline sf::Vector3f SphereToCart(sf::Vector2f i) {
auto r = sf::Vector3f( auto r = sf::Vector3f(
@ -224,25 +142,18 @@ inline void DumpLog(std::stringstream* ss, std::string file_name) {
} }
#ifdef _MSC_VER
# include <intrin.h>
# define __builtin_popcount _mm_popcnt_u32
# define __builtin_popcountll _mm_popcnt_u64
#endif
inline int count_bits(int32_t v) { inline int count_bits(int32_t v) {
v = v - ((v >> 1) & 0x55555555); // reuse input as temporary return static_cast<int>(__builtin_popcount(v));
v = (v & 0x33333333) + ((v >> 2) & 0x33333333); // temp
return (((v + (v >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24; // count
} }
inline int count_bits(int64_t v) { inline int count_bits(int64_t v) {
int32_t left = (int32_t)(v); return static_cast<int>(__builtin_popcountll(v));
int32_t right = (int32_t)(v >> 32); }
left = left - ((left >> 1) & 0x55555555); // reuse input as temporary
left = (left & 0x33333333) + ((left >> 2) & 0x33333333); // temp
left = ((left + (left >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; // count
right = right - ((right >> 1) & 0x55555555); // reuse input as temporary
right = (right & 0x33333333) + ((right >> 2) & 0x33333333); // temp
right = ((right + (right >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; // count
return left + right;
}

@ -1,6 +1,7 @@
#include <OpenCL.h> #include <OpenCL.h>
#include "util.hpp" #include "util.hpp"
OpenCL::OpenCL() { OpenCL::OpenCL() {
} }
@ -36,8 +37,8 @@ void OpenCL::run_kernel(std::string kernel_name, sf::Vector2i work_size) {
} }
void OpenCL::draw(sf::RenderWindow *window) { void OpenCL::draw(sf::RenderWindow *window) {
for (auto i: image_map) { for (auto &&i: image_map) {
window->draw(i.second.first); window->draw(i.second.first);
} }
} }
@ -88,6 +89,7 @@ bool OpenCL::aquire_hardware()
} }
} }
return true;
} }
bool OpenCL::create_shared_context() { bool OpenCL::create_shared_context() {
@ -257,20 +259,20 @@ bool OpenCL::create_image_buffer(std::string buffer_name, sf::Vector2i size, cl_
image_map.erase(buffer_name); image_map.erase(buffer_name);
} }
sf::Texture texture; std::unique_ptr<sf::Texture> texture(new sf::Texture);
texture.create(size.x, size.y); texture->create(size.x, size.y);
sf::Sprite sprite(texture);
image_map[buffer_name] = std::make_pair(sprite, texture);
cl_mem buff = clCreateFromGLTexture( cl_mem buff = clCreateFromGLTexture(
context, access_type, GL_TEXTURE_2D, context, access_type, GL_TEXTURE_2D,
0, texture.getNativeHandle(), &error); 0, texture->getNativeHandle(), &error);
if (vr_assert(error, "clCreateFromGLTexture")) if (vr_assert(error, "clCreateFromGLTexture"))
return false; return false;
sf::Sprite sprite(*texture);
image_map[buffer_name] = std::pair<sf::Sprite, std::unique_ptr<sf::Texture>>(sf::Sprite(*texture), std::move(texture));
store_buffer(buff, buffer_name); store_buffer(buff, buffer_name);
return true; return true;

@ -38,20 +38,17 @@ int main() {
OpenCL cl; OpenCL cl;
sf::Vector4f range(-1.0f, 1.0f, -1.0f, 1.0f); sf::Vector4f range(-1.0f, 1.0f, -1.0f, 1.0f);
sf::Vector2i window_dimensions(WINDOW_X, WINDOW_Y); sf::Vector2i image_resolution(WINDOW_X, WINDOW_Y);
cl.init(); if (!cl.init())
return -1;
while (!cl.compile_kernel("../kernels/mandlebrot.cl", "mandlebrot")) { while (!cl.compile_kernel("../kernels/mandlebrot.cl", "mandlebrot")) {
std::cin.get(); std::cin.get();
} }
sf::Texture t; cl.create_image_buffer("viewport_image", image_resolution, CL_MEM_WRITE_ONLY);
t.create(WINDOW_X, WINDOW_Y); cl.create_buffer("image_res", sizeof(sf::Vector2i), &image_resolution);
sf::Sprite window_sprite(t);
cl.create_image_buffer("viewport_image", &t, CL_MEM_WRITE_ONLY);
cl.create_buffer("image_res", sizeof(sf::Vector2i), &window_dimensions);
cl.create_buffer("range", sizeof(sf::Vector4f), (void*)&range, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR); cl.create_buffer("range", sizeof(sf::Vector4f), (void*)&range, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR);
cl.set_kernel_arg("mandlebrot", 0, "image_res"); cl.set_kernel_arg("mandlebrot", 0, "image_res");
@ -67,32 +64,32 @@ int main() {
} }
if (event.type == sf::Event::KeyPressed) { if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Down) { if (event.key.code == sf::Keyboard::Down) {
range.z += 0.001; range.z += 0.001f;
range.w += 0.001; range.w += 0.001f;
} }
if (event.key.code == sf::Keyboard::Up) { if (event.key.code == sf::Keyboard::Up) {
range.z -= 0.001; range.z -= 0.001f;
range.w -= 0.001; range.w -= 0.001f;
} }
if (event.key.code == sf::Keyboard::Right) { if (event.key.code == sf::Keyboard::Right) {
range.x += 0.001; range.x += 0.001f;
range.y += 0.001; range.y += 0.001f;
} }
if (event.key.code == sf::Keyboard::Left) { if (event.key.code == sf::Keyboard::Left) {
range.x -= 0.001; range.x -= 0.001f;
range.y -= 0.001; range.y -= 0.001f;
} }
if (event.key.code == sf::Keyboard::Equal) { if (event.key.code == sf::Keyboard::Equal) {
range.x *= 1.02; range.x *= 1.02f;
range.y *= 1.02; range.y *= 1.02f;
range.z *= 1.02; range.z *= 1.02f;
range.w *= 1.02; range.w *= 1.02f;
} }
if (event.key.code == sf::Keyboard::Dash) { if (event.key.code == sf::Keyboard::Dash) {
range.x *= 0.98; range.x *= 0.98f;
range.y *= 0.98; range.y *= 0.98f;
range.z *= 0.98; range.z *= 0.98f;
range.w *= 0.98; range.w *= 0.98f;
} }
} }
} }
@ -111,9 +108,8 @@ int main() {
window.clear(sf::Color::White); window.clear(sf::Color::White);
cl.run_kernel("mandlebrot", window_dimensions); cl.run_kernel("mandlebrot", image_resolution);
cl.draw(&window); cl.draw(&window);
window.draw(window_sprite);
window.display(); window.display();

Loading…
Cancel
Save