examples/Qt/ISFEditor/ISFEditor_app/Spout/SpoutGLDXinterop.cpp
//
// Draw the shared DirectX 9 texture
// equivalent to DrawSharedTexture for the shared OpenGL texture
//

Draw the shared DirectX 9 texture
equivalent to DrawSharedTexture for the shared OpenGL texture
Draw the shared DirectX 9 texture equivalent to DrawSharedTexture for the shared OpenGL texture
bool spoutGLDXinterop::DrawDX9texture(float max_x, float max_y, float aspect, bool bInvert, GLuint HostFBO)
{
	IDirect3DSurface9 * SharedTextureSurface = NULL;
	D3DLOCKED_RECT d3dlr; 
	HRESULT hr;
	unsigned int width, height;
	UNREFERENCED_PARAMETER(HostFBO);
	if(!GetDX9())
		return false;
	width  = m_TextureInfo.width;
	height = m_TextureInfo.height;
	if(!CheckDX9surface(width, height))
		return false;
	CheckOpenGLTexture(m_TexID, GL_RGBA, width, height, m_TexWidth, m_TexHeight);
	if(spoutdx.CheckAccess(m_hAccessMutex)) {
		hr = m_dxTexture->GetSurfaceLevel(0, &SharedTextureSurface);
		if(SUCCEEDED(hr)) {
			hr = m_pDevice->GetRenderTargetData(SharedTextureSurface, g_DX9surface);
			if(SUCCEEDED(hr)) {
				hr = g_DX9surface->LockRect(&d3dlr, NULL, D3DLOCK_NO_DIRTY_UPDATE | D3DLOCK_READONLY);
				if(SUCCEEDED(hr)) {
					glBindTexture(GL_TEXTURE_2D, m_TexID);
					glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_BGRA_EXT, GL_UNSIGNED_BYTE, d3dlr.pBits);
					glBindTexture(GL_TEXTURE_2D, 0);
					g_DX9surface->UnlockRect();
					SaveOpenGLstate(width, height);
					glEnable(GL_TEXTURE_2D);
					glBindTexture(GL_TEXTURE_2D, m_TexID);
					glColor4f(1.f, 1.f, 1.f, 1.f);
					glBegin(GL_QUADS);
					if(bInvert) {
						glTexCoord2f(0.0,   0.0);	glVertex2f(-aspect,-1.0); 
						glTexCoord2f(0.0,   max_y);	glVertex2f(-aspect, 1.0); 
						glTexCoord2f(max_x, max_y);	glVertex2f( aspect, 1.0); 
						glTexCoord2f(max_x, 0.0);	glVertex2f( aspect,-1.0); 
					}
					else {
						glTexCoord2f(0.0,	max_y);	glVertex2f(-aspect,-1.0); 
						glTexCoord2f(0.0,	0.0);	glVertex2f(-aspect, 1.0); 
						glTexCoord2f(max_x, 0.0);	glVertex2f( aspect, 1.0); 
						glTexCoord2f(max_x, max_y);	glVertex2f( aspect,-1.0); 
					}
					glEnd();
					glBindTexture(GL_TEXTURE_2D, 0);
					glDisable(GL_TEXTURE_2D);
					RestoreOpenGLstate();
				}
				if(SharedTextureSurface) SharedTextureSurface->Release();
				spoutdx.AllowAccess(m_hAccessMutex);
				return true;
			} 
		} 
	} 
	if(SharedTextureSurface) SharedTextureSurface->Release();
	spoutdx.AllowAccess(m_hAccessMutex);
	return false;
}
bool spoutGLDXinterop :: DrawDX9texture ( float max_x , float max_y , float aspect , bool bInvert , GLuint HostFBO ) { IDirect3DSurface9 * SharedTextureSurface = NULL ; D3DLOCKED_RECT d3dlr ; HRESULT hr ; unsigned int width , height ; UNREFERENCED_PARAMETER ( HostFBO ) ; if ( ! GetDX9 ( ) ) return false ; width = m_TextureInfo . width ; height = m_TextureInfo . height ; if ( ! CheckDX9surface ( width , height ) ) return false ; CheckOpenGLTexture ( m_TexID , GL_RGBA , width , height , m_TexWidth , m_TexHeight ) ; if ( spoutdx . CheckAccess ( m_hAccessMutex ) ) { hr = m_dxTexture -> GetSurfaceLevel ( 0 , & SharedTextureSurface ) ; if ( SUCCEEDED ( hr ) ) { hr = m_pDevice -> GetRenderTargetData ( SharedTextureSurface , g_DX9surface ) ; if ( SUCCEEDED ( hr ) ) { hr = g_DX9surface -> LockRect ( & d3dlr , NULL , D3DLOCK_NO_DIRTY_UPDATE | D3DLOCK_READONLY ) ; if ( SUCCEEDED ( hr ) ) { glBindTexture ( GL_TEXTURE_2D , m_TexID ) ; glTexSubImage2D ( GL_TEXTURE_2D , 0 , 0 , 0 , width , height , GL_BGRA_EXT , GL_UNSIGNED_BYTE , d3dlr . pBits ) ; glBindTexture ( GL_TEXTURE_2D , 0 ) ; g_DX9surface -> UnlockRect ( ) ; SaveOpenGLstate ( width , height ) ; glEnable ( GL_TEXTURE_2D ) ; glBindTexture ( GL_TEXTURE_2D , m_TexID ) ; glColor4f ( 1.f , 1.f , 1.f , 1.f ) ; glBegin ( GL_QUADS ) ; if ( bInvert ) { glTexCoord2f ( 0.0 , 0.0 ) ; glVertex2f ( - aspect , -1.0 ) ; glTexCoord2f ( 0.0 , max_y ) ; glVertex2f ( - aspect , 1.0 ) ; glTexCoord2f ( max_x , max_y ) ; glVertex2f ( aspect , 1.0 ) ; glTexCoord2f ( max_x , 0.0 ) ; glVertex2f ( aspect , -1.0 ) ; } else { glTexCoord2f ( 0.0 , max_y ) ; glVertex2f ( - aspect , -1.0 ) ; glTexCoord2f ( 0.0 , 0.0 ) ; glVertex2f ( - aspect , 1.0 ) ; glTexCoord2f ( max_x , 0.0 ) ; glVertex2f ( aspect , 1.0 ) ; glTexCoord2f ( max_x , max_y ) ; glVertex2f ( aspect , -1.0 ) ; } glEnd ( ) ; glBindTexture ( GL_TEXTURE_2D , 0 ) ; glDisable ( GL_TEXTURE_2D ) ; RestoreOpenGLstate ( ) ; } if ( SharedTextureSurface ) SharedTextureSurface -> Release ( ) ; spoutdx . AllowAccess ( m_hAccessMutex ) ; return true ; } } } if ( SharedTextureSurface ) SharedTextureSurface -> Release ( ) ; spoutdx . AllowAccess ( m_hAccessMutex ) ; return false ; }
Draw the shared DirectX 9 texture
equivalent to DrawSharedTexture for the shared OpenGL texture
Draw the shared DirectX 9 texture equivalent to DrawSharedTexture for the shared OpenGL texture
// LockRect for data transfer
 // Only for DX9 mode
 // If a CPU surface has not been created or it is a different size, create a new one
 // Create or resize a local OpenGL texture
 // Create a local shared texture surface
 // Use GetRenderTargetData to copy the shared texture data from device memory to system memory
 // Lock the system memory surface for read
 // Copy the surface pixels to the local OpenGL texture
 // Draw the local texture and invert as necessary
 // DirectX coordinates are already inverted
 // lower left
 // upper left
 // upper right
 // lower right
 // lower left
 // upper left
 // upper right
 // lower right
 // endif GetRenderTargetData OK
 // endif GetSurfaceLevel OK
 // endif mutex access OK
