Continuing on adding argument handling.

Added a small sample texture and sprite which can be handed over to either gl or cl and then handed back. It can then be rendered.
Changed to just an array of floats for the view matrix
master
mitchellhansen 8 years ago
parent 11146a6551
commit a7234675cb

@ -42,17 +42,20 @@ public:
int run_kernel(std::string kernel_name); int run_kernel(std::string kernel_name);
bool assert(int error_code, std::string function_name);
cl_device_id getDeviceID(); cl_device_id getDeviceID();
cl_platform_id getPlatformID(); cl_platform_id getPlatformID();
cl_context getContext(); cl_context getContext();
cl_kernel getKernel(std::string kernel_name); cl_kernel getKernel(std::string kernel_name);
cl_command_queue getCommandQueue();
private: private:
int error = 0; int error = 0;
bool initialized = false; bool initialized = false;
bool assert(int error_code, std::string function_name);
cl_platform_id platform_id; cl_platform_id platform_id;
cl_device_id device_id; cl_device_id device_id;
cl_context context; cl_context context;

@ -3,13 +3,17 @@ __kernel void min_kern(
global char* map, global char* map,
global int3* map_dim, global int3* map_dim,
global int2* resolution, global int2* resolution,
global float3* projection_matrix global float3* projection_matrix,
global float3* cam_dir,
global float3* cam_pos
){ ){
size_t id = get_global_id(0); size_t id = get_global_id(0);
//printf("%i %c -- ", id, map[id]); //printf("%i %c -- ", id, map[id]);
//printf("%i, %i, %i\n", map_dim->x, map_dim->y, map_dim->z); //printf("%i, %i, %i\n", map_dim->x, map_dim->y, map_dim->z);
printf("\n%i\nX: %f\nY: %f\nZ: %f\n", id, projection_matrix[id].x, projection_matrix[id].y, projection_matrix[id].z); //printf("\n%i\nX: %f\nY: %f\nZ: %f\n", id, projection_matrix[id].x, projection_matrix[id].y, projection_matrix[id].z);
//printf("%f, %f, %f\n", cam_dir->x, cam_dir->y, cam_dir->z);
//printf("%f, %f, %f\n", cam_pos->x, cam_pos->y, cam_pos->z);
} }

