Skip to content

Instantly share code, notes, and snippets.

@ChristophHaag
Created November 26, 2018 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChristophHaag/b144c5837ff6b90f38f391d761d68f38 to your computer and use it in GitHub Desktop.
Save ChristophHaag/b144c5837ff6b90f38f391d761d68f38 to your computer and use it in GitHub Desktop.
// g++ submitTextureTime.cpp -o submitTextureTime $(pkg-config --cflags --libs gl sdl2) -lopenvr_api
#define GL_GLEXT_PROTOTYPES 1
#define GL3_PROTOTYPES 1
#include <GL/gl.h>
#include <chrono>
#include <iostream>
#include <SDL2/SDL.h>
#include <openvr.h>
#include <unistd.h>
#define Sleep(x) usleep((x)*1000)
using namespace vr;
void check_error(int line, EVRInitError error) { if (error != 0) printf("%d: error %s\n", line, VR_GetVRInitErrorAsSymbol(error)); }
int main(int argc, char **argv)
{
SDL_Window *window = SDL_CreateWindow("SDL2/OpenGL Demo", 0, 0, 640, 480, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);
SDL_GLContext glcontext = SDL_GL_CreateContext(window);
SDL_GL_SetSwapInterval(0);
EVRInitError error;
VR_Init(&error, vr::VRApplication_Overlay);
check_error(__LINE__, error);
VROverlayHandle_t handle;
VROverlay()->CreateOverlay ("textoverlay", "testoverlay", &handle);
VROverlay()->SetOverlayWidthInMeters(handle, 0.1);
VROverlay()->ShowOverlay(handle);
uint32_t *fakeImage = (uint32_t*) malloc(sizeof(uint32_t) * 4096 * 4096 * 4);
int loops = 10;
for (int createNewTextures = 0; createNewTextures <= 1; createNewTextures++) {
GLuint texture;
if (!createNewTextures)
glGenTextures(1, &texture);
double totalSubmitDuration = 0;
for (int i = 0; i < loops; i++) {
const int width = 32;
const int height = 32;
glClearColor(i / (float)loops, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
if (createNewTextures) {
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)fakeImage);
Texture_t tex;
tex.handle = (void*)(uintptr_t) texture;
tex.eColorSpace = ColorSpace_Linear;
tex.eType = TextureType_OpenGL;
auto t_start = std::chrono::high_resolution_clock::now();
VROverlay()->SetOverlayTexture(handle, &tex);
auto t_end = std::chrono::high_resolution_clock::now();
double submitDuration = std::chrono::duration<double, std::milli>(t_end-t_start).count();
totalSubmitDuration += submitDuration;
std::cout << "Submit time: " << submitDuration << "ms" << std::endl;
if (createNewTextures)
glDeleteTextures(1, &texture);
SDL_GL_SwapWindow(window);
Sleep(90./1); // doesn't matter that it's not actually 90 fps
}
if (!createNewTextures)
glDeleteTextures(1, &texture);
std::cout << "Avg texture submit time " << (createNewTextures ? "when creating new GL textures every time" : "when reusing GL textures") << " : " << totalSubmitDuration / (float)loops << std::endl;
}
free (fakeImage);
SDL_GL_DeleteContext(glcontext);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment