Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Get DX10 Texture object from classic DX9 sampler


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 Meltac   Members   -  Reputation: 423

Like
0Likes
Like

Posted 28 February 2013 - 08:25 AM

Hi all

 

In DX10/11 the sampler and texture objects have been separated, whereas in DX9 there was only one object for both.

 

Meaning, the Sample() and Gather() functions in HLSL require an object of type Texture which I do not have with a "classic" DX9 sampler2D object.

 

So is there a way to access the Texture object of a DX9 sampler2D object when compiling against DX10 / ps_4_0 or ps_5_0?

 

I'm talking pure HLSL here, so please no hints in C++ or C#.

 

Thanks in advance.


Edited by Meltac, 28 February 2013 - 08:26 AM.


Sponsor:

#2 Seabolt   Members   -  Reputation: 633

Like
0Likes
Like

Posted 28 February 2013 - 08:59 AM

I use macros that define how to sample and declare a texture that just defines differently per platform. Like so:

 

#ifdef DX11
	#define DEFINE_2D_TEXTURE(tex, regIndex) Texture2D tex##Texture : register(t##regIndex); \
			                         SamplerState tex##Sampler : register(s##regIndex)
	#define SAMPLE_TEXTURE_2D(tex, coord) tex##Texture.Sample(tex##Sampler, coord)
#else
	#define DEFINE_2D_TEXTURE(tex, regIndex) sampler tex##Sampler : register(s##regIndex)
	#define SAMPLE_TEXTURE_2D(tex, coord) tex2D(tex##Sampler, coord)
#endif

 

It does require you to #define your platform when you create your shader though.


Perception is when one imagination clashes with another

#3 Meltac   Members   -  Reputation: 423

Like
0Likes
Like

Posted 28 February 2013 - 09:12 AM

Thanks, but that's not what I'm after. I do not want to just use a different sampling method depending on platform. Instead I want to make use of the Sample() and especially of the Gather() function from within a HLSL pixel shader that has only direct access to DX9 sampler objects. So I need a way of getting a Texture object "out of" a DX9 sampler object in order to apply the Gather() function on it.


Edited by Meltac, 28 February 2013 - 09:12 AM.


#4 MJP   Moderators   -  Reputation: 11569

Like
1Likes
Like

Posted 28 February 2013 - 12:58 PM

You can't use the legacy "sampler2D" type at all in SM4.0/SM5.0 shader code. You have to use the new "Texture2D" and "SamplerState" types.



#5 Meltac   Members   -  Reputation: 423

Like
0Likes
Like

Posted 01 March 2013 - 02:14 AM

You can't use the legacy "sampler2D" type at all in SM4.0/SM5.0 shader code. You have to use the new "Texture2D" and "SamplerState" types.

 

Hmm, but why am I able to compile my "legacy" code against ps_5_0 then?

 

I had to change "half" to "float" and some tex2D calls to tex2Dlod as the compiler complained about that, but sampler2D works without any issues!



#6 Meltac   Members   -  Reputation: 423

Like
0Likes
Like

Posted 06 March 2013 - 03:03 AM

Nobody?



#7 Seabolt   Members   -  Reputation: 633

Like
0Likes
Like

Posted 06 March 2013 - 09:08 AM

Are you compiling your shaders with this (D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY), flag? If so they will compile. Look at the MSDN page for tex2dlod at the bottom of the page here.


Perception is when one imagination clashes with another

#8 Meltac   Members   -  Reputation: 423

Like
0Likes
Like

Posted 13 March 2013 - 03:31 AM

Are you compiling your shaders with this (D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY), flag? If so they will compile. Look at the MSDN page for tex2dlod at the bottom of the page here.

 

Ah, ok, that explains it. Thanks.

 

However, my initial question is still unanswered. I'm intending to use something like the Gather() function on a ps_5_0 target, but with legacy HLSL shader code. I don't get any Texture object from the host application, which I would need to call Gather() on it. All I have is a legacy "sampler2D" object.

 

Is there a way to use something like Gather() in this scenario in order to benefit from the hardware multi-sampling feature of DX10/11? E.g. Create a Texture object out of my HLSL shader code, which in turn makes calls to my samper2D when queried? Or some specific function call that works on sampler2D objects but in fact returns a mutli-sample like the Gather() method?


Edited by Meltac, 13 March 2013 - 03:32 AM.


#9 Seabolt   Members   -  Reputation: 633

Like
0Likes
Like

Posted 13 March 2013 - 11:58 AM

You'll have to manually gather the texels you want.

So your code would find the width and height of a texel:

 

float texelWidth = 1.0f/textureWidth;
float texelHeight = 1.0f/textureHeight;

 

Then you would manually sample the texture using texel width and height as an offset:

 

float4 color = 0;

float2 offsetUp = texCoords + float2( 0.0f, texelHeight );
float2 offsetDown = texCoords + float2( 0.0f, -texelHeight );
float2 offsetLeft = texCoords + float2( -texelWidth, 0.0f );
float2 offsetRight = texCoords + float2( texelWidth, 0.0f );

color += tex2D( tex, offsetUp );
color += tex2D( tex, offsetDown );
color += tex2D( tex, offsetLeft );
color += tex2D( tex, offsetRight );

color /= 4.0f;

 

This will sample the texture data in the cardinal directions of your input texcoord.
Note: This is just pseudo-code, I haven't tested this exact code, but the principle still stands.


Perception is when one imagination clashes with another

#10 Meltac   Members   -  Reputation: 423

Like
0Likes
Like

Posted 13 March 2013 - 02:18 PM

Yeah, but then I don't benefit from the hardware accelerated way of sampling four texels in just one procedure call like it's possible under DX10/11 using Gather().

 

My goal is improving runtime performance by the use of new hardware features of DX10/11, i.e. bring those improvements into my legacy DX9-like code (which compiles against Shader Model 5 and runs on a DX11 GPU). Is this not possible at all?



#11 Seabolt   Members   -  Reputation: 633

Like
0Likes
Like

Posted 13 March 2013 - 05:49 PM

If you're using tex2D, then this is not possible. 


Perception is when one imagination clashes with another

#12 Meltac   Members   -  Reputation: 423

Like
0Likes
Like

Posted 14 March 2013 - 03:20 AM

Too bad. Well, thank you for your help!






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS