Advertisement Jump to content
Sign in to follow this  
ggeasy

Render To Texture To Texture in Directx

This topic is 1733 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

He everyone,

 

For a school project I am working on a old engine we once made in directx9 and the challenge is to implement the oculus rift in it. Now I am having a issue with rendering of the eyes. As the SDK of oculus says that I need to render my scene twice (in 2 viewports) and then pass the result to a distortion shader for the distortion correction. 

 

But the distortion shader (pixel shader) distorts only one side of the screen, atleast the one I am using. I use the one from this forum post one with will work with DX9: 

https://developer.oculusvr.com/forums/viewtopic.php?f=33&t=497

 

This means I have to distort for each eye. I want to do this by rendering the full scene to a texture. Then apply the shader to this texture twice for left and right. The result of this is also a texture and then put this two textures to my quad. 

 

Now the full scene is rendering perfectly and when I apply this directly to my quad you see the scene rendered twice. However when I apply the pixelshader to my full scene texture and apply the resulting texture to my quad I won't see a thing... The HRESULTS of all SetTextures are S_OK. And I cant find the problem anywhere.

 

Here is some code:

Here I create all textures and surfaces:

pd3dDevice->CreateTexture(backBufferWidth,
		backBufferHeight,
		1,
		D3DUSAGE_RENDERTARGET,
		D3DFMT_A16B16G16R16,
		D3DPOOL_DEFAULT,
		&fullSceneTexture,
		NULL);
	fullSceneTexture->GetSurfaceLevel(0, &fullSceneSurface);

	pd3dDevice->CreateTexture(backBufferWidth,
		backBufferHeight,
		1,
		D3DUSAGE_RENDERTARGET,
		D3DFMT_A16B16G16R16,
		D3DPOOL_DEFAULT,
		&leftEyeTexture,
		NULL);
	leftEyeTexture->GetSurfaceLevel(0, &leftEyeSurface);

	pd3dDevice->CreateTexture(backBufferWidth,
		backBufferHeight,
		1,
		D3DUSAGE_RENDERTARGET,
		D3DFMT_A16B16G16R16,
		D3DPOOL_DEFAULT,
		&rightEyeTexture,
		NULL);
	rightEyeTexture->GetSurfaceLevel(0, &rightEyeSurface);

Here is my main render loop (the renderScene() parameters are still to be decided):

void Scene::renderScene()
{
	rnd->setupRenderToTexture();
	render(SConfig.GetEyeRenderParams(OVR::Util::Render::StereoEye_Left));
	render(SConfig.GetEyeRenderParams(OVR::Util::Render::StereoEye_Right));
	rnd->endRenderToTexture();
	rnd->renderEye(SConfig.GetEyeRenderParams(OVR::Util::Render::StereoEye_Left), SConfig);
	rnd->renderEye(SConfig.GetEyeRenderParams(OVR::Util::Render::StereoEye_Right), SConfig);
	rnd->renderScene(SConfig.GetEyeRenderParams(OVR::Util::Render::StereoEye_Right), SConfig);
	rnd->present(window->getHandle());
};

void Scene::render(const OVR::Util::Render::StereoEyeParams& stereo){
	rnd->setViewport(stereo);
	for (unsigned int i = 0; i < entities.size(); ++i)
	{
		entities[i]->drawSelf(rnd);
	}
	camera->drawSelf(rnd, stereo, SConfig);
	skybox->drawSelf(rnd);
	terrain->drawSelf(rnd);
}

Here are my render methods in my Renderer. In the RenderScene() method I call for now setTexture once, but even this texture is not showing up:

void RendererDX9::renderEye(const OVR::Util::Render::StereoEyeParams& params, OVR::Util::Render::StereoConfig SConfig)
{
	if (params.Eye == OVR::Util::Render::StereoEye_Right){
		pd3dDevice->SetRenderTarget(0, rightEyeSurface);
	}
	else{
		pd3dDevice->SetRenderTarget(0, leftEyeSurface);
	}
	pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL, D3DCOLOR_XRGB(0, 0, 255), 1.0f, 0);
	pd3dDevice->BeginScene();
	//apply shader
	pd3dDevice->SetPixelShader(lpPixelShader);
	setPixelShaderConstants(params, SConfig);	
	pd3dDevice->SetTexture(0, fullSceneTexture);
	pd3dDevice->SetPixelShader(NULL);

	pd3dDevice->EndScene();
};

void RendererDX9::renderScene(const OVR::Util::Render::StereoEyeParams& params, OVR::Util::Render::StereoConfig SConfig){
	pd3dDevice->SetRenderTarget(0, backBuffer);
	pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL, D3DCOLOR_XRGB(0, 0, 255), 1.0f, 0);
	pd3dDevice->BeginScene();
	//apply shader
	//render to aligned quad
	fillVertexBuffer("screenVertex", screenQuad, sizeof(CustomVertex)* 4);
	setZWriteEnable(false);
	setStreamSource("screenVertex", sizeof(CustomVertex));
	pd3dDevice->SetTexture(0, leftEyeTexture);
	//reset matrixes
	//resetWorldMatrix();
	pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
	setZWriteEnable(true);
	pd3dDevice->EndScene();
}

void RendererDX9::setupRenderToTexture(){
	pd3dDevice->SetRenderTarget(0, fullSceneSurface);
	pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL, D3DCOLOR_XRGB(0, 0, 255), 1.0f, 0);
	pd3dDevice->BeginScene();

};

void RendererDX9::endRenderToTexture(){
	pd3dDevice->EndScene();
};

Thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!