How to efficiently simulate a video display

69 views
Skip to first unread message

wanderley...@gmail.com

unread,
Oct 22, 2019, 11:54:58 AM10/22/19
to Racket-Users List
Hi folks,

I decided to write a computer simulator (16-bit machine as the one
built in nand2tetris course) in Racket. The computer maps the screen
to a memory space where bits represent the pixel colors (black and
white).

Right now, I am generating the screen image by converting the bits to
a vector of argb pixels and then converting it to bitmap using
argb-pixels->pict and then pict-bitmap. The screen updates 10 frames
per second but this conversion bits->bitmap is a bottleneck even in
this low rate of update.

How would be a better approach for this process? I can imagine how an
incremental approach can be faster by only changing the necessary
parts of the image, but my idea of representing the screen as a
quadtree sounds too complex to be the right path.

Thanks,
Wanderley Guimarães

Jay McCarthy

unread,
Oct 22, 2019, 11:58:07 AM10/22/19
to wanderley...@gmail.com, Racket-Users List
I would use a really simple OpenGL draw call that just draws a
rectangle and update the texture contents with the screen bits.

--
Jay McCarthy
Associate Professor @ CS @ UMass Lowell
http://jeapostrophe.github.io
Vincit qui se vincit.
> --
> You received this message because you are subscribed to the Google Groups "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/CAAmHZocH9NaV2hwvQsi%2B_dXZy%3DT4nkPd-HmwhzTGKABh89kntg%40mail.gmail.com.

wanderley...@gmail.com

unread,
Oct 22, 2019, 1:33:13 PM10/22/19
to Jay McCarthy, Racket-Users List
Thanks for the quick response. Can you elaborate a bit more? I've
never worked with OpenGL before, so I don't know exactly how to start
with that.

I found that I can use the mapped screen directly to generate a
monochrome bitbmap.

(make-monochrome-bitmap 512 256
(apply bytes (map (lambda (x) (modulo x 255)) (range (* 512 256)))))

With that I can get rid of the transformation bits->argb-bytes->pict->bitmap.

I saw gl-context but I didn't figure out how to use it the image that
I send to big-bang.

Thanks,
Wanderley
--
Abraço,
Wanderley Guimarães

Jay McCarthy

unread,
Oct 28, 2019, 4:50:21 PM10/28/19
to wanderley...@gmail.com, Racket-Users List
You really do not want to be creating bitmap structures or converting
anything like that. You want to directly manipulate a byte array to
store the image and then upload it to the GPU with glTexImage2D every
frame. You then want to just draw a fullscreen quad. It is practically
the simplest shader you can think of. In mode-lambda, my `std` shader
is what you want:

https://github.com/jeapostrophe/mode-lambda/blob/master/mode-lambda/backend/gl/std.fragment.glsl
https://github.com/jeapostrophe/mode-lambda/blob/master/mode-lambda/backend/gl/std.vertex.glsl

And the rendering code would be very simple, like:

https://github.com/jeapostrophe/mode-lambda/blob/master/mode-lambda/backend/gl.rkt#L253-L288

If you Google, "OpenGL draw fullscreen quad", you'll find a bunch of
tutorials on doing this.

Jay

--
Jay McCarthy
Associate Professor @ CS @ UMass Lowell
http://jeapostrophe.github.io
Vincit qui se vincit.

Reply all
Reply to author
Forward
0 new messages