Home » Community » Forums » DirectX and XNA » How to use a procedural texture in xna?
  Intel sponsors gamedev.net search:   
[Control Panel] [Register] [Bookmarks] [Who's Online] [Active Topics] [Stats] [FAQ] [Search]

Add Forum to Favorites |  Send Topic To a Friend | View Forum FAQ | Track this topic


 Last Thread Next Thread 
 How to use a procedural texture in xna?
Post New Topic  Post Reply 
I am generating the data for a 2D texture manually, and this data might change from frame to frame. Currently I just use a simple function to compute the texture, given below:


private void RenderTexture()
{
	for (int i = 0; i < 256; ++i)
	{
		for (int j = 0; j < 256; ++j)
		{
			int x = (int)(4.0f * (float)i / 256.0f);
			int y = (int)(4.0f * (float)j / 256.0f);
			textureData[i*256 + j] = gridColors[x, y];
		}
	}

	texture_.SetData<Color>(textureData, 0, 256 * 256, SetDataOptions.Discard);
}



So it basically contains a 5x5 grid of colors, and then just "scales" this value to fit into a larger texture.

In my Draw() method I then just call effect.Parameters["Texture"] = texture;

When I do this I get an error:

Additional information: The operation was aborted. You may not modify a resource that has been set on a device, or after it has been used within a tiling bracket.

I'm obviously just supposed to do things a bit differently, could someone clarify how I'm supposed to do this?

Thanks



------------------------------------------------------------
First differentiate, then integrate. This should give you the answer. And if all else fails, let epsilon > 0.

 User Rating: 1204   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

The error you are getting is that the texture is currently in use. You can make sure that nothing is using your texture by clearing the texture registers before calling SetData. The texture registers can be set directly with GraphicsDevice.Textures[], which contains GraphicsDevice.GraphicsDeviceCapabilities.MaxSimultaneousTextures registers.

That said, you might want to move this code to a shader, and change your dynamic texture to a render target. Event shader model 2.0 can handle 25 colors as effect parameters, and it would save your CPU from having to loop through the pixels.

Finally, if this texture is only ever going to display 5x5 color blocks, you might just want to write a material shader that chooses the grid color procedurally from the uv's, rather than sampling a texture.

 User Rating: 1000   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Quote:
Original post by ColdEquation
The error you are getting is that the texture is currently in use. You can make sure that nothing is using your texture by clearing the texture registers before calling SetData. The texture registers can be set directly with GraphicsDevice.Textures[], which contains GraphicsDevice.GraphicsDeviceCapabilities.MaxSimultaneousTextures registers.

That said, you might want to move this code to a shader, and change your dynamic texture to a render target. Event shader model 2.0 can handle 25 colors as effect parameters, and it would save your CPU from having to loop through the pixels.

Finally, if this texture is only ever going to display 5x5 color blocks, you might just want to write a material shader that chooses the grid color procedurally from the uv's, rather than sampling a texture.


It's eventually going to be more complicated, as I'm going to be writing text into each grid cell as well. It's also eventually going to write color grids that are layed out differently, the 5x5 was just a simple example so that I could get a proof of concept. I'll look into the GraphicsDevice.Textures[] member, thanks!



------------------------------------------------------------
First differentiate, then integrate. This should give you the answer. And if all else fails, let epsilon > 0.

 User Rating: 1204   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

All times are ET (US)

Post Reply
 Last Thread Next Thread 
Forum Rules:
You may not post new threads
You may post replies
You may not edit your posts
You may not use HTML in your posts
Jump To:
Administrative Options: