Sign in to follow this  

Rendering & Saving Environment Maps

This topic is 1722 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

I'm trying to render environment maps from my probes and I've read about a technique of using the GS to draw all 6 faces at the same time (?)

I haven't really found any details on how that is done, and if so is it the best way to do that? Or should I just render face after face 6 times ?

Now is there some way to compose this cubemap from my rendertargets or do I have to do that by hand using CubeMapGen from each face ?

And my last question how do I write this rendertarget to my filesystem as a *.DDS file ? Is there an API call or something ?

Share this post


Link to post
Share on other sites

Regarding the GS instancing to render to all faces in one pass, there is a nice little sample in the dxsdk showing exactly that.

Share this post


Link to post
Share on other sites

Yes, you may use GS to output a primitive to multiple render targets (the DX SDK example does exactly that). 

 

However, it is perfectly fine to render each cube map side separately too. The performance difference may be negligible since GS is typically a performance hog if used to output lots of primitives. I prefer slightly rendering each side separately since then you don't need separate vertex shaders for rendering cube maps / rendering normally. Since cube map render target gives you 6 rendertargetviews, you can render them separately by just selecting the desired target. 

 

There is a function in D3DX to save dds files (may work with cubemaps), but MSDN suggests using DirectXTK with similar function since D3DX is almost deprecated. 

 

[edit] It seems that DirectXTK won't save cubemaps correctly.

 

Cheers!

Edited by kauna

Share this post


Link to post
Share on other sites

@kauna So I should use D3DX to do that ?

 

And if I wanted to render dynamic cubemaps what size would be okay to render them (128x128, 256x256, 512x512) ?

Also does it make sense to interleave them over a few frames (i.e. render it only every 5 frames) ? 

Share this post


Link to post
Share on other sites

http://msdn.microsoft.com/en-us/library/windows/desktop/ff476298(v=vs.85).aspx You could try using this function for saving. It may or may not work with cube maps. 

 

The cube map resolution is up to your needs. How many cube maps you are planning to use in every scene? What is the cube map surface format? Are you using HDR rendering? Are you using cube maps for specular reflections? 

 

It is ok to render cubemaps "when needed". It is even possible to only render one side at a time and then swap the cube map when finished rendering. 

However, if you are considering supporting SLI rendering, then there might be some things to know. 

 

Cheers!

Share this post


Link to post
Share on other sites

So I don't do anything wrong...am I correct in thinking that for deferred rendering I need to create the entire GBuffer RenderTargets for each theoretical cubemap size?

(meaning 64², 128², 256², 512²) ?

Share this post


Link to post
Share on other sites

The way it works with the geometry shader, is, that you project each vertex inside the geometry shader onto the different sides and than output the resulting vertices with a SV_RenderTargetArrayIndex semantic which represents which side the projected vertex you are outputting is on.

Share this post


Link to post
Share on other sites

Well I've managed to get the rendering done (but using 6 passes) now...

How do I combine these 6 RenderTargets to a single CubeMap ShaderResource ?

Is there any way to do that using each RT's ID3D11Texture* ?

 

edit:

I tried something like:

	ID3D11Texture2D* cubeFaces[6];
	for(unsigned int i = 0; i < 6; i++)
	{
		this->faces[i] = new CustomRenderTarget(this->device, DXGI_FORMAT_R8G8B8A8_UNORM, this->size, this->size);
		cubeFaces[i] = this->faces[i]->GetRenderTargetTexture();
	}

	D3D11_SUBRESOURCE_DATA subData;
	subData.pSysMem = cubeFaces;
	subData.SysMemPitch = sizeof(XMFLOAT4) * this->size;

	ID3D11Texture2D* cubeMap = NULL;
	D3D11_TEXTURE2D_DESC texDesc;
	texDesc.Width = this->size;
	texDesc.Height = this->size;
	texDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	texDesc.MipLevels = 0;
	texDesc.ArraySize = 6;
	texDesc.SampleDesc.Count = 1;
	texDesc.Usage = D3D11_USAGE_DEFAULT;
	texDesc.SampleDesc.Quality = 0;
	texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
	texDesc.CPUAccessFlags = 0;
	texDesc.MiscFlags = D3D11_RESOURCE_MISC_TEXTURECUBE;
	
	HRESULT hr = this->device->CreateTexture2D(&texDesc, &subData, &cubeMap);

 

But I'm getting an error on the last line saying Invalid arguments.

Edited by lipsryme

Share this post


Link to post
Share on other sites

Well I've managed to get the rendering done (but using 6 passes) now...

How do I combine these 6 RenderTargets to a single CubeMap ShaderResource ?

Is there any way to do that using each RT's ID3D11Texture* ?

 

edit:

I tried something like:

	ID3D11Texture2D* cubeFaces[6];
	for(unsigned int i = 0; i < 6; i++)
	{
		this->faces[i] = new CustomRenderTarget(this->device, DXGI_FORMAT_R8G8B8A8_UNORM, this->size, this->size);
		cubeFaces[i] = this->faces[i]->GetRenderTargetTexture();
	}

	D3D11_SUBRESOURCE_DATA subData;
	subData.pSysMem = cubeFaces;
	subData.SysMemPitch = sizeof(XMFLOAT4) * this->size;

	ID3D11Texture2D* cubeMap = NULL;
	D3D11_TEXTURE2D_DESC texDesc;
	texDesc.Width = this->size;
	texDesc.Height = this->size;
	texDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	texDesc.MipLevels = 0;
	texDesc.ArraySize = 6;
	texDesc.SampleDesc.Count = 1;
	texDesc.Usage = D3D11_USAGE_DEFAULT;
	texDesc.SampleDesc.Quality = 0;
	texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
	texDesc.CPUAccessFlags = 0;
	texDesc.MiscFlags = D3D11_RESOURCE_MISC_TEXTURECUBE;
	
	HRESULT hr = this->device->CreateTexture2D(&texDesc, &subData, &cubeMap);

 

But I'm getting an error on the last line saying Invalid arguments.

 

 

It's all in the sample I mentioned in the first answer in the dxsdk, you should really take a look at it.

You create a texture with 6 slices, and the correct flags.

You create one shader resource view

you create 6 rendertarget views if you want to do the 6 passes manually, each referencing a specific slice. Job done

Share this post


Link to post
Share on other sites

Oh I see, sorry I thought it was just a sample about doing the GS "thing" ;)

I'll have a look asap rolleyes.gif

 

Update: Got it working now !

@kauna D3DX11SaveTextureToFile() works brilliantly.

 

My cube maps appear rather dark since I'm not rendering to my backbuffer (which is R8G8B8A8_SRGB).

Is there anything I need to know for gamma correct rendering ? Or rendering them to an FP16 format ?

 

Also my cubemap seems to reflect the opposite side of what's there. (basically like I can look through)

Not sure why that is...

I've defined my RT's in the order of X+, X-, Y+, Y-, Z+, Z-.

And here's my shader:


float4 PS(VSO input) : SV_TARGET
{
	float3 Normal_WS = normalize(input.NormalWS);
	float3 View_WS = normalize(input.CamPosWS.xyz - input.PosWS.xyz);
	float3 R = reflect(Normal_WS, View_WS);

	float3 reflectionColor = EnvironmentMap.SampleLevel(LinearSampler, R, 0).xyz;

	return float4(reflectionColor, 1.0f);
}

Edited by lipsryme

Share this post


Link to post
Share on other sites

In case you are interested in more details about how the technique works, you can take a look here.  There is descriptions for sphere mapping, cube mapping, and dual paraboloid mapping too, all of them as single pass techniques.

Share this post


Link to post
Share on other sites
I've managed to fix my problems so far. Great info on that link though.

Share this post


Link to post
Share on other sites

This topic is 1722 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.

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