ohhmm / skrypt

Serg Kryvonos Relation Yarn Plausibility Tool
MIT License
4 stars 2 forks source link

Ports & Viewport #4

Open ohhmm opened 1 year ago

ohhmm commented 1 year ago

Mapping of pixel value variables with display/window contents for visual/GUI experience

ohhmm commented 1 year ago

Use direct video output buffer memory mapping.

Here is an example for Windows of how to get a pointer to the direct video output buffer in C++ for direct access to video memory:

// Includes
#include <d3d11.h>
#include <dxgi.h>

// Get ID3D11Device and IDXGISwapChain interfaces
ID3D11Device* device; 
IDXGISwapChain* swapChain;

// Get back buffer texture
ID3D11Texture2D* backBuffer;
swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&backBuffer);

// Get shared handle
HANDLE sharedHandle;
backBuffer->QueryInterface(__uuidof(IDXGIResource), (void**)&dxgiResource);
dxgiResource->GetSharedHandle(&sharedHandle); 

// Map to process virtual address space 
HANDLE mapHandle = CreateFileMapping(sharedHandle, NULL, PAGE_READWRITE, 0, size, NULL);
void* ptr = MapViewOfFile(mapHandle, FILE_MAP_WRITE, 0, 0, size);

// ptr now points to video memory!
// Read/write pixels directly 
uint8_t* pixel = (uint8_t*)ptr;
pixel[0] = 255; 

// Unmap when done
UnmapViewOfFile(ptr);

This gets a shared handle to the swap chain back buffer, maps it into the process address space, and then writes to the pixel data directly.

ohhmm commented 1 year ago

This feature seem best to require generator and this why it seem better to implement it in new Visual SKRYPT project.