Skip to content

Instantly share code, notes, and snippets.

@rrika
Created December 14, 2018 00:08
Show Gist options
  • Save rrika/ae0dc8fa1715421ceab92623b60e7781 to your computer and use it in GitHub Desktop.
Save rrika/ae0dc8fa1715421ceab92623b60e7781 to your computer and use it in GitHub Desktop.
Mesa hooking
/*
MESA=<path to git clone of git://anongit.freedesktop.org/mesa/mesa>
gcc
-I$MESA/include/
-I$MESA/src/
-I$MESA/src/mesa
-I$MESA/src/mapi
-I$MESA/src/gallium/include
-lglapi
-lOSMesa
-g -o test test.c
*/
#include <stdio.h>
#include "GL/gl.h"
#include "GL/osmesa.h"
#define HAVE_PTHREAD
#define HAVE_TIMESPEC_GET
#define GLX_USE_TLS
#include "mesa/main/mtypes.h"
#include "mapi/glapi/glapi.h"
#include "gallium/include/state_tracker/st_api.h"
#include "gallium/include/pipe/p_context.h"
#include "gallium/include/pipe/p_state.h"
static void (*original_draw_vbo)(struct pipe_context *pipe, const struct pipe_draw_info *info);
void override_draw_vbo(struct pipe_context *pipe, const struct pipe_draw_info *info) {
printf("struct pipe_draw_info *info = {\n");
printf(" ...\n");
printf(" .has_user_indices = %d,\n", info->has_user_indices);
printf(" ...\n");
printf(" .start = %d,\n", info->start);
printf(" .count = %d,\n", info->count);
printf(" ...\n");
printf("};\n");
printf("\n");
original_draw_vbo(pipe, info);
}
int main(int argc, char const *argv[]) {
void *buffer = malloc(64*64*4);
OSMesaContext osmesa_context = OSMesaCreateContext(OSMESA_RGBA, 0);
OSMesaMakeCurrent(osmesa_context, buffer, 0x1401 /*GL_UNSIGNED_BYTE*/, 64, 64);
char const *renderer;
struct gl_context *glapi_context = _glapi_get_context();
// glGetString loaded through libGL will return NULL regardless
// renderer = glGetString(0x1F01); // GL_RENDERER
// printf("glGetString(GL_RENDERER) = %s\n", renderer);
#define GL(X) ((typeof(&gl##X))OSMesaGetProcAddress("gl" #X))
renderer = GL(GetString)(0x1F01);
printf("OSMesaGetProcAddress(\"glGetString\")(GL_RENDERER) = %s\n", renderer);
// renderer = glapi_context->Driver.GetString(glapi_context, 0x1F01);
// printf("ctx->Driver->GetString(drv, GL_RENDERER) = %s\n", renderer);
// you can't get the st through glapi_context->st sadly, but there's another way
struct st_context_iface *st = (struct st_context_iface *)*(void**)osmesa_context;
struct pipe_context *pipe = st->pipe;
original_draw_vbo = pipe->draw_vbo;
pipe->draw_vbo = &override_draw_vbo;
GL(Begin)(GL_TRIANGLES);
GL(Vertex3f)(0.0f, 0.0f, 0.0f);
GL(Vertex3f)(0.0f, 1.0f, 0.0f);
GL(Vertex3f)(1.0f, 0.0f, 0.0f);
GL(End)();
GL(Finish)();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment