Hot answers tagged

49

This looks like the bottom layer of a volume texture that many games these days use to perform color correction. The idea is that the final RGB screen color, after rendering and tonemapping, is used as a texture coordinate to index into this texture, and the color found in the texture replaces the original color. This allows artists to arbitrarily modify ...


33

Internally, GPUs never run one instance of a pixel shader at a time. At the finest level of granularity, they are always running 32-64 pixels at the same time using a SIMD architecture. Within this, the pixels are further organized into 2x2 quads, so each group of 4 consecutive pixels in the SIMD vector corresponds to a 2x2 block of pixels on screen. ...


25

The physical basis of the colors of an oil slick is iridescence, and also related to Newton's rings. Specifically, the thickness of the oil layer is on the order of the wavelength of light. Since light reflects from both the top and bottom surface of the oil, at any given wavelength, at some angles the two reflections will be out of phase and cancel each ...


18

The first step is to tell the graphics card we need the stencil buffer. To do this when you create GraphicsDeviceManager we set the PreferredDepthStencilFormat to DepthFormat.Depth24Stencil8 so there is actually a stencil to write to. graphics = new GraphicsDeviceManager(this) { PreferredDepthStencilFormat = DepthFormat.Depth24Stencil8 }; The ...


17

Many rules for micro-optimising shaders are the same as for traditional CPUs with vector extensions. Here are a few hints: there are built-in test functions (test, lerp/mix) adding two vectors has the same cost as adding two floats swizzling is free It is true that branches are cheaper on modern hardware than they used to be, but it is still better to ...


14

Pseudo random numbers in a pixel shader aren't easy to obtain. A pseudo random number generator on the CPU will have some state which it both reads from and writes to, on every call to the function. You can't do that in a pixel shader. Here's some options: Use a compute shader instead of a pixel shader - they support read-write access to a buffer, so you ...


14

Actually no, the 'job' of the geometry shader (GS) is primative evaluation. Geometry shaders can tesselate, but they are limited by a) an in-process upper bounds on the number of output elements, and b) execution within a single shader...of course shader instancing aleviates the 2nd issue, but overall geometry shaders are more effective at primative ...


12

First, it helps to know that GPUs always evaluate fragment/pixel shaders on 2x2 blocks of pixels at a time. (Even if only some of those pixels ultimately need to be drawn, while others are outside the polygon or occluded - the unneeded fragments are masked out instead of being written at the end). The screenspace derivative of a variable (or expression) v ...


11

You can use your pixel shader approach but it is incomplete. You will also need to add some parameters to it that inform the pixel shader where you want your stencil to be located. sampler BaseTexture : register(s0); sampler MaskTexture : register(s1) { addressU = Clamp; addressV = Clamp; }; //All of these variables are pixel values //Feel free to ...


11

My impression is that this color is essentially a fog color, thinking of the water as being a fog volume with a shiny surface. The simplest thing to do is probably to just let your artists pick a shallow color and a deep color, and lerp between them based on the depth. Something like: lerp(shallowColor, deepColor, saturate(depth * depthColorScale)); Here ...


11

Well, a lot of stuff happens between the Vertex and Pixel shaders, but before we get into that, I'm worried that you may be a bit confused about what the Vertex and Pixel shaders do. So let's quickly go through the entire Direct3D 9 programmable pipeline again. This nice chart shows the entire pipeline, and you should print it out for reference. (feel free ...


10

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 ...


10

Well, when asked to design any shader, we should start by breaking things down to smaller problems. And just as note the glittering effect doesn't actually makes the shader looks good, but the overall lighting and effect, using only one of them won't look as good. First of all let us state what is not directly part of the shader: The shadowing is not ...


8

Short answer: yes, that formula is correct. Longer answer: if you think of a texture as being a grid of little squares, one for each texel, the actual color value stored in the texture can be thought of as being located at a point at the center of each texel square. So, in UV coordinates where the texture ranges from (0, 0) to (1, 1), the color samples are ...


8

I believe what you are looking for is called Deferred Rendering. It is a rendering technique that scales extremely well with a lot of lights, so well that it can used for dynamic indirect illumination. That means 1000s of lights on the screen. It is basically a technique in which you first render all your geometry data(position, normal, depth) into an ...


8

As far as I know, Unity uses Cg (which is deprecated by NVIDIA since 2012, I have no idea why they still use it) as its shader language (which is really similar to HLSL) instead of HLSL or GLSL as stated here: In Unity, shader programs are written in a variant of HLSL language (also called Cg but for most practical uses the two are the same). Later on, ...


7

When blurSize gets too large, the texture samples will be spaced so far apart from each other that some pixels that should be in the blur will be missed. This can easily lead to aliasing. To do really large blurs you would have to sample a lot more pixels, but since that's slow (and/or impossible depending on the limit for texture samples on your GPU), you ...


7

Wouldn't setting your texture's edge mode to clamp (instead of repeat) solve this issue?


7

The fragment shader is executed for each fragment with a single uv for this fragment which will probably never fall perfectly on 1. You could map the target area roughly to the width of a render target fragment. Eg something like: abs(hyp - 1) * CircleRadiusInPixel < BorderWidthInPixel*0.5 Further explanation: Your gpu rasterizes the triangles and ...


7

Use several constant buffers and group variables together based on how often they change. If your variables are fairly static ( or just huge ) you may be better off converting values into a texture and extracting them in the shader.


7

it doesn't generate a quad, instead it generates a fullscreen triangle. The outputs end up as: output[0].texcoord = float2(0,0); output[0].position_cs = float4(-1, 1, 0, 1); output[1].texcoord = float2(2,0); output[1].position_cs = float4(3, 1, 0, 1); output[2].texcoord = float2(0,2); output[2].position_cs = float4(-1, -3, 0, 1); It goes beyond the ...


6

Firstly you can do texture fetching inside conditional blocks in HLSL. tex2Dlod() and tex2Dgrad() will work fine inside one. It's just tex2D() that won't compile, and you can work round that by computing ddx() and ddy() outside the conditional and using tex2Dgrad(). To reliably stop the texture fetch (or any other block of code) being executed in HLSL, use ...


6

Bitwise operations and integer operations were new for SM 4.0/DX10. As your error says: Bitwise operations not supported on legacy targets. You'll have to target DX10. Alternatively, this blog post suggests using a texture to map results of AND,OR,XOR to different color channels. bitwise operators texture: AND,OR,XOR Seems plausible, and might be ...


6

SV_Position in the pixel shader gives you the the center point of the pixel being shaded, in a [0, bufferWidth] x [0, bufferHeight] range. (It can also be used in centroid mode with MSAA to get the centroid of the covered samples.) This value can be passed directly to Texture2D.Load to retrieve the corresponding pixel from a texture the same size as the ...


6

The assembler code in the ATI presentation seems like it contains instructions specifically targeted for the GPU, but is this merely a hypothetical example created for the purpose of conceptual understanding, or is this code really generated during shader compilation? PIX and other vendor-specific graphical debugging tools can show you the assembly ...


Only top voted, non community-wiki answers of a minimum length are eligible