0
\$\begingroup\$

For my game that I'm developing I want to have my cards dynamically drawn so if there is any changes done to them it could be drawn on the card itself.

What I have currently is that the cards are drawn to a RenderTarget at their originally designed size of 2100x1480 px. (I'll probably for game purposes have to scale that down to 1050x740 for its native size, but was designed that way to have 600 dpi for physical versions).

Anyway the way I want to do it is that after drawing everything to the RenderTarget I scale it down and store that scaled down texture to the card's texture (which will be 164 x 115 for the playing size, the 1050 x 740 is the zoomed in size that I want so players can view what the card does and stats). The issue that I'm facing is that the way I currently tried to do it was far too slow taking over 9 seconds to scale it down after drawing the original size to the RenderTarget.

Is there a way to scale a texture via GPU rather than by CPU? I'm currently using a CPU method as I'm new to working with textures and drawing.

\$\endgroup\$
  • 1
    \$\begingroup\$ Like the last time you brought this up, you need to narrow the scope of the question by only including the code that's causing the slow down. \$\endgroup\$ – MichaelHouse Feb 2 '15 at 20:22
  • \$\begingroup\$ I figured out the original issue that I had with the question that I asked before for the timing issue. This time trying to figure out how to make it faster. \$\endgroup\$ – Thomas Morse Feb 2 '15 at 20:29
  • \$\begingroup\$ Wasn't sure if I should have re-posted the question since it was different from what I originally asked from my question that is like this one since I had figured out what was causing the timing issue that I was seeing. I apologize if I did wrong there. :( \$\endgroup\$ – Thomas Morse Feb 2 '15 at 20:47
  • 1
    \$\begingroup\$ The problem is that it's essentially a "please code review my code and tell my how I can make it faster". These types of questions are not well suited for the stackexchange network. I suggest you ask on a more open-ended discussion type forum like gamedev.net or reddit.com/r/gamedev \$\endgroup\$ – MichaelHouse Feb 2 '15 at 21:14
  • 1
    \$\begingroup\$ Ok well I was more wondering if there is a GPU way to do it or if a programmer has to do it by using CPU to scale a texture. :S \$\endgroup\$ – Thomas Morse Feb 2 '15 at 21:20
1
\$\begingroup\$

I'm not sure I understand the question correctly, but if you are asking only about how to draw the texture with a different resolution than the original, there is no need to change the texture itself.

XNA provides several overloads of SpriteBatch.Draw() method . Some take destination rectangle as the second parameter and allow you to specify the area in which the texture will be inscribed. Others contain scale parameter of type float or Vector2, which allows you to draw texture scaled proportionally or not. All rasterization operations performs GPU.

If this is the not answer to your question, please rephrase it more concretely, and best of all insert code snippet.

\$\endgroup\$
0
\$\begingroup\$

Simply drawing the texture to a smaller quad will scale it down automatically for you; there's no rule stating that the dimensions of the quad you draw a texture to have to be the same as the dimensions of the texture.

In your specific case, you can take a 2100x1480 texture and draw a 164x115 quad using it, and the MinFilter you specify in your sampler state (or shader code) will take effect, automatically scaling the texture down.

However, be aware that a typical linear MinFilter operates on a 2x2 block of texels, so it's only good for reducing to 50% of the size of the original; any lower and it will introduce noise.

The solution you need is therefore mipmapping. Instead of creating your texture with a single miplevel, you create it with a full mipmap chain, then use hardware mipmapping to generate the submips for it. Then when drawing you just specify the miplevel you wish to draw from, depending on the size of the quad you're drawing, and the GPU will do the rest.

Further reading: http://blogs.msdn.com/b/shawnhar/archive/2009/09/14/texture-filtering-mipmaps.aspx

\$\endgroup\$
0
\$\begingroup\$

Simply use the SpriteBatch.Draw overload which allows you to specify the destination rectangle:

public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
{
    spriteBatch.Begin(SpriteSortMode.Deferred);

    spriteBatch.Draw(
         texture: texture, 
         destinationRectangle: destinationRectangle,
         sourceRectangle: null, 
         color: Color.White);

    spriteBatch.End();
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.