Sign in to follow this  
zqueezy

OpenGL SwapChain DX10

Recommended Posts

zqueezy    122
heyhey, It's almost driving me crazy... I wrote an independent renderer (OpenGL/DX10). The problem is the aspect ratio of the DX10 renderer. When I use a single window to render everything goes fine ( So I assume my matrices/Viewport-setup) is correct. when I use 2 windows (wxWidgets and a splitter) the rendering is screwed up (but only the DX-one). As both share some code I guess the problem is somehow related to the swapchains.
bool MxD3D10Renderer::DrawWindow( MxWindow* pWindow )
{
	_MxWindowD3D10* pWindowData = GetD3D10WindowStruct( pWindow );

	// Check dx10struct for validity! An Error Here Must be traced immediately!!!
	MxAssert( pWindowData );
	MxAssert( pWindowData->m_pRenderTargetView != 0 && pWindowData->m_pSwapChain != 0 && pWindowData->m_pDepthStencilView != 0 );

	// Some SwapChain Checking:
	{
		//DXGI_SURFACE_DESC swapDesc;
		DXGI_SWAP_CHAIN_DESC swapDesc;

		pWindowData->m_pSwapChain->GetDesc( &swapDesc );
		MxAssert( swapDesc.BufferDesc.Width  == pWindow->GetWxWindow()->GetSize().GetWidth() );
		MxAssert( swapDesc.BufferDesc.Height == pWindow->GetWxWindow()->GetSize().GetHeight() );
	}

	m_pD3DDevice->OMSetRenderTargets( 1, &pWindowData->m_pRenderTargetView, pWindowData->m_pDepthStencilView );

	float ClearColor[4] = { 0.176f, 0.196f, 0.667f, 1.0f };

	m_pD3DDevice->ClearRenderTargetView( pWindowData->m_pRenderTargetView, ClearColor );

	m_pD3DDevice->ClearDepthStencilView( pWindowData->m_pDepthStencilView, D3D10_CLEAR_DEPTH|D3D10_CLEAR_STENCIL, 1.f, 0 );

	std::list< MxSceneView* >& refSceneViews = pWindow->GetConnectedViews();
	std::list< MxSceneView* >::iterator itor;


	D3D10_VIEWPORT vp;
	vp.Width = pWindow->GetWxWindow()->GetSize().GetWidth();
	vp.Height = pWindow->GetWxWindow()->GetSize().GetHeight();
	vp.MinDepth = 0.0f;
	vp.MaxDepth = 1.0f;
	vp.TopLeftX = 0;
	vp.TopLeftY = 0;
	m_pD3DDevice->RSSetViewports( 1, &vp );


	for ( itor = refSceneViews.begin(); itor != refSceneViews.end(); ++itor ) {
		// Call superclass for rendering
		Render( (*itor), &pWindow->m_WindowRenderOptions );
	}

	// todo: might be that the vsynching under dx10 is the first parameter of the present-call, might set to 1
	if ( FAILED ( pWindowData->m_pSwapChain->Present( 0, 0 ) ) ) {
		MxAssert( 0 );
		return false;
	}
}

each window has it's own swapchain and depth/stencil texture that are created like this (resizing uses almost the same)
bool MxD3D10Renderer::MakeWindowCompatible( MxWindow* pWindow )
{
	MX_FUNCTION_BEGIN();

	if ( !pWindow ) return false;
	MxWindowWin32* pWindowWin32 = static_cast< MxWindowWin32* > ( pWindow );

	// descriptions:
	wxSize wndSize = pWindowWin32->GetSize();
	int iWidth = wndSize.GetWidth();
	int iHeight = wndSize.GetHeight();

	RECT rect;
	GetClientRect( (HWND)pWindowWin32->GetHWND(), &rect );
	iWidth = rect.right - rect.left;
	iHeight = rect.bottom - rect.top;

	iWidth  = std::max( 1, iWidth );
	iHeight = std::max( 1, iHeight );

	DXGI_FORMAT format = DXGI_FORMAT_R8G8B8A8_UNORM;			// 32Bit + Alpha
	int iNumSamples = 1 << 3;
	
	// check maximum multisampling
	MxAssert( iNumSamples <= D3D10_MAX_MULTISAMPLE_SAMPLE_COUNT );
	
	// Check the format that was set above
	UINT uiFormatSupport = 0;
	if ( FAILED( m_pD3DDevice->CheckFormatSupport( format, &uiFormatSupport ) ) ) {
		MxAssert( 0 );
		return false;
	}

	// Check the multisampling and quality settings:
	UINT SampleCount = iNumSamples;								// MSDN: "The default sampler mode, with no anti-aliasing, has a count of 1 and a quality level of 0."
	UINT NumQualityLevels = 0;

	if ( FAILED( m_pD3DDevice->CheckMultisampleQualityLevels( format, SampleCount, &NumQualityLevels ) ) ) {
		MX_LOG_MAIN << MX_LOG_FUNCTION << "Failed to check multisample quality levels!\n";
		MxAssert( 0 );
		return false;
	}
	if ( NumQualityLevels == 0 ) {								// 0 means not compatible
		MX_LOG_MAIN << MX_LOG_FUNCTION << "Multisample quality levels == 0!\n";
		MxAssert( 0 );
		return false;
	}

	UINT Quality = NumQualityLevels-1;							// can range from 0 to QualityLevels-1

	DXGI_SAMPLE_DESC sampledesc;
	sampledesc.Count		= SampleCount;
	sampledesc.Quality		= Quality;

	// Fill out Swap Chain
	DXGI_SWAP_CHAIN_DESC dsc				= {0};
	dsc.BufferDesc.Width					= iWidth;
	dsc.BufferDesc.Height					= iHeight;
	dsc.BufferDesc.RefreshRate.Denominator	= 1;				// 60Hz
	dsc.BufferDesc.RefreshRate.Numerator	= 60;
	dsc.BufferDesc.Format					= format;
	dsc.BufferDesc.Scaling					= DXGI_MODE_SCALING_UNSPECIFIED;
	dsc.BufferDesc.ScanlineOrdering			= DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
	dsc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;			// Use as Render Target
	dsc.BufferCount = 1;	
	dsc.OutputWindow = (HWND)pWindowWin32->GetHWND();			// hwnd of window
	dsc.Windowed = true;
	dsc.SampleDesc = sampledesc;								// Number of samples per pixel
	dsc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;					// Switch pointers instead of copying
	dsc.Flags = 0;												// No Flags

	//_MxWindowD3D10 compatibleWindow;
	_MxWindowD3D10* pWindowData = GetD3D10WindowStruct( pWindow );// new _MxWindowD3D10();

	
	// create a swap-chain for that window
	if ( FAILED ( m_pFactory->CreateSwapChain( m_pD3DDevice, &dsc, &pWindowData->m_pSwapChain ) ) ) {
		MX_LOG_MAIN << MX_LOG_FUNCTION << "Failed to create Swap Chain!\n";
		MxAssert( 0 );
		return false;
	}

	// retrieve the backbuffer ( it's a com resource )
	ID3D10Texture2D* pBackBuffer;
	if ( FAILED ( pWindowData->m_pSwapChain->GetBuffer( 0, __uuidof( ID3D10Texture2D ), reinterpret_cast< void** >( &pBackBuffer ) ) ) ) {
		MX_LOG_MAIN << MX_LOG_FUNCTION << "Failed to retrieve SwapChain Backbuffer!\n";
		MxAssert( 0 );
		return false;
	}
	// Create a RenderTargetView from the backbuffer
	if ( FAILED ( m_pD3DDevice->CreateRenderTargetView( pBackBuffer, 0, &pWindowData->m_pRenderTargetView ) ) ) {
		MX_LOG_MAIN << MX_LOG_FUNCTION << "Failed to Create RenderTargetView!\n";
		MxAssert( 0 );
		return false;
	}
	// release com-resource
	pBackBuffer->Release();
	
	// also create a depth/stencil buffer for this window:
	D3D10_TEXTURE2D_DESC depthStencilDesc;
	depthStencilDesc.Width              = iWidth;
	depthStencilDesc.Height             = iHeight;
	depthStencilDesc.MipLevels          = 1;										// Only one mipmap-level
	depthStencilDesc.ArraySize          = 1;										// Only 1 texture
	depthStencilDesc.Format             = DXGI_FORMAT_D32_FLOAT;

	depthStencilDesc.SampleDesc	= sampledesc;								// Sample description must match
	depthStencilDesc.Usage              = D3D10_USAGE_DEFAULT;						// cannot be read/written by CPU
	depthStencilDesc.BindFlags          = D3D10_BIND_DEPTH_STENCIL;
	depthStencilDesc.CPUAccessFlags     = 0;
	depthStencilDesc.MiscFlags          = 0;

	if ( FAILED ( m_pD3DDevice->CreateTexture2D( &depthStencilDesc, 0, &pWindowData->m_pDepthStencilBuffer ) ) ) {
		MX_LOG_MAIN << MX_LOG_FUNCTION << "Failed to create 2D Texture for window!\n";
		MxAssert( 0 );
		return false;
	}

	// Create the depth stencil view
	D3D10_DEPTH_STENCIL_VIEW_DESC descDSV;
	descDSV.Format = depthStencilDesc.Format;
	descDSV.ViewDimension = D3D10_DSV_DIMENSION_TEXTURE2DMS;
	descDSV.Texture2D.MipSlice = 0;

	if ( FAILED ( m_pD3DDevice->CreateDepthStencilView( pWindowData->m_pDepthStencilBuffer, &descDSV, &pWindowData->m_pDepthStencilView ) ) ) {
		MX_LOG_MAIN << MX_LOG_FUNCTION << "Failed to create depth stencil view for window!\n";
		MxAssert( 0 );
		return false;
	}

	pWindowWin32->SetUserData( pWindowData );

	return true;
}

I get no error or assert-break whatsoever. I checked the matrices and compared them with the rendering of a single window (regarding a specific size and stuff). The only thing that I found on the internet was on how to actually create different swap-chains for each window (as I do above). My device is created without a swapchain ("D3D10CreateDevice"). When I use a wxSplitter and only render one window (and the other wxPanel) everything goes fine.. I have no clue anymore where to look for the error. Please, please help me. thanks in advance zqueezy

Share this post


Link to post
Share on other sites
zqueezy    122
More code? Please I'm desperate... :)
Some is not optimized... mostly the same as the MakeCompatible


bool MxD3D10Renderer::ResizeWindow( MxWindow* pWindow, int iWidth, int iHeight )
{
if ( !pWindow ) return true;

_MxWindowD3D10* pWindowData = GetD3D10WindowStruct( pWindow );

if ( pWindowData->m_pSwapChain == 0 || pWindowData->m_pRenderTargetView == 0 ) {
/// Window is not ready yet...
return false;
}

// Get safe with and height for resizing
iHeight = std::max( 1, iHeight );
iWidth = std::max( 1, iWidth );


DXGI_SWAP_CHAIN_DESC swapDesc;
pWindowData->m_pSwapChain->GetDesc( &swapDesc );

pWindowData->m_pRenderTargetView->Release();
pWindowData->m_pDepthStencilView->Release();
pWindowData->m_pDepthStencilBuffer->Release();

pWindowData->m_pSwapChain->ResizeBuffers( swapDesc.BufferCount, (UINT)iWidth, (UINT)iHeight, DXGI_FORMAT_UNKNOWN, 0 );

// retrieve the backbuffer ( it's a com resource )
ID3D10Texture2D* pBackBuffer;
if ( FAILED ( pWindowData->m_pSwapChain->GetBuffer( 0, __uuidof( ID3D10Texture2D ), reinterpret_cast< void** >( &pBackBuffer ) ) ) ) {
MX_LOG_MAIN << MX_LOG_FUNCTION << "Failed to retrieve SwapChain Backbuffer!\n";
MxAssert( 0 );
return false;
}
// Get a rendertargetview for the backbuffer
if ( FAILED ( m_pD3DDevice->CreateRenderTargetView( pBackBuffer, 0, &pWindowData->m_pRenderTargetView ) ) ) {
MX_LOG_MAIN << MX_LOG_FUNCTION << "Failed to Create RenderTargetView!\n";
MxAssert( 0 );
return false;
}
// release com-resource
pBackBuffer->Release();

DXGI_FORMAT format = DXGI_FORMAT_R8G8B8A8_UNORM;
int iNumSamples = 1 << 3;

// check maximum multisampling
MxAssert( iNumSamples <= D3D10_MAX_MULTISAMPLE_SAMPLE_COUNT );

// Check the format that was set above
UINT uiFormatSupport = 0;
if ( FAILED( m_pD3DDevice->CheckFormatSupport( format, &uiFormatSupport ) ) ) {
MxAssert( 0 );
return false;
}

// Check the multisampling and quality settings:
UINT SampleCount = iNumSamples; // MSDN: "The default sampler mode, with no anti-aliasing, has a count of 1 and a quality level of 0."
UINT NumQualityLevels = 0;

if ( FAILED( m_pD3DDevice->CheckMultisampleQualityLevels( format, SampleCount, &NumQualityLevels ) ) ) {
MX_LOG_MAIN << MX_LOG_FUNCTION << "Failed to check multisample quality levels!\n";
MxAssert( 0 );
return false;
}
if ( NumQualityLevels == 0 ) { // 0 means not compatible
MX_LOG_MAIN << MX_LOG_FUNCTION << "Multisample quality levels == 0!\n";
MxAssert( 0 );
return false;
}

UINT Quality = NumQualityLevels-1; // can range from 0 to QualityLevels-1

DXGI_SAMPLE_DESC sampledesc;
sampledesc.Count = SampleCount;
sampledesc.Quality = Quality;


// also create a depth/stencil buffer for this window:
D3D10_TEXTURE2D_DESC depthStencilDesc;
depthStencilDesc.Width = iWidth;
depthStencilDesc.Height = iHeight;
depthStencilDesc.MipLevels = 1; // Only one mipmap-level
depthStencilDesc.ArraySize = 1; // Only 1 texture
//depthStencilDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthStencilDesc.Format = DXGI_FORMAT_D32_FLOAT;


depthStencilDesc.SampleDesc = sampledesc; // Sample description must match
depthStencilDesc.Usage = D3D10_USAGE_DEFAULT; // cannot be read/written by CPU
depthStencilDesc.BindFlags = D3D10_BIND_DEPTH_STENCIL;
depthStencilDesc.CPUAccessFlags = 0;
depthStencilDesc.MiscFlags = 0;

if ( FAILED ( m_pD3DDevice->CreateTexture2D( &depthStencilDesc, 0, &pWindowData->m_pDepthStencilBuffer ) ) ) {
MX_LOG_MAIN << MX_LOG_FUNCTION << "Failed to create 2D Texture for window!\n";
MxAssert( 0 );
return false;
}

// Create the depth stencil view
D3D10_DEPTH_STENCIL_VIEW_DESC descDSV;
descDSV.Format = depthStencilDesc.Format;
descDSV.ViewDimension = D3D10_DSV_DIMENSION_TEXTURE2DMS;
descDSV.Texture2D.MipSlice = 0;

if ( FAILED ( m_pD3DDevice->CreateDepthStencilView( pWindowData->m_pDepthStencilBuffer, &descDSV, &pWindowData->m_pDepthStencilView ) ) ) {
MX_LOG_MAIN << MX_LOG_FUNCTION << "Failed to create depth stencil view for window!\n";
MxAssert( 0 );
return false;
}

return true;
}




Screwed:
Screwed
Unscrewed (1 Splitter, 1 Window, 1 Empty Panel):
Non-Screwed

[Edited by - zqueezy on March 21, 2010 7:54:16 AM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By Arulbabu Donbosco
      There are studios selling applications which is just copying any 3Dgraphic content and regenerating into another new window. especially for CAVE Virtual reality experience. so that the user opens REvite or CAD or any other 3D applications and opens a model. then when the user selects the rendered window the VR application copies the 3D model information from the OpenGL window. 
      I got the clue that the VR application replaces the windows opengl32.dll file. how this is possible ... how can we copy the 3d content from the current OpenGL window.
      anyone, please help me .. how to go further... to create an application like VR CAVE. 
       
      Thanks
    • By cebugdev
      hi all,

      i am trying to build an OpenGL 2D GUI system, (yeah yeah, i know i should not be re inventing the wheel, but this is for educational and some other purpose only),
      i have built GUI system before using 2D systems such as that of HTML/JS canvas, but in 2D system, i can directly match a mouse coordinates to the actual graphic coordinates with additional computation for screen size/ratio/scale ofcourse.
      now i want to port it to OpenGL, i know that to render a 2D object in OpenGL we specify coordiantes in Clip space or use the orthographic projection, now heres what i need help about.
      1. what is the right way of rendering the GUI? is it thru drawing in clip space or switching to ortho projection?
      2. from screen coordinates (top left is 0,0 nd bottom right is width height), how can i map the mouse coordinates to OpenGL 2D so that mouse events such as button click works? In consideration ofcourse to the current screen/size dimension.
      3. when let say if the screen size/dimension is different, how to handle this? in my previous javascript 2D engine using canvas, i just have my working coordinates and then just perform the bitblk or copying my working canvas to screen canvas and scale the mouse coordinates from there, in OpenGL how to work on a multiple screen sizes (more like an OpenGL ES question).
      lastly, if you guys know any books, resources, links or tutorials that handle or discuss this, i found one with marekknows opengl game engine website but its not free,
      Just let me know. Did not have any luck finding resource in google for writing our own OpenGL GUI framework.
      IF there are no any available online, just let me know, what things do i need to look into for OpenGL and i will study them one by one to make it work.
      thank you, and looking forward to positive replies.
    • By fllwr0491
      I have a few beginner questions about tesselation that I really have no clue.
      The opengl wiki doesn't seem to talk anything about the details.
       
      What is the relationship between TCS layout out and TES layout in?
      How does the tesselator know how control points are organized?
          e.g. If TES input requests triangles, but TCS can output N vertices.
             What happens in this case?
      In this article,
      http://www.informit.com/articles/article.aspx?p=2120983
      the isoline example TCS out=4, but TES in=isoline.
      And gl_TessCoord is only a single one.
      So which ones are the control points?
      How are tesselator building primitives?
    • By Orella
      I've been developing a 2D Engine using SFML + ImGui.
      Here you can see an image
      The editor is rendered using ImGui and the scene window is a sf::RenderTexture where I draw the GameObjects and then is converted to ImGui::Image to render it in the editor.
      Now I need to create a 3D Engine during this year in my Bachelor Degree but using SDL2 + ImGui and I want to recreate what I did with the 2D Engine. 
      I've managed to render the editor like I did in the 2D Engine using this example that comes with ImGui. 
      3D Editor preview
      But I don't know how to create an equivalent of sf::RenderTexture in SDL2, so I can draw the 3D scene there and convert it to ImGui::Image to show it in the editor.
      If you can provide code will be better. And if you want me to provide any specific code tell me.
      Thanks!
    • By Picpenguin
      Hi
      I'm new to learning OpenGL and still learning C. I'm using SDL2, glew, OpenGL 3.3, linmath and stb_image.
      I started following through learnopengl.com and got through it until I had to load models. The problem is, it uses Assimp for loading models. Assimp is C++ and uses things I don't want in my program (boost for example) and C support doesn't seem that good.
      Things like glVertexAttribPointer and shaders are still confusing to me, but I have to start somewhere right?
      I can't seem to find any good loading/rendering tutorials or source code that is simple to use and easy to understand.
      I have tried this for over a week by myself, searching for solutions but so far no luck. With tinyobjloader-c and project that uses it, FantasyGolfSimulator, I was able to actually load the model with plain color (always the same color no matter what I do) on screen and move it around, but cannot figure out how to use textures or use its multiple textures with it.
      I don't ask much: I just want to load models with textures in them, maybe have lights affect them (directional spotlight etc). Also, some models have multiple parts and multiple textures in them, how can I handle those?
      Are there solutions anywhere?
      Thank you for your time. Sorry if this is a bit confusing, English isn't my native language
  • Popular Now