@ -254,7 +254,8 @@ int CL_Wrapper::run_kernel(std::string kernel_name){
cl_device_id CL_Wrapper::getDeviceID(){ return device_id; }; cl_device_id CL_Wrapper::getDeviceID(){ return device_id; };
cl_platform_id CL_Wrapper::getPlatformID(){ return platform_id; }; cl_platform_id CL_Wrapper::getPlatformID(){ return platform_id; };
cl_context CL_Wrapper::getContext(){ return context; }; cl_context CL_Wrapper::getContext(){ return context; };
cl_kernel CL_Wrapper::getKernel(std::string kernel_name ){ return kernel_map.at(kernel_name); } cl_kernel CL_Wrapper::getKernel(std::string kernel_name ){ return kernel_map.at(kernel_name); };
cl_command_queue CL_Wrapper::getCommandQueue(){ return command_queue; };
bool CL_Wrapper::assert(int error_code, std::string function_name){ bool CL_Wrapper::assert(int error_code, std::string function_name){

@ -15,6 +15,7 @@
#include <windows.h> #include <windows.h>
#elif defined TARGET_OS_MAC #elif defined TARGET_OS_MAC
#include <OpenGL/gl.h>
# include <OpenGL/OpenGL.h> # include <OpenGL/OpenGL.h>
# include <OpenCL/opencl.h> # include <OpenCL/opencl.h>
#include <OpenCL/cl_gl_ext.h> #include <OpenCL/cl_gl_ext.h>
@ -33,8 +34,38 @@ const int WINDOW_Y = 150;
int main(){
float elap_time(){
static std::chrono::time_point<std::chrono::system_clock> start;
static bool started = false;
if (!started){
start = std::chrono::system_clock::now();
started = true;
}
std::chrono::time_point<std::chrono::system_clock> now = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_time = now - start;
return elapsed_time.count();
}
sf::Sprite window_sprite;
sf::Texture window_texture;
// Y: -1.57 is straight up
// Y: 1.57 is straight down
int main() {
sf::RenderWindow window(sf::VideoMode(WINDOW_X, WINDOW_Y), "SFML");
sf::Sprite s;
sf::Texture t;
{
CL_Wrapper c; CL_Wrapper c;
c.acquire_platform_and_device(); c.acquire_platform_and_device();
c.create_shared_context(); c.create_shared_context();
@ -51,7 +82,7 @@ int main(){
char map[100 * 100 * 100]; char map[100 * 100 * 100];
for (int i = 0; i < 100*100*100; i++){ for (int i = 0; i < 100 * 100 * 100; i++) {
map[i] = '+'; map[i] = '+';
} }
@ -59,7 +90,7 @@ int main(){
cl_mem map_buff = clCreateBuffer( cl_mem map_buff = clCreateBuffer(
c.getContext(), CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, c.getContext(), CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
sizeof(char) * 100*100*100, map, NULL sizeof(char) * 100 * 100 * 100, map, NULL
); );
int dim[3] = {101, 100, 99}; int dim[3] = {101, 100, 99};
@ -80,8 +111,9 @@ int main(){
double x_increment_radians = DegreesToRadians(80.0 / res[0]); double x_increment_radians = DegreesToRadians(80.0 / res[0]);
// SFML 2.4 has Vector4 datatypes....... // SFML 2.4 has Vector4 datatypes.......
sf::Vector3f* view_plane_vectors = new sf::Vector3f[res[0] * res[1]];
for (int y = -res[1] / 2 ; y < res[1] / 2; y++) { float view_matrix[res[0] * res[1] * 4];
for (int y = -res[1] / 2; y < res[1] / 2; y++) {
for (int x = -res[0] / 2; x < res[0] / 2; x++) { for (int x = -res[0] / 2; x < res[0] / 2; x++) {
// The base ray direction to slew from // The base ray direction to slew from
@ -102,73 +134,95 @@ int main(){
); );
int index = (x + res[0] / 2) + res[0] * (y + res[1] / 2); int index = (x + res[0] / 2) + res[0] * (y + res[1] / 2);
view_plane_vectors[index] = Normalize(ray); ray = Normalize(ray);
view_matrix[index * 4 + 0] = ray.x;
view_matrix[index * 4 + 1] = ray.y;
view_matrix[index * 4 + 2] = ray.z;
view_matrix[index * 4 + 3] = 0;
} }
} }
int ind = 1; // int ind = 4;
std::cout << "\nX: " << view_plane_vectors[ind].x // std::cout << "\nX: " << view_matrix[ind]
<< "\nY: " << view_plane_vectors[ind].y // << "\nY: " << view_matrix[ind + 1]
<< "\nZ: " << view_plane_vectors[ind].z; // << "\nZ: " << view_matrix[ind + 2]
// << "\npad: " << view_matrix[ind + 3];
std::cout << "\n======================" << std::endl; //
// std::cout << "\n======================" << std::endl;
cl_mem view_matrix_buff = clCreateBuffer( cl_mem view_matrix_buff = clCreateBuffer(
c.getContext(), CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, c.getContext(), CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
sizeof(float) * 3 * res[0] * res[1], &view_plane_vectors[0], NULL sizeof(float) * 3 * res[0] * res[1], view_matrix, NULL
);
float cam_dir[4] = {1, 0, 0, 0};
cl_mem cam_dir_buff = clCreateBuffer(
c.getContext(), CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
sizeof(float) * 4, cam_dir, NULL
);
float cam_pos[4] = {25, 25, 25, 0};
cl_mem cam_pos_buff = clCreateBuffer(
c.getContext(), CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
sizeof(float) * 4, cam_pos, NULL
); );
c.store_buffer(buff, "buffer_1"); c.store_buffer(buff, "buffer_1");
c.store_buffer(map_buff, "map_buffer"); c.store_buffer(map_buff, "map_buffer");
c.store_buffer(dim_buff, "dim_buffer"); c.store_buffer(dim_buff, "dim_buffer");
c.store_buffer(res_buff, "res_buffer"); c.store_buffer(res_buff, "res_buffer");
c.store_buffer(view_matrix_buff, "view_matrix_buffer"); c.store_buffer(view_matrix_buff, "view_matrix_buffer");
c.store_buffer(cam_dir_buff, "cam_dir_buffer");
c.store_buffer(cam_pos_buff, "cam_pos_buffer");
c.set_kernel_arg("min_kern", 0, "buffer_1"); c.set_kernel_arg("min_kern", 0, "buffer_1");
c.set_kernel_arg("min_kern", 1, "map_buffer"); c.set_kernel_arg("min_kern", 1, "map_buffer");
c.set_kernel_arg("min_kern", 2, "dim_buffer"); c.set_kernel_arg("min_kern", 2, "dim_buffer");
c.set_kernel_arg("min_kern", 3, "res_buffer"); c.set_kernel_arg("min_kern", 3, "res_buffer");
c.set_kernel_arg("min_kern", 4, "view_matrix_buffer"); c.set_kernel_arg("min_kern", 4, "view_matrix_buffer");
c.set_kernel_arg("min_kern", 5, "cam_dir_buffer");
c.set_kernel_arg("min_kern", 6, "cam_pos_buffer");
c.run_kernel("min_kern"); c.run_kernel("min_kern");
}; unsigned char* pixel_array = new sf::Uint8[WINDOW_X * WINDOW_Y * 4];
for (int i = 0; i < 100 * 100 * 4; i += 4) {
float elap_time(){ pixel_array[i] = i % 255; // R?
static std::chrono::time_point<std::chrono::system_clock> start; pixel_array[i + 1] = 70; // G?
static bool started = false; pixel_array[i + 2] = 100; // B?
pixel_array[i + 3] = 100; // A?
if (!started){
start = std::chrono::system_clock::now();
started = true;
} }
std::chrono::time_point<std::chrono::system_clock> now = std::chrono::system_clock::now(); t.create(100, 100);
std::chrono::duration<double> elapsed_time = now - start; t.update(pixel_array);
return elapsed_time.count();
}
sf::Sprite window_sprite; int error;
sf::Texture window_texture;
// Y: -1.57 is straight up cl_mem image_buff = clCreateFromGLTexture(c.getContext(), CL_MEM_WRITE_ONLY, GL_TEXTURE_2D, 0, t.getNativeHandle(), &error);
// Y: 1.57 is straight down if (c.assert(error, "clCreateFromGLTexture"))
return -1;
error = clEnqueueAcquireGLObjects(c.getCommandQueue(), 1, &image_buff, 0, 0, 0);
if (c.assert(error, "clEnqueueAcquireGLObjects"))
return -1;
int main0() { //c.run_kernel("min_kern");
// Initialize the render window error = clEnqueueReleaseGLObjects(c.getCommandQueue(), 1, &image_buff, 0, NULL, NULL);
Curses curse(sf::Vector2i(5, 5), sf::Vector2i(WINDOW_X, WINDOW_Y)); if (c.assert(error, "clEnqueueReleaseGLObjects"))
sf::RenderWindow window(sf::VideoMode(WINDOW_X, WINDOW_Y), "SFML"); return -1;
s.setTexture(t);
}
// The step size in milliseconds between calls to Update() // The step size in milliseconds between calls to Update()
// Lets set it to 16.6 milliseonds (60FPS) // Lets set it to 16.6 milliseonds (60FPS)
float step_size = 0.0166f; float step_size = 0.0166f;
@ -264,21 +318,6 @@ int main0() {
cam_pos.y += cam_vec.y / 1.0; cam_pos.y += cam_vec.y / 1.0;
cam_pos.z += cam_vec.z / 1.0; cam_pos.z += cam_vec.z / 1.0;
// if (cam_vec.x > 0.0f)
// cam_vec.x -= 0.1;
// else if (cam_vec.x < 0.0f)
// cam_vec.x += 0.1;
//
// if (cam_vec.y > 0.0f)
// cam_vec.y -= 0.1;
// else if (cam_vec.y < 0.0f)
// cam_vec.y += 0.1;
//
// if (cam_vec.z > 0.0f)
// cam_vec.z -= 0.1;
// else if (cam_vec.z < 0.0f)
// cam_vec.z += 0.1;
std::cout << cam_vec.x << " : " << cam_vec.y << " : " << cam_vec.z << std::endl; std::cout << cam_vec.x << " : " << cam_vec.y << " : " << cam_vec.z << std::endl;
@ -292,11 +331,7 @@ int main0() {
while ((accumulator_time - step_size) >= step_size) { while ((accumulator_time - step_size) >= step_size) {
accumulator_time -= step_size; accumulator_time -= step_size;
// Update cycle // Update cycle
curse.Update(delta_time);
} }
// Fps cycle // Fps cycle
@ -307,16 +342,6 @@ int main0() {
// Cast the rays and get the image // Cast the rays and get the image
sf::Color* pixel_colors = ray_caster.CastRays(cam_dir, cam_pos); sf::Color* pixel_colors = ray_caster.CastRays(cam_dir, cam_pos);
for (int i = 0; i < WINDOW_X * WINDOW_Y; i++) {
Curses::Tile t(sf::Vector2i(i % WINDOW_X, i / WINDOW_X));
Curses::Slot s(L'\u0045', pixel_colors[i], sf::Color::Black);
t.push_back(s);
curse.setTile(t);
}
// Cast it to an array of Uint8's // Cast it to an array of Uint8's
auto out = (sf::Uint8*)pixel_colors; auto out = (sf::Uint8*)pixel_colors;
@ -324,18 +349,15 @@ int main0() {
window_sprite.setTexture(window_texture); window_sprite.setTexture(window_texture);
window.draw(window_sprite); window.draw(window_sprite);
curse.Render();
// Give the frame counter the frame time and draw the average frame time // Give the frame counter the frame time and draw the average frame time
fps.frame(delta_time); fps.frame(delta_time);
fps.draw(&window); fps.draw(&window);
window.display();
window.draw(s);
window.display();
} }
return 0; return 0;
} }

Loading…
Cancel
Save