2004-06-12 16:28:34 -04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2002-2004 LWJGL Project
|
2003-10-22 07:45:51 -04:00
|
|
|
* All rights reserved.
|
2004-06-12 16:28:34 -04:00
|
|
|
*
|
2003-10-22 07:45:51 -04:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
2004-06-12 16:28:34 -04:00
|
|
|
* modification, are permitted provided that the following conditions are
|
2003-10-22 07:45:51 -04:00
|
|
|
* met:
|
2004-06-12 16:28:34 -04:00
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
2003-10-22 07:45:51 -04:00
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
2004-06-12 16:28:34 -04:00
|
|
|
* * Neither the name of 'LWJGL' nor the names of
|
|
|
|
* its contributors may be used to endorse or promote products derived
|
2003-10-22 07:45:51 -04:00
|
|
|
* from this software without specific prior written permission.
|
2004-06-12 16:28:34 -04:00
|
|
|
*
|
2003-10-22 07:45:51 -04:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
2004-06-12 16:28:34 -04:00
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
2003-10-22 07:45:51 -04:00
|
|
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
2004-06-12 16:28:34 -04:00
|
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
2003-10-22 07:45:51 -04:00
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
2004-03-01 20:59:32 -05:00
|
|
|
|
2003-10-22 07:45:51 -04:00
|
|
|
/**
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Win32 Pbuffer.
|
|
|
|
*
|
|
|
|
* @author elias_naur <elias_naur@users.sourceforge.net>
|
|
|
|
* @version $Revision$
|
|
|
|
*/
|
|
|
|
|
2003-05-20 08:20:13 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include "org_lwjgl_opengl_Pbuffer.h"
|
|
|
|
#include "Window.h"
|
2004-02-15 14:24:03 -05:00
|
|
|
|
2003-05-20 08:20:13 -04:00
|
|
|
#include "extgl.h"
|
2004-07-03 10:39:29 -04:00
|
|
|
#include "extgl_wgl.h"
|
2004-02-15 14:24:03 -05:00
|
|
|
|
2003-10-11 12:29:40 -04:00
|
|
|
#include "common_tools.h"
|
2003-05-20 08:20:13 -04:00
|
|
|
|
|
|
|
typedef struct _PbufferInfo {
|
|
|
|
HGLRC Pbuffer_context;
|
|
|
|
HPBUFFERARB Pbuffer;
|
|
|
|
HDC Pbuffer_dc;
|
|
|
|
} PbufferInfo;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_lwjgl_opengl_Pbuffer
|
|
|
|
* Method: isPbufferSupported
|
|
|
|
* Signature: ()Z
|
|
|
|
*/
|
|
|
|
JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_Pbuffer_getPbufferCaps
|
|
|
|
(JNIEnv *env, jclass clazz)
|
|
|
|
{
|
2004-03-01 20:59:32 -05:00
|
|
|
int caps = 0;
|
|
|
|
if ( extgl_Extensions.WGL_ARB_pixel_format && extgl_Extensions.WGL_ARB_pbuffer )
|
|
|
|
caps |= org_lwjgl_opengl_Pbuffer_PBUFFER_SUPPORTED;
|
|
|
|
|
|
|
|
if ( extgl_Extensions.WGL_ARB_render_texture )
|
|
|
|
caps |= org_lwjgl_opengl_Pbuffer_RENDER_TEXTURE_SUPPORTED;
|
|
|
|
|
|
|
|
if ( extgl_Extensions.WGL_NV_render_texture_rectangle )
|
|
|
|
caps |= org_lwjgl_opengl_Pbuffer_RENDER_TEXTURE_RECTANGLE_SUPPORTED;
|
|
|
|
|
|
|
|
if ( extgl_Extensions.WGL_NV_render_depth_texture )
|
|
|
|
caps |= org_lwjgl_opengl_Pbuffer_RENDER_DEPTH_TEXTURE_SUPPORTED;
|
|
|
|
|
|
|
|
return caps;
|
2003-05-20 08:20:13 -04:00
|
|
|
}
|
|
|
|
|
2004-07-24 17:19:08 -04:00
|
|
|
static HPBUFFERARB createPbuffer(JNIEnv *env, int width, int height, jobject pixel_format, jobject pixelFormatCaps, const int *pBufferAttribs_ptr) {
|
2004-07-24 18:06:43 -04:00
|
|
|
HWND dummy_hwnd = createWindow(1, 1, false, false);
|
2004-07-06 03:50:33 -04:00
|
|
|
if (dummy_hwnd == NULL) {
|
2004-07-24 18:06:43 -04:00
|
|
|
throwException(env, "Could not create dummy window");
|
2004-07-24 17:19:08 -04:00
|
|
|
return NULL;
|
2004-07-06 03:50:33 -04:00
|
|
|
}
|
|
|
|
HDC dummy_hdc = GetDC(dummy_hwnd);
|
|
|
|
int iPixelFormat = findPixelFormat(env, dummy_hdc, pixel_format);
|
2004-07-03 06:16:28 -04:00
|
|
|
if (iPixelFormat == -1) {
|
2004-07-24 17:19:08 -04:00
|
|
|
return NULL;
|
2004-07-06 03:50:33 -04:00
|
|
|
}
|
2004-07-24 18:06:43 -04:00
|
|
|
if (!applyPixelFormat(dummy_hdc, iPixelFormat)) {
|
2004-07-06 03:50:33 -04:00
|
|
|
closeWindow(dummy_hwnd, dummy_hdc);
|
2004-07-24 18:06:43 -04:00
|
|
|
throwException(env, "Could not apply pixel format to window");
|
2004-07-24 17:19:08 -04:00
|
|
|
return NULL;
|
2004-07-06 03:50:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
HGLRC dummy_hglrc = wglCreateContext(dummy_hdc);
|
|
|
|
if (dummy_hglrc == NULL) {
|
|
|
|
closeWindow(dummy_hwnd, dummy_hdc);
|
|
|
|
throwException(env, "Failed to create OpenGL rendering context");
|
2004-07-24 17:19:08 -04:00
|
|
|
return NULL;
|
2004-07-06 03:50:33 -04:00
|
|
|
}
|
|
|
|
BOOL result = wglMakeCurrent(dummy_hdc, dummy_hglrc);
|
|
|
|
if (!result) {
|
|
|
|
wglDeleteContext(dummy_hglrc);
|
|
|
|
closeWindow(dummy_hwnd, dummy_hdc);
|
|
|
|
throwException(env, "Could not bind context to dummy window");
|
2004-07-24 17:19:08 -04:00
|
|
|
return NULL;
|
2004-07-06 03:50:33 -04:00
|
|
|
}
|
|
|
|
extgl_InitWGL(env);
|
2004-02-15 14:24:03 -05:00
|
|
|
|
2004-07-24 17:19:08 -04:00
|
|
|
iPixelFormat = findPixelFormatARB(env, dummy_hdc, pixel_format, pixelFormatCaps, false, false, true, false);
|
2004-07-26 10:27:34 -04:00
|
|
|
if (iPixelFormat == -1)
|
|
|
|
iPixelFormat = findPixelFormatARB(env, dummy_hdc, pixel_format, pixelFormatCaps, false, false, true, true);
|
2004-07-24 17:19:08 -04:00
|
|
|
wglDeleteContext(dummy_hglrc);
|
2004-07-06 03:50:33 -04:00
|
|
|
if (iPixelFormat == -1) {
|
|
|
|
closeWindow(dummy_hwnd, dummy_hdc);
|
2004-07-24 17:19:08 -04:00
|
|
|
throwException(env, "Could not find suitable pixel format.");
|
|
|
|
return NULL;
|
2003-05-20 08:20:13 -04:00
|
|
|
}
|
2004-07-24 17:19:08 -04:00
|
|
|
HPBUFFERARB Pbuffer = wglCreatePbufferARB(dummy_hdc, iPixelFormat, width, height, pBufferAttribs_ptr);
|
|
|
|
closeWindow(dummy_hwnd, dummy_hdc);
|
|
|
|
return Pbuffer;
|
|
|
|
}
|
2004-03-01 20:59:32 -05:00
|
|
|
|
2004-07-24 17:19:08 -04:00
|
|
|
static HGLRC createPbufferContext(JNIEnv *env, HDC Pbuffer_dc) {
|
|
|
|
HGLRC Pbuffer_context = wglCreateContext(Pbuffer_dc);
|
|
|
|
if (Pbuffer_context == NULL) {
|
|
|
|
throwException(env, "Failed to create Pbuffer rendering context");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (getCurrentContext() != NULL && !wglShareLists(getCurrentContext(), Pbuffer_context)) {
|
|
|
|
wglDeleteContext(Pbuffer_context);
|
|
|
|
throwException(env, "Could not share buffer context.");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return Pbuffer_context;
|
|
|
|
}
|
2004-03-01 20:59:32 -05:00
|
|
|
|
2004-07-25 10:51:28 -04:00
|
|
|
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Pbuffer_nCreate
|
2004-08-02 10:47:52 -04:00
|
|
|
(JNIEnv *env, jclass clazz, jobject buffer_handle,
|
2004-07-24 17:19:08 -04:00
|
|
|
jint width, jint height, jobject pixel_format,
|
|
|
|
jobject pixelFormatCaps, jobject pBufferAttribs)
|
|
|
|
{
|
2004-07-25 10:51:28 -04:00
|
|
|
if (env->GetDirectBufferCapacity(buffer_handle) < sizeof(PbufferInfo)) {
|
|
|
|
throwException(env, "Buffer handle not large enough");
|
|
|
|
return;
|
|
|
|
}
|
2004-07-24 17:19:08 -04:00
|
|
|
HPBUFFERARB Pbuffer;
|
|
|
|
const int *pBufferAttribs_ptr;
|
2004-03-01 20:59:32 -05:00
|
|
|
if ( pBufferAttribs != NULL ) {
|
2004-07-24 17:19:08 -04:00
|
|
|
pBufferAttribs_ptr = (const int *)env->GetDirectBufferAddress(pBufferAttribs);
|
2004-03-01 20:59:32 -05:00
|
|
|
} else {
|
2004-07-24 17:19:08 -04:00
|
|
|
pBufferAttribs_ptr = NULL;
|
|
|
|
}
|
2004-08-02 10:47:52 -04:00
|
|
|
Pbuffer = createPbuffer(env, width, height, pixel_format, pixelFormatCaps, pBufferAttribs_ptr);
|
2004-03-01 20:59:32 -05:00
|
|
|
|
2003-05-20 08:20:13 -04:00
|
|
|
if (Pbuffer == NULL) {
|
|
|
|
throwException(env, "Could not create Pbuffer.");
|
2004-07-25 10:51:28 -04:00
|
|
|
return;
|
2003-05-20 08:20:13 -04:00
|
|
|
}
|
2004-03-01 20:59:32 -05:00
|
|
|
|
2003-05-20 08:20:13 -04:00
|
|
|
HDC Pbuffer_dc = wglGetPbufferDCARB(Pbuffer);
|
|
|
|
if (Pbuffer_dc == NULL) {
|
|
|
|
wglDestroyPbufferARB(Pbuffer);
|
|
|
|
throwException(env, "Could not get Pbuffer dc.");
|
2004-07-25 10:51:28 -04:00
|
|
|
return;
|
2003-05-20 08:20:13 -04:00
|
|
|
}
|
2004-07-24 17:19:08 -04:00
|
|
|
HGLRC Pbuffer_context;
|
2004-08-02 10:47:52 -04:00
|
|
|
Pbuffer_context = createPbufferContext(env, Pbuffer_dc);
|
|
|
|
if (Pbuffer_context == NULL) {
|
|
|
|
wglReleasePbufferDCARB(Pbuffer, Pbuffer_dc);
|
|
|
|
wglDestroyPbufferARB(Pbuffer);
|
|
|
|
return;
|
2003-05-20 08:20:13 -04:00
|
|
|
}
|
2004-07-25 10:51:28 -04:00
|
|
|
PbufferInfo *Pbuffer_info = (PbufferInfo *)env->GetDirectBufferAddress(buffer_handle);
|
2003-05-20 08:20:13 -04:00
|
|
|
Pbuffer_info->Pbuffer = Pbuffer;
|
|
|
|
Pbuffer_info->Pbuffer_context = Pbuffer_context;
|
|
|
|
Pbuffer_info->Pbuffer_dc = Pbuffer_dc;
|
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_Pbuffer_nIsBufferLost
|
2004-07-25 10:51:28 -04:00
|
|
|
(JNIEnv *env, jclass clazz, jobject buffer_handle)
|
2003-05-20 08:20:13 -04:00
|
|
|
{
|
2004-07-25 10:51:28 -04:00
|
|
|
PbufferInfo *Pbuffer_info = (PbufferInfo *)env->GetDirectBufferAddress(buffer_handle);
|
2003-05-20 08:20:13 -04:00
|
|
|
BOOL buffer_lost;
|
|
|
|
wglQueryPbufferARB(Pbuffer_info->Pbuffer, WGL_PBUFFER_LOST_ARB, &buffer_lost);
|
|
|
|
return buffer_lost ? JNI_TRUE : JNI_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Pbuffer_nMakeCurrent
|
2004-07-25 10:51:28 -04:00
|
|
|
(JNIEnv *env, jclass clazz, jobject buffer_handle)
|
2003-05-20 08:20:13 -04:00
|
|
|
{
|
2004-07-25 10:51:28 -04:00
|
|
|
PbufferInfo *Pbuffer_info = (PbufferInfo *)env->GetDirectBufferAddress(buffer_handle);
|
|
|
|
// PbufferInfo *Pbuffer_info = (PbufferInfo *)handle;
|
2004-07-04 04:39:09 -04:00
|
|
|
if (!wglMakeCurrent(Pbuffer_info->Pbuffer_dc, Pbuffer_info->Pbuffer_context))
|
|
|
|
throwException(env, "Could not make pbuffer context current");
|
2003-05-20 08:20:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Pbuffer_nDestroy
|
2004-07-25 10:51:28 -04:00
|
|
|
(JNIEnv *env, jclass clazz, jobject buffer_handle)
|
2003-05-20 08:20:13 -04:00
|
|
|
{
|
2004-07-25 10:51:28 -04:00
|
|
|
PbufferInfo *Pbuffer_info = (PbufferInfo *)env->GetDirectBufferAddress(buffer_handle);
|
2004-08-02 10:47:52 -04:00
|
|
|
wglDeleteContext(Pbuffer_info->Pbuffer_context);
|
2003-05-20 08:20:13 -04:00
|
|
|
wglReleasePbufferDCARB(Pbuffer_info->Pbuffer, Pbuffer_info->Pbuffer_dc);
|
|
|
|
wglDestroyPbufferARB(Pbuffer_info->Pbuffer);
|
|
|
|
}
|
2004-03-01 20:59:32 -05:00
|
|
|
|
|
|
|
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Pbuffer_nSetAttrib
|
2004-07-25 10:51:28 -04:00
|
|
|
(JNIEnv *env, jclass clazz, jobject buffer_handle, jint attrib, jint value)
|
2004-03-01 20:59:32 -05:00
|
|
|
{
|
2004-07-25 10:51:28 -04:00
|
|
|
PbufferInfo *Pbuffer_info = (PbufferInfo *)env->GetDirectBufferAddress(buffer_handle);
|
2004-03-01 20:59:32 -05:00
|
|
|
|
|
|
|
int attribs[3];
|
|
|
|
|
|
|
|
attribs[0] = attrib;
|
|
|
|
attribs[1] = value;
|
|
|
|
attribs[2] = 0;
|
|
|
|
|
|
|
|
wglSetPbufferAttribARB(Pbuffer_info->Pbuffer, attribs);
|
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Pbuffer_nBindTexImage
|
2004-07-25 10:51:28 -04:00
|
|
|
(JNIEnv *env, jclass clazz, jobject buffer_handle, jint buffer)
|
2004-03-01 20:59:32 -05:00
|
|
|
{
|
2004-07-25 10:51:28 -04:00
|
|
|
PbufferInfo *Pbuffer_info = (PbufferInfo *)env->GetDirectBufferAddress(buffer_handle);
|
2004-03-01 20:59:32 -05:00
|
|
|
wglBindTexImageARB(Pbuffer_info->Pbuffer, buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Pbuffer_nReleaseTexImage
|
2004-07-25 10:51:28 -04:00
|
|
|
(JNIEnv *env, jclass clazz, jobject buffer_handle, jint buffer)
|
2004-03-01 20:59:32 -05:00
|
|
|
{
|
2004-07-25 10:51:28 -04:00
|
|
|
PbufferInfo *Pbuffer_info = (PbufferInfo *)env->GetDirectBufferAddress(buffer_handle);
|
2004-03-01 20:59:32 -05:00
|
|
|
wglReleaseTexImageARB(Pbuffer_info->Pbuffer, buffer);
|
|
|
|
}
|