Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
C
cuda_lab
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Gaurav Kukreja
cuda_lab
Commits
7b9d580a
Commit
7b9d580a
authored
Mar 23, 2014
by
Gaurav Kukreja
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
tried using pbo and texture as per Nvidia example, still not working
Signed-off-by:
Gaurav Kukreja
<
gmkukreja@gmail.com
>
parent
639b6601
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
100 additions
and
21 deletions
+100
-21
main.cu
miklos/project/main.cu
+100
-21
No files found.
miklos/project/main.cu
View file @
7b9d580a
...
@@ -37,7 +37,7 @@
...
@@ -37,7 +37,7 @@
using namespace std;
using namespace std;
// uncomment to use the camera
// uncomment to use the camera
//
#define CAMERA
#define CAMERA
template<typename T>
template<typename T>
__device__ __host__ T min(T a, T b)
__device__ __host__ T min(T a, T b)
...
@@ -100,7 +100,7 @@ __global__ void norm_grad(float *U, float *vx, float *vy, int w, int h)
...
@@ -100,7 +100,7 @@ __global__ void norm_grad(float *U, float *vx, float *vy, int w, int h)
* @param tau update coefficient
* @param tau update coefficient
*/
*/
#ifdef CAMERA
#ifdef CAMERA
__global__ void update(float
4
*output, float *U, float *F, float *vx, float *vy,
__global__ void update(float *output, float *U, float *F, float *vx, float *vy,
int w, int h, float lambda, float tau)
int w, int h, float lambda, float tau)
#else
#else
__global__ void update(float *U, float *F, float *vx, float *vy,
__global__ void update(float *U, float *F, float *vx, float *vy,
...
@@ -128,9 +128,10 @@ __global__ void update(float *U, float *F, float *vx, float *vy,
...
@@ -128,9 +128,10 @@ __global__ void update(float *U, float *F, float *vx, float *vy,
U[i] += tau * (lambda * d + div_v);
U[i] += tau * (lambda * d + div_v);
#ifdef CAMERA
#ifdef CAMERA
float u = x / (float)w;
// float u = x / (float)w;
float v = y / (float)h;
// float v = y / (float)h;
output[i] = make_float4(u, U[i], v, 1.0f);
// output[i] = make_float4(u, v, U[i], 1.0f);
output[i] = U[i];
#else
#else
#endif
#endif
}
}
...
@@ -146,6 +147,34 @@ inline dim3 make_grid(dim3 whole, dim3 block)
...
@@ -146,6 +147,34 @@ inline dim3 make_grid(dim3 whole, dim3 block)
}
}
// shader for displaying floating-point texture
static const char *shader_code =
"!!ARBfp1.0\n"
"TEX result.color, fragment.texcoord, texture[0], 2D; \n"
"END";
GLuint compileASMShader(GLenum program_type, const char *code)
{
GLuint program_id;
glGenProgramsARB(1, &program_id);
glBindProgramARB(program_type, program_id);
glProgramStringARB(program_type, GL_PROGRAM_FORMAT_ASCII_ARB, (GLsizei) strlen(code), (GLubyte *) code);
GLint error_pos;
glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &error_pos);
if (error_pos != -1)
{
const GLubyte *error_string;
error_string = glGetString(GL_PROGRAM_ERROR_STRING_ARB);
cout << "Program error at position: " << (int)error_pos << endl << error_string << endl;
return 0;
}
return program_id;
}
int main(int argc, char **argv)
int main(int argc, char **argv)
{
{
// Before the GPU can process your kernels, a so called "CUDA context" must be initialized
// Before the GPU can process your kernels, a so called "CUDA context" must be initialized
...
@@ -274,24 +303,50 @@ int main(int argc, char **argv)
...
@@ -274,24 +303,50 @@ int main(int argc, char **argv)
GLuint outputVBO;
GLuint outputVBO;
struct cudaGraphicsResource* outputVBO_CUDA;
struct cudaGraphicsResource* outputVBO_CUDA;
GLuint texid; // Texture
GLuint shader;
// Explicitly set device 0
// Explicitly set device 0
cudaGLSetGLDevice(0);
cudaGLSetGLDevice(0);
//
Create buffer object and register it with CUDA
//
create pixel buffer object
glGenBuffers(1, &outputVBO);
glGenBuffers
ARB
(1, &outputVBO);
glBindBuffer
(GL_ARRAY_BUFFER, outputVBO);
glBindBuffer
ARB(GL_PIXEL_UNPACK_BUFFER_ARB, outputVBO);
unsigned int size = w * h * 4 * sizeof(float
);
glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, w*h*sizeof(GLfloat), 0, GL_STREAM_DRAW_ARB
);
glBufferData(GL_ARRAY_BUFFER, size, 0, GL_DYNAMIC_DRAW);
glBindBuffer
(GL_ARRAY_BUFFER
, 0);
glBindBuffer
ARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
cudaGraphicsGLRegisterBuffer(&outputVBO_CUDA,
outputVBO,
cudaGraphicsGLRegisterBuffer(&outputVBO_CUDA,
outputVBO,
cudaGraphicsMapFlagsWriteDiscard);
cudaGraphicsMapFlagsWriteDiscard);
// // Create buffer object and register it with CUDA
// glGenBuffers(1, &outputVBO);
// glBindBuffer(GL_ARRAY_BUFFER, outputVBO);
// unsigned int size = w * h * 4 * sizeof(float);
// glBufferData(GL_ARRAY_BUFFER, size, 0, GL_DYNAMIC_DRAW);
// glBindBuffer(GL_ARRAY_BUFFER, 0);
// cudaGraphicsGLRegisterBuffer(&outputVBO_CUDA,
// outputVBO,
// cudaGraphicsMapFlagsWriteDiscard);
// load shader program
shader = compileASMShader(GL_FRAGMENT_PROGRAM_ARB, shader_code);
// create texture for display
glGenTextures(1, &texid);
glBindTexture(GL_TEXTURE_2D, texid);
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, w, h, 0, GL_LUMINANCE, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, 0);
// Read a camera image frame every 30 milliseconds:
// Read a camera image frame every 30 milliseconds:
// cv::waitKey(30) waits 30 milliseconds for a keyboard input,
// cv::waitKey(30) waits 30 milliseconds for a keyboard input,
// returns a value <0 if no key is pressed during this time, returns immediately with a value >=0 if a key is pressed
// returns a value <0 if no key is pressed during this time, returns immediately with a value >=0 if a key is pressed
while (
cv::waitKey(30) < 0 ||
!glfwWindowShouldClose(window))
while (!glfwWindowShouldClose(window))
{
{
// Get camera image
// Get camera image
camera >> mIn;
camera >> mIn;
...
@@ -301,7 +356,7 @@ int main(int argc, char **argv)
...
@@ -301,7 +356,7 @@ int main(int argc, char **argv)
// convert range of each channel to [0,1] (opencv default is [0,255])
// convert range of each channel to [0,1] (opencv default is [0,255])
mIn /= 255.f;
mIn /= 255.f;
float
4
* d_output;
float* d_output;
cudaGraphicsMapResources(1, &outputVBO_CUDA, 0);
cudaGraphicsMapResources(1, &outputVBO_CUDA, 0);
size_t num_bytes;
size_t num_bytes;
cudaGraphicsResourceGetMappedPointer((void**)&d_output,
cudaGraphicsResourceGetMappedPointer((void**)&d_output,
...
@@ -353,12 +408,36 @@ int main(int argc, char **argv)
...
@@ -353,12 +408,36 @@ int main(int argc, char **argv)
#ifdef CAMERA
#ifdef CAMERA
// Render from buffer object
// Render from buffer object
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// OpenGL display code path
glBindBuffer(GL_ARRAY_BUFFER, outputVBO);
{
glVertexPointer(4, GL_FLOAT, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glEnableClientState(GL_VERTEX_ARRAY);
glDrawArrays(GL_POINTS, 0, w * h);
// load texture from pbo
glDisableClientState(GL_VERTEX_ARRAY);
glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, outputVBO);
glBindTexture(GL_TEXTURE_2D, texid);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, GL_LUMINANCE, GL_FLOAT, 0);
glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
// fragment program is required to display floating point texture
glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader);
glEnable(GL_FRAGMENT_PROGRAM_ARB);
glDisable(GL_DEPTH_TEST);
glBegin(GL_QUADS);
{
glTexCoord2f(0.0f, 0.0f);
glVertex2f(0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex2f(1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex2f(1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex2f(0.0f, 1.0f);
}
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_FRAGMENT_PROGRAM_ARB);
}
#else
#else
// show input image
// show input image
showImage("Input", mIn, 100, 100); // show at position (x_from_left=100,y_from_above=100)
showImage("Input", mIn, 100, 100); // show at position (x_from_left=100,y_from_above=100)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment