Shared memory tricks?

6 posts
by xernobyl » Sat Jun 23, 2012 2:27 am
I'm not used to coding graphics on devices with shared memory, so I would like to know if it's possible to get pointers to the memory allocated by GL, and access it with the CPU while the GPU does it's stuff.
Also stuff like tricking the GPU to use a VertexBuffer as a FrameBuffer in order to simulate OpenGL >=3 own transform feedback.
If we can't do that kind of stuff it's no fun to have shared memory.
Posts: 26
Joined: Tue Oct 25, 2011 2:03 pm
by jamesh » Sat Jun 23, 2012 9:56 am
I believe its technically possible, but does require translation between the GPU flat address space and that of the Arm which is via a paged memory manager. This can mean a contiguous page on the Arm is dis contiguous when viewed by the GPU and visa versa. So its possible you need to do copies to make things contiguous and usable.

That's how I understand it anyway. Dom would know more.

Whether shared memory is fun or not, that's how it works!
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Please direct all questions to the forum, I do not do support via PM.
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 17092
Joined: Sat Jul 30, 2011 7:41 pm
by RichardUK » Sat Jun 23, 2012 4:10 pm
This can be really cool if we can do it. But you would need to know how the buffers are stored and used by the GPU and possibly be able to construct texture headers by hand. This could well be protected info as it could reveal some info about the hardware.

I have used these kind of tricks to get the XBox1 to do post render image processing with a lookup table by telling it the frame buffer was really a DUDV texture. :)
User avatar
Posts: 205
Joined: Fri Jun 01, 2012 5:12 pm
by dattrax » Mon Jun 25, 2012 10:46 pm
I did some digging in the kernel tree (arch/arm/mach-bcm2708/vc_mem.c)

Maps the vc-mem into user space and goes searching for the word "Broadcom" as I figured it would be in the firmware somewhere. The next thing to do would be load a texture and see if you can find it.

Code: Select all
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <vc_mem.h>

#include <stdio.h>
#include <malloc.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>

#define PAGE_SIZE (4*1024)
#define BLOCK_SIZE (128*1024)

int main(int argc, char * argv[])
   int fd;
   char *p;
   volatile unsigned char *vc;
   unsigned long address, size, base;
   int i;

   fd = open("/dev/vc-mem", O_RDWR | O_SYNC);
   if (fd == -1)
      printf("unable to open /dev/vc-mem ...\n");
      return 1;
   ioctl(fd, VC_MEM_IOC_MEM_PHYS_ADDR, &address);
   printf("VC_MEM_IOC_MEM_PHYS_ADDR = %p\n", (char *)address);

   ioctl(fd, VC_MEM_IOC_MEM_SIZE, &size);
   printf("VC_MEM_IOC_MEM_SIZE = %d\n", size);

   ioctl(fd, VC_MEM_IOC_MEM_BASE, &base);
   printf("VC_MEM_IOC_MEM_BASE = %p\n", (char *)base);

   vc = (unsigned char *)mmap(
   if (vc == (unsigned char *)-1)
      printf("mmap failed %s\n", strerror(errno));
      return -1;

   printf("vc = %p\n", vc);

   for (i == 0; i < size; i++)
      if (strcmp((char *)&vc[i], "Broadcom") == 0)
         printf("found Broadcom @ %d\n", i);
         printf("%2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x\n",
            vc[i], vc[i+1], vc[i+2], vc[i+3], vc[i+4], vc[i+5], vc[i+6], vc[i+7]);


Posts: 52
Joined: Sat Dec 24, 2011 5:09 pm
by dani2810 » Wed Apr 23, 2014 8:54 pm
Have there been any progresses in this topic?

It would be interesting to access to OpenGL results directly from OpenCV, for example.
Posts: 3
Joined: Wed Mar 19, 2014 8:04 pm
by lask1 » Wed Apr 30, 2014 10:47 pm
dani2810 wrote:Have there been any progresses in this topic?

It would be interesting to access to OpenGL results directly from OpenCV, for example.

Not sure what happened to this... I would find it very useful to be able to access the gpu ram anywhere! I might take a look into this, If I get the chance to try I will keep you updated.

Posts: 17
Joined: Thu Jul 18, 2013 8:48 pm