Will this anyhit shader ignore all hits? - raytracing

I am learning DXR (DirectX Raytracing), and have a very simple question as follows:
I am using Unity's HDRP RTX, and I have a simple "anyhit" shader as below:
[shader("anyhit")]
void AnyHit(inout RayPayload payload : SV_RayPayload, BuiltInTriangleIntersectionAttributes attribs : SV_IntersectionAttributes)
{
IgnoreHit();
}
I thought it should ignore all hits, and thus no "closesthit" shader will be executed. However, I still see "closesthit" shader executed. The behavior is same even if I delete this "anyhit" shader totally. Why?

IgnoreHit only ends the current anyhit shader.
https://learn.microsoft.com/en-us/windows/win32/direct3d12/any-hit-shader
To ignore closesthit shader, configure the flag to RAY_FLAG_SKIP_CLOSEST_HIT_SHADER in your TraceRay function.
https://learn.microsoft.com/en-us/windows/win32/direct3d12/ray_flag

Related

Write into single colorAttachment in one drawcall

I created a fbo with two-color attachments i.e., colorAttachment0 and colorAttachment1.
so my question is I want to draw into single colorattachment in one draw call.
in my shader i tried this but not working:
if(attachmentVal==0) //I am sending the attachmentVal as uniform
{
outColor = vec4(1,0,0,0);
}
if(attachmentVal==1)
{
outColor2 = vec4(1,1,1,0);
}
But it is not working.
so my question is, is it possible to render into a single attachment in a single draw call by controlling the selection of the attachment??
If possible please give me a solution to this problem.
Thank you.
Use glDrawBuffers() to select which attachments are active for the current draw.

Does webgl drawArray() empty/discard the buffers?

Trying to speed up the display of many near-identical objects in WebGL, I tried (naively, I guess), to re-use the buffers content. In the drawing routine of each object, I have (somewhat simplified):
if (! dataBuffered) {
dataBuffered = true;
:
: gl stuff here: texture loading, buffer binding and filling
:
}
// set projection and model-view matrices
gl.uniformMatrix4fv (shaderProgram.uPMatrix, false, pMatrix);
gl.uniformMatrix4fv (shaderProgram.uMVMatrix, false, mvMatrix);
// draw rectangle filled with texture
gl.drawArrays(gl.TRIANGLE_STRIP, 0, starVertexPositionBuffer.numItems);
My idea was that the texture, vertex, and texture coordinates buffer are the same, but the model-view matrix changes (same object in different places). But, alas, nothing shows up. When I comment the dataBuffered = true, it's visible.
So my question is, does drawArray() discard or empty the buffers? What else is happening? (I'm working along the lessons at learningwebgl.com, if that matters.)
Short answer is, Yes, you can reuse all the state you've set up for more than one gl.drawArrays().
http://omino.com/experiments/webgl/simplestWebGlReuseBuffers.html is a little example where it just changes one uniform float (Y-scale) and redraws, twice for every tick.
(In this case there's no textures, but some other state stays sticky.)
Hope that helps!
uniformSetFloat(gl,prog,"scaleY",1.0);
gl.drawArrays(gl.TRIANGLES, 0, posPoints.length / 3);
uniformSetFloat(gl,prog,"scaleY",0.2);
gl.drawArrays(gl.TRIANGLES, 0, posPoints.length / 3);

Getting shadows to work in Three.js custom shader

I'm trying to get shadows to work in a custom shader in Three.js. I've tried to add these into my codes:
In uniforms:
THREE.UniformsLib["shadowmap"]
In the fragment shader:
THREE.ShaderChunk["shadowmap_pars_fragment"]
THREE.ShaderChunk["shadowmap_fragment"]
In the vertex shader:
THREE.ShaderChunk["shadowmap_pars_vertex"]
THREE.ShaderChunk["shadowmap_vertex"]
which works. The object can receive shadows.
However, it cannot cast shadows. Does anyone know what other bits of codes are needed?
I believe that you need to mark each object as casting and receiving shadows
I think its just
obj.castShadow = true;
obj.recieveShadow = true;

DirectX: Get Filter Type in Shader

I am trying to get the filter type set in the sampler stat in the Shader code. I am not getting a way to retrieve that. Can someone suggest me a way to do that??
Texture2D InputTexture;
SamplerState Sampler;
float4 PSMain(float2 pos: TEXCOORD, float4 SVP : SV_POSITION) : SV_TARGET {
float4 image = InputTexture.Sample(Sampler, pos);
//How to get Filter type - Sampler.Filter???
return image;
}
Doesn't work the above option for me. Can someone help me out?
Thanks.
You can't do that, the way to go if you want altering behaviour in your shader is to use either shader constants and branch based on them, or different versions of your shader.

Hardcoding GLSL texture sampler2D

In my fragment shader, I have the line
gl_FragColor = texture2D(texture, destinationTextureCoordinate) * destinationColor;
Where texture is a uniform of type sampler2D. In my code , I always set this to the value '0'.
glUniform1i(_uniformTexture, 0);
Is it possible to skip the call to glUniform1i and just hardcore 0 in the fragment shader? I tried just replacing texture with 0 and it complained about not being a valid type.
I'm not entirely sure what you're trying to achieve, but here are some thoughts:
sampler2D needs to sample a 2D texture, as the name indicates. It is a special GLSL variable, so the compiler is right to complain about 0 not being a valid type when fed into the first parameter of texture2D.
Unless you are using multiple textures, the second parameter to glUniform1i should always be 0 (default). You can skip this call if you are only using a single texture, but it's good practice to leave it in.
Why do you need a call to texture2D if you just want to pass the value 0? Surely you can just do gl_FragColor = destinationColor. This will color your fragment based on the vertex shader output from gl_Position. I'm not sure why you are implementing a texture if you don't plan on using it (or so it seems).
EDIT: Code to send two textures to the fragment shader correctly.
//glClear();
// Attach Texture 0
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, _texture0);
glUniform1i(_uSampler0, 0);
// Attach Texture 1
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, _texture1);
glUniform1i(_uSampler1, 1);
//glDrawArrays();
You need a layout, like this:
#version 420
//#extension GL_ARB_shading_language_420pack: enable Use for GLSL versions before 420.
layout(binding=0) uniform sampler2D diffuseTex;
In that way you are getting a binding to the sampler uniform 0. I think that is what you want. But keep in mind that when you are binding uniforms they got incremental values from zero, so be sure what you want to bind. The idea to bind uniform variables via the glUniform1i() function is to ensure the correctness of your code.
Source: http://www.opengl.org/wiki/GLSL_Sampler#Version_4.20_binding

Resources