8 C
New York
Friday, November 21, 2025

Does it make sense to make use of a compute shader with Dispatch(1,1,1) and Numthreads(1,1,1) to attract a cone?


I used to be engaged on this concept of ​​drawing a cone rotating in regards to the y-axis with a parametric equation, utilizing a calculus shader with a operate like:

CS(SV_GroupID, uint3 nDTid : SV_DispatchThreadID, SV_GroupThreadID). 

To keep away from a number of renders, I used to be utilizing a check in nDTid draw solely on a sure nDTid.xy interval.

I discovered this difficult and at last determined to make use of Dispatch(1,1,1) and numthreads(1,1,1)that get rid of the requirement of nDTid interval check and the cone is drawn solely as soon as.

What I’ve observed is an erratic body fee which I assume is determined by the variety of pixels drawn.

This turns into extra of an train than one thing relevant to my ordinary mission, however is it fascinating to restrict it? Dispatch and numthread How did I do with computing shaders? How can I cut back this erratic body fee?

Beneath is a code instance and the rendered consequence.

Making ready fixed shader

void UpdateShaderConstantCone()
{
    FLOAT R = 10.0f;//radius at cone base
    FLOAT H = 30.0f;//top of Cone
    XMVECTOR U = XMVectorSet(R, 0, 0, 1);//vector for airplane of base cone
    XMVECTOR DirC = XMVectorSet(0, 0, H, 360);//preliminary course of cone alongside Z axis
    XMMATRIX M = XMMatrixRotationY(RotCone);
    M.r(3) = XMVectorSet(20, 50, 0, 1);//place of the cone high = purple ball middle
    gCBCone.mWorld = XMMatrixTranspose(M*gVP);//set World.View.Perspective mat
    XMStoreFloat4(&gCBCone.DirC, DirC);
    XMStoreFloat4(&gCBCone.U, U);
    FLOAT C = 0.5f * 255.0f;
    gCBCone.Coloration = XMFLOAT4(0 * C, C * 255 , C * 255 * 255, 1.0f);
    gpDC11->UpdateSubresource(gpCBBufferCone, 0, NULL, &gCBCone, 0, 0);
    gpDC11->CSSetConstantBuffers(dwSlotCone, 1, &gpCBBufferCone);
}

Compute shader settings. As an alternative of progressing alongside the axis of the cone and drawing a circle at every step, which required a change/illumination calculation per pixel of the drawn circle, I loop first for every pixel of the circle on the base and draw a line from the highest cone to this pixel. This requires just one transformation/illumination calculation per line drawn. I can say that it isn’t actually environment friendly, for the reason that high pixels may be drawn a number of occasions. This shader doesn’t draw the bottom of the cone and didn’t carry out a z check. For some causes, the lighting makes the cone flip reddish as an alternative of grey, which I have not mounted but.

#outline SM_SCREENX 1920.0
#outline SM_SCREENY 1080.0
RWTexture2D<uint> UAVDiffuse0  : register( u0 );

cbuffer cbCone: register(b6)
{
    matrix WVP;
    float4 DirC;//course of cone, w=Top
    float4 U;//vector airplane of circle w=radius / Top
    //Notice the airplane requires two vectors UP(R,0,0) and VP(0,R,0).
    //so U is used as U.xyy is UP and U.yxy is VP
    float4 C;//shade precalculated half
};

#outline CPInc 2*3.14159265359f
static const float3 f3_u3 = float3(255,255*255,255*255*255);
static const float3 LDir = normalize(float3(-1,-1, 1));//Mild dir
static const float3 LCol = float3(0.5f,0.5f,0.5f)*f3_u3;//ambient shade 0.5f,0.5f,0.5f
static const float2 Sd2a = float2(SM_SCREENX, SM_SCREENY)*0.5f;
static const float2 Sd2m = float2(SM_SCREENX, -SM_SCREENY)*0.5f;
(numthreads(1,1,1))
void CS_PostDeferred( uint3 nGid : SV_GroupID, uint3 nDTid : SV_DispatchThreadID, uint3 nGTid : SV_GroupThreadID )
{
    float4 P = mul(float4(0,0,0,1),WVP);
    P/=P.w;
    P.xy=P.xy*Sd2m+Sd2a;
    float4 E = float4(0,0,0,1);
    float okay=CPInc/DirC.w;//angle increment to attract the circle
    float Angle = 0;
    float2 sd2aP = Sd2a-P.xy;//precalculate delta for line tracing
    for ( int a = 0; a < DirC.w; a++ )//make a circle at top h
    {
        float3 D = U.xyy*cos(Angle)+U.yxy*sin(Angle);//calculate circle pixel pos
        E.xyz = DirC.xyz+D;//add base place from high eg 0,0,Top
        Angle+=okay;
        E = mul(E,WVP);//convert to display house
        E/=E.w;
        E.xy=mad(E.xy,Sd2m,sd2aP);//convert pixel place E to distance in pixel from high cone
        float2 dXY = abs(E.xy);//calculate longest display pixel path
        int L = (dXY.x>dXY.y)?dXY.x:dXY.y;//decide variety of step
        float3 CL = C*dot(normalize(D),LDir)+LCol;//calculate ambient lighting
        //we assume the conventional D alongside the road is fixed
        E.xy/=L;//convert E to pixel path step
        float2 XY = P.xy;
        for (int l=0;l<L;l++)
         (uint(CL.g)&0xFF00) 
    }
}
    

Screenshot

Another answer is to make use of a geometry shader to generate a triangle from the highest on two successive pixel circles.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles