Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6986759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:51:00+00:00 2026-05-27T18:51:00+00:00

Suppose I already have a bitmap (an image) in Device (Video) Memory using cudaMalloc()

  • 0

Suppose I already have a bitmap (an image) in Device (Video) Memory using cudaMalloc() and cudaMemcpy().

What is the simplest way to display this bitmap directly on screen (current window)?

I am guessing there should be an alternative to GDI’s BitBlt()… but any approach (Direct3D, OpenGL or even GDI) will be fine.

Examples in CUDA SDK (OpenGL version) use Texture, that uses buffer (Pixel Buffer Object) for holding data, that is registered as CUDA-resource, that should be mapped/unmapped at each frame (glutDisplayFunc() call). And all that seems just a tiny little bit complicated and uncalled for.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:51:01+00:00Added an answer on May 27, 2026 at 6:51 pm

    Okay, will try to answer my own question. Looks like there is a simpler way for displaying a bitmap via OpenGL. Instead of using Texture + Pixel Buffer Object, we could use cudaGraphicsGLRegisterImage() that supports texture format GL_RGBA8 since CUDA Toolkit 4.0.

    For simplicity, example has no cutilSafeCall() or error checks. I have little knowledge of OpenGL and will be glad to hear recommendations.

    #include <stdio.h>
    #include <string.h>
    
    #include <GL/glew.h>
    #include <GL/freeglut.h>
    
    #include <cuda_runtime_api.h>
    #include <cuda_gl_interop.h>
    
    /* Handles OpenGL-CUDA exchange. */
    cudaGraphicsResource *cuda_texture;
    
    /* Registers (+ resizes) CUDA-Texture (aka Renderbuffer). */
    void resizeTexture(int w, int h) {
        static GLuint gl_texture = 0;
    
        /* Delete old CUDA-Texture. */
        if (gl_texture) {
            cudaGraphicsUnregisterResource(cuda_texture);
            glDeleteTextures(1, &gl_texture);
        } else glEnable(GL_TEXTURE_2D);
    
        glGenTextures(1, &gl_texture);
        glBindTexture(GL_TEXTURE_2D, gl_texture);
    
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
    
        cudaGraphicsGLRegisterImage(&cuda_texture, gl_texture, GL_TEXTURE_2D, cudaGraphicsMapFlagsWriteDiscard);
    }
    
    void updateMemDevice(cudaArray *memDevice, int w, int h) {
        struct Color {
            unsigned char r, g, b, a;
        } *memHost = new Color[w*h];
    
        memset(memHost, 128, w*h*4);
        for (int y = 0; y<h; ++y) {
            for (int x = 0; x<w; ++x) {
                Color &c = memHost[y*w + x];
                c.r = c.b = 255 * x/w;
            }
        }
        cudaMemcpyToArray(memDevice, 0, 0, memHost, w*h*4, cudaMemcpyHostToDevice);
        delete [] memHost;
    }
    
    void editTexture(int w, int h) {
        cudaGraphicsMapResources(1, &cuda_texture);
        cudaArray* memDevice;
        cudaGraphicsSubResourceGetMappedArray(&memDevice, cuda_texture, 0, 0);
        updateMemDevice(memDevice, w, h);
        cudaGraphicsUnmapResources(1, &cuda_texture);
    }
    
    
    void windowResizeFunc(int w, int h) {
        glViewport(-w, -h, w*2, h*2);
    }
    
    void displayFunc() {
        glBegin(GL_QUADS);
        glTexCoord2i(0, 0); glVertex2i(0, 0);
        glTexCoord2i(1, 0); glVertex2i(1, 0);
        glTexCoord2i(1, 1); glVertex2i(1, 1);
        glTexCoord2i(0, 1); glVertex2i(0, 1);
        glEnd();
    
        glFlush();
    }
    
    
    int main(int argc, char *argv[]) {
        /* Initialize OpenGL context. */
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_RGB);
        glutInitWindowSize(400, 300);
        glutCreateWindow("Bitmap in Device Memory");
        glutReshapeFunc(windowResizeFunc);
        glutDisplayFunc(displayFunc);
    
        glewInit();
    
        cudaGLSetGLDevice(0);
    
        int width = 5, height = 5;
        resizeTexture(width, height);
        editTexture(width, height);
    
        glutMainLoop();
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

NSMutableString *str; //I have already done the allocation etc Suppose this str has some
Suppose I was given a URL. It might already have GET parameters (e.g. http://example.com/search?q=question
Suppose I have my models set up already. class books(models.Model): title = models.CharField... ISBN
The fictional example here. Suppose I designed a class named IODevice. This class already
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
Suppose I have my project using Base SDK = 4 but set the Target
Suppose I have this sort of HTML from which I need to select text2
Basically, suppose that I have a fingerprint. I know the dimension of my image,
I can't understand one thing. Suppose I already have these coded entities: [Table(Name =
Sorry if this is a dumb question, I'm new to Lift. Suppose I already

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.