Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Most efficient way of rendering pixel by pixel?  (Read 2727 times)

0 Members and 1 Guest are viewing this topic.

RaySteak

  • Newbie
  • *
  • Posts: 3
    • View Profile
Most efficient way of rendering pixel by pixel?
« on: October 31, 2018, 05:44:26 pm »
Hi,
I'm making a simple raycasting engine and I'd like to change the color of each pixel individually but the problem is that I haven't been able to find a method of doing it efficiently. I've tried using an Uint8 array:
 sf::Uint8 *canvas= new sf::Uint8[SCREEN_H*SCREEN_W*4];  
that I pass to an sf::Image after it's ready to be displayed. The problem is that accessing arrays this big seems to be quite slow which results in about 40 fps at 1280*720. Are there any better methods of drawing a pixel by pixel image to the window?
Thanks!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
Re: Most efficient way of rendering pixel by pixel?
« Reply #1 on: October 31, 2018, 06:00:09 pm »
The best way is to implement your logic in a shader, so you can execute it directly on the GPU.

Otherwise it won't be very format either way, but the best option is to use the sf::Texture::update() directly with your pixel array.
The access of the array is not the issue, but the bandwidth between CPU and GPU.

1280*720*4*60 is around 200MiB/s
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

RaySteak

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Most efficient way of rendering pixel by pixel?
« Reply #2 on: October 31, 2018, 06:24:41 pm »
Thanks! with Texture.update() i get about 10-15 more fps.

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Most efficient way of rendering pixel by pixel?
« Reply #3 on: October 31, 2018, 07:47:22 pm »
Did you profile your program to see what takes most time and are you running in release?

I first thought to recommend PBO and GL_PIXEL_UNPACK_BUFFER but it's a sort of advanced topic (it'd require you to use OpenGL functions yourself, SFML doesn't provide it) and might help just a little and/or require you to restructure your app a bit: http://www.songho.ca/opengl/gl_pbo.html#map

Quote
1280*720*4*60 is around 200MiB/s
But this isn't that much these days if you think about it, especially for a RAM/CPU to GPU bandwidth.

On my relatively new 'gaming' MSI laptop in the test program from the link above I can get 10-20x that on my integrated Intel and 20-30x that on my GTX 1050 (which is on PCIe v3 x16 according to HWiNFO64) depending on whether or not I use PBOs.

A single PCIe v1 lane did 250 MB/s max and that's properly ancient by today's standard. Unless there are other data transfers going on or I'm missing something this speed (~200 MB/s) is completely put to shame by everything except 100 Megabit/1 Gigabit Ethernet, HDDs and USB 2.0. It's below USB 3.0, 3.1, all relatively new SSDs no matter the protocol and form factor, Intel Optane memory, RAM disks, 10 Gigabit Ethernet, etc.
« Last Edit: October 31, 2018, 07:50:10 pm by FRex »
Back to C++ gamedev with SFML in May 2023

RaySteak

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Most efficient way of rendering pixel by pixel?
« Reply #4 on: November 01, 2018, 01:50:20 pm »
It was a caching issue apparently. Storing the pixels in the array row wise instead of line wise seems to have fixed it.