18 #include <QApplication>
25 #define GPGPU_INFO MITK_INFO("mitk.gpgpu")
26 #define GPGPU_ERROR MITK_ERROR("mitk.gpgpu")
27 #define GPGPU_CHECKGLERR MITK_ERROR(glGetError() != GL_NO_ERROR)("mitk.gpgpu") << "GL ERROR @ "
29 #define OPERATING_TEXTURE GL_TEXTURE15
36 return GL_LUMINANCE_FLOAT32_ATI;
38 return GL_LUMINANCE_ALPHA_FLOAT32_ATI;
40 return GL_RGBA32F_ARB;
54 return GL_LUMINANCE_ALPHA;
74 return GL_UNSIGNED_BYTE;
95 glTarget = GL_TEXTURE_2D;
97 glTarget = GL_TEXTURE_3D;
106 glGenTextures(1, &handle);
107 glTextureHandle = handle;
109 glBindTexture(glTarget, glTextureHandle);
113 if (glTarget == GL_TEXTURE_2D)
115 glTexImage2D(GL_TEXTURE_2D,
126 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
127 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
129 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
130 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
134 glTexImage3D(GL_TEXTURE_3D,
146 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
147 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
148 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
150 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
151 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
156 glGenFramebuffers(1, &handle);
157 glFBOHandle = handle;
166 handle = glFBOHandle;
167 glDeleteFramebuffers(1, &handle);
170 handle = glTextureHandle;
171 glDeleteTextures(1, &handle);
177 glActiveTexture(GL_TEXTURE0 + unit);
178 glBindTexture(glTarget, glTextureHandle);
185 static GLenum buffers[5][4] = {
186 {GL_NONE, GL_NONE, GL_NONE, GL_NONE},
187 {GL_COLOR_ATTACHMENT0, GL_NONE, GL_NONE, GL_NONE},
188 {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_NONE, GL_NONE},
189 {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_NONE},
190 {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3},
193 glBindFramebuffer(GL_FRAMEBUFFER, glFBOHandle);
194 glDrawBuffers(4, buffers[1]);
195 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, glTarget, glTextureHandle, 0);
199 int error = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
202 case GL_FRAMEBUFFER_COMPLETE_EXT:
204 case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
207 case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
210 case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
213 case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:
216 case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
219 case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
222 case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
230 glViewport(0, 0, myWidth, myHeight);
238 glBindTexture(glTarget, glTextureHandle);
240 if (glTarget == GL_TEXTURE_2D)
242 glTexSubImage2D(GL_TEXTURE_2D,
254 glTexSubImage3D(GL_TEXTURE_3D,
273 glBindTexture(glTarget, glTextureHandle);
275 if (glTarget == GL_TEXTURE_2D)
286 static char stubVertexShader[] =
"void main() { gl_Position = vec4( 2*gl_Vertex.xy-1,0,1 ); }\n";
292 glHandleVertex = glCreateShader(GL_VERTEX_SHADER);
293 glHandleFragment = glCreateShader(GL_FRAGMENT_SHADER);
294 glHandleProgram = glCreateProgram();
301 glShaderSource(glHandleVertex, 1, (
const GLchar **)src,
nullptr);
306 glShaderSource(glHandleFragment, 1, (
const GLchar **)src,
nullptr);
312 glCompileShader(glHandleVertex);
314 glGetShaderiv(glHandleVertex, GL_COMPILE_STATUS, &_sv);
317 GPGPU_ERROR <<
"vertex shader compilation failed\n";
321 glCompileShader(glHandleFragment);
323 glGetShaderiv(glHandleFragment, GL_COMPILE_STATUS, &_sf);
326 GPGPU_ERROR <<
"fragment shader compilation failed\n";
330 glAttachShader(glHandleProgram, glHandleVertex);
331 glAttachShader(glHandleProgram, glHandleFragment);
332 glLinkProgram(glHandleProgram);
334 glGetProgramiv(glHandleProgram, GL_LINK_STATUS, &_sl);
343 int infologLength = 0;
344 int charsWritten = 0;
347 glGetProgramiv(glHandleProgram, GL_INFO_LOG_LENGTH, &infologLength);
349 if (infologLength > 0)
351 infoLog = (
char *)malloc(infologLength);
352 glGetProgramInfoLog(glHandleProgram, infologLength, &charsWritten, infoLog);
353 GPGPU_ERROR <<
"SHADER CREATION FAILED INFOLOG:\n" << infoLog;
361 glDeleteProgram(glHandleProgram);
362 glDeleteShader(glHandleVertex);
363 glDeleteShader(glHandleFragment);
368 glUseProgram(glHandleProgram);
372 int mitk::GPGPU::Shader::GetUniformLocation(
char *name)
374 return glGetUniformLocation(glHandleProgram, name);
379 glUniform1i(GetUniformLocation(name), i0);
389 glUniform2iv(GetUniformLocation(name), 1, i);
399 glUniform3iv(GetUniformLocation(name), 1, i);
410 glUniform4iv(GetUniformLocation(name), 1, i);
416 GLint location = GetUniformLocation(name);
417 glUniform1f(location, i0);
426 glUniform2fv(GetUniformLocation(name), 1, i);
436 glUniform3fv(GetUniformLocation(name), 1, i);
447 glUniform4fv(GetUniformLocation(name), 1, i);
452 LRESULT CALLBACK MainWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
463 return DefWindowProc(hwnd, msg, wParam, lParam);
516 HWND desktopWindow = (HWND)QApplication::topLevelWidgets().at(0)->winId();
518 windowsContext = GetDC(desktopWindow);
520 if (windowsContext == 0)
521 std::cout <<
"failed getting window device context\n";
523 static PIXELFORMATDESCRIPTOR pfd =
525 sizeof(PIXELFORMATDESCRIPTOR),
557 int iFormat = ChoosePixelFormat(windowsContext, &pfd);
558 SetPixelFormat(windowsContext, iFormat, &pfd);
560 openGLContext = wglCreateContext(windowsContext);
562 int errw = GetLastError();
564 if (openGLContext == 0)
565 std::cout <<
"failed creating openGL context " << errw <<
"\n";
569 X_display = XOpenDisplay(
nullptr);
573 GLX_drawable = QApplication::topLevelWidgets().at(0)->winId();
575 GPGPU_ERROR(!GLX_drawable) <<
"cant get toplevel widget from QT";
577 static int visAttributes[] = {GLX_RGBA, GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1, GLX_DOUBLEBUFFER, None};
579 XVisualInfo *visinfo = glXChooseVisual(X_display, 0, visAttributes);
581 GPGPU_ERROR(!visinfo) <<
"Unable to choose specified visual!";
583 openGLContext = glXCreateContext(X_display, visinfo,
nullptr,
true);
588 GPGPU_ERROR(!openGLContext) <<
"cant create GLX context";
596 int err = glewInit();
599 GPGPU_ERROR(GLEW_OK != err) <<
"glewInit() fails with " << err <<
" as text: " << glewGetErrorString(err);
601 glMatrixMode(GL_PROJECTION);
603 glOrtho(0, 1, 0, 1, -1, 1);
604 glMatrixMode(GL_MODELVIEW);
609 glDisable(GL_CULL_FACE);
610 glShadeModel(GL_SMOOTH);
611 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
613 glDisable(GL_DEPTH_TEST);
614 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
615 glHint(GL_TEXTURE_COMPRESSION_HINT, GL_NICEST);
625 wglDeleteContext(openGLContext);
630 glXDestroyContext(X_display, openGLContext);
633 XCloseDisplay(X_display);
641 wglMakeCurrent(windowsContext, openGLContext);
643 glXMakeCurrent(X_display, GLX_drawable, openGLContext);
655 glBegin(GL_TRIANGLE_STRIP);
667 glBegin(GL_TRIANGLE_STRIP);
668 glVertex2f(0, start);
670 glVertex2f(1, start);
static char stubVertexShader[]
void Upload(mitk::GPGPU::TextureFormat inputformat, const void *src)
static GLint convertTextureFormatToTypeGL(mitk::GPGPU::TextureFormat format)
Texture(mitk::GPGPU::TextureFormat format, int width, int height, int depth=0)
void SetUniform(char *name, int i0)
static GLint convertTextureFormatToInternalFormatGL(mitk::GPGPU::TextureFormat format)
void Download(mitk::GPGPU::TextureFormat inputformat, void *dst)
static GLint convertTextureFormatToFormatGL(mitk::GPGPU::TextureFormat format)
#define OPERATING_TEXTURE
void ActivateAsSource(int unit)
void ActivateAsDestination()