Hot answers tagged pixel-shader
15
Yes, you can implement Gaussian blur in one pass, by sampling all n^2 pixels in the kernel (for kernel width n). It's usually faster to run it on the rows and columns in two passes, since then you have O(n) pixels to sample rather than O(n^2). This is not an approximation, since Gaussian blur is mathematically separable.
12
I think everybody's giving way too complicated solutions to this problem..
So first, we have the card (or whatever you want to draw), depicted here by (a).
Next, we take a copy of it, fill it in black and run a gaussian blur on it (b). All of this happens in photoshop or whatever your favorite art tool is.
Next, in-game, when we want to draw the card, ...
7
Graphics hardware can perform early depth-based culling of fragments before computing their color value (in other words, before running your fragment shader). Consequently, if you utilize any features that would affect that, such as discard, alpha-testing, or manipulating gl_FragDepth the hardware's ability to do that optimization will be compromised since ...
6
Unfortunately no. You cannot read from the current render target in a shader. You have two basic options:
If the operation you would do with the read fragment color is simple enough that you can achieve it using blend state operations, you can use those.
You can pass the color data you need into shader via some other resource, for example, a texture ...
6
What you are probably looking for is a form of edge detection before, after, or even before and after your Gaussian blur. Maybe a version of Sobel Edge Detection could work. I know your game is 2D but if the wiki page is too rough here's a tutorial on how to get it working in the UDK that might translate over better.
Your edge detection only really has to ...
5
BlendState.AlphaBlend uses premultiplied blending so check out this for why you can a value of 0 for alpha and still have the pixel not be transparent (especially the last paragraph).
So I believe what you need is in your shader is (I'm bad with shaders so take this with a grain of salt).
c.a = clamp(c.a - 0.05, 0, 1);
c.r = c.r * c.a;
c.g = c.g * c.a;
c.b ...
5
First of all, I think you meant to ask whether a 2-bit texture will be 16 times faster than a 32-bit texture, not slower.
The answer is: sometimes. Performance of graphics hardware is a complex topic. It is a concurrent, pipelined system that contains many different processes operating simultaneously and passing data to one another.
In general, the speed ...
4
Since grayscale is a post-process effect it is very easy to integrate. You just have to make sure that before drawing the scene you set a rendertarget. This means that everything is being drawn to someplace in the videocard memory and not directly to the screen. After that you save the texture you get from the rendertarget and draw that as a full screen quad ...
4
Maybe. The problem you'll likely run into with replacing math functions with a texture lookup is precision and range. The texture can only be so large, and you have to decide what region of the function's domain it's going to cover and how finely subdivided in the domain it needs to be. Then, with standard texture formats you have only 8 bits to encode ...
4
SV_Position gives you the position in screen coordinates, not in a [0,1] range though but basically in pixel coordinates.
The range will correspond to the D3D11_VIEWPORT you set, possibly something along the lines of:
D3D11_VIEWPORT viewport = {0};
viewport.Width = 1280;
viewport.Height = 720;
So in order to get a [0,1] range again, for the colors, you ...
4
You can't draw with multiple vertex or pixel shaders at the same time. GPUs don't work that way. To render objects with different shaders, you sequentially set one shader, draw the objects for it, set another shader, draw its objects, etc.
for each (vertex, pixel) shader pair:
set vertex shader in the device context
set pixel shader in the device ...
4
Taking your example, you have a step function of the distance, which produces a perfectly hard (aliased) edge. A simple way to antialias the circle would be to turn that into a soft threshold, like:
float distFromEdge = 1.0 - dist; // positive when inside the circle
float thresholdWidth = 0.01; // a constant you'd tune to get the right level of softness
...
3
Might I suggest that the simplest (and so probably the best) solution is not to use a custom shader at all. Simply have two sprites.
The first sprite can be your "base" layer containing parts of the unit common to all teams. This can be colour with an alpha channel.
And the second layer would contain only the team-coloured elements in grayscale with an ...
3
You should apply your shader AFTER your sprite has been rotated.
If the whole scene has not been shaded yet and your sprites are actually pixelated, what you need is some kind of post-FX filter for your whole scene. Averaging regions of pixels will work ok. It's not exactly what you intend (it'll look kinda dithered when moving/rotating) but it might do the ...
2
The first one isn't too hard. If you render an alpha channel for your hand of cards (which could be as simple as black for a pixel with a card in it, and white for transparent), you can perform any kind of blur you want on just the alpha channel, and then offset it and use that to control the lighting of the table. (Presumably, if you don't have a Z-buffer, ...
2
UPDATE
Basic maths: if your input is a variable called X and lies in the [a,b] interval, but you'd like to have it translated to the [c,d] interval, the common sense way of doing that is via a linear/affine transformation (affine operators are functions of the form AoX+B where o is a multiplication like operation - A and X could be matrices or other ...
2
You could achieve this through (at least) two different methods. One is to use the stencil buffer, this way you could draw arbitrary shapes in pictures and use those as masks. The other option is to make the mask in a vertex buffer and thus draw only the wanted part directly in the first call.
Both would work fine, in this case the second type would ...
2
As always for performance questions the most accurate answer is to try it out on your target hardware and measure what happens.
In your case it's probably not a bad thing to do. In fact there's a chance it will help performance by saving on memory bandwidth. It will also add shader instructions though, so it's not always a performance benefit.
Even when ...
2
I found a complete solution to this issue.
Please notice size is now 10,000,000
Here is the new code snippet:
private const SIZE : int = Math.pow(10, 7);
private var testVectorInt : Vector.<Number>; //gotta use Number (not int)
private var testVectorInt2 : Vector.<Number>; //Need separate vectors for I & O
...
2
Instead of having the highlight color be an effect parameter, you probably want to have it be a vertex parameter. The vertices of each individual sprite can then store the highlight color for that sprite, and you can batch them all together and draw them in the correct Y-order.
It looks like the XNA SpriteBatch system already supports vertex color (there's ...
2
You need to think of the built-in HLSL functions that let you perform tests (min, max, saturate…) or interpolations (lerp, smoothstep…). Many times they map to a single assembly instruction on the hardware.
Also, as Adam mentioned in his answer, it’s best to try to parallelise operations. Adding two float has exactly the same cost as adding two float4.
...
2
What worked for me with text outlining was:
Pick the outline colour
Draw the text at (x-1, y-1) with outline colour
Draw the text at (x+1, y-1) with the outline colour
Draw the text at (x-1, y+1) with the outline colour
Draw the text at (x+1, y+1) with the outline colour
Draw the text at (x, y)
You can do the same thing -- all you need to do is draw the ...
1
Your line:
existing.rgb *= (1 - addition.a) + addition.rgb;
does
existing.rgb = existing.rgb * ((1 - addition.a) + addition.rgb);
what you probably wanted is
existing.rgb = existing.rgb * (1 - addition.a) + addition.rgb;
1
My first suggestion would be to combine both the x and y variables and the screen resolution into a float2 which then lets you do the distance calculation in one line. That should get rid of a few instructions.
I'd also suggest passing the square of the screenZoom as a constant if you only use it squared in the calculations.
float2 lightPosition;
float2 ...
1
Based on your code example, it looks like you use the alpha channel in your bitmask to determine whether or not to draw a pixel. It also looks like you treat an alpha of 0 as "draw the pixel" and anything > 0 to "not draw the pixel." I'm also going to assume that in your bitmask, you can make anti-aliased edges in the alpha channel where the alpha value ...
1
can I send the instance data straight to the pixel shader somehow?
What would that mean, exactly? The values your fragment (or pixel if you insist) shader gets are per-vertex values interpolated across the face of the primitive, and constant uniform values that do not change within a draw call.
There is no way to magic a vertex stage input into a ...
1
Multiple discrete output textures. You'd set up several individual render targets, each in exactly the same way as you set up one render target, then specify a list of them rather than a single one when you bind them to be drawn to.
In DX11, the render targets can be different formats and color depths. I'm not aware of any restrictions on this. You can ...
1
Sorry ahead of time for not posting any code, but your problem might be HSV itself.
Hue is represented as a circle, called a Color Wheel. In HSV, the color wheel is based on color frequencies, evenly divided out across the visual spectrum. In other words, The Sextants your solution assumes exist are based on evenly distributing the 0-359 degrees over this ...
1
Finally I figured it out.
This shader worked for me.
// Amount to shift the Hue, range 0 to 6
float Hue;
float Brightness;
float Contrast;
float Saturation;
sampler Samp : register(S0);
float3x3 QuaternionToMatrix(float4 quat)
{
float3 cross = quat.yzx * quat.zxy;
float3 square= quat.xyz * quat.xyz;
float3 wimag = quat.w * quat.xyz;
square ...
1
I don't have exact numbers as I have not done any benchmarks.
That said, in my engine for compatibility with DirectX 9 I have one sampler per texture, so there are quite a few, and they are not always referenced in the shader.
So far I have not noted any performance degradation compared to before we added DX9 support.
As long as you create your sampler ...
Only top voted, non community-wiki answers of a minimum length are eligible