Jump to content

  • Log In with Google      Sign In   
  • Create Account

directx user

Member Since 03 Jun 2012
Offline Last Active Apr 27 2015 10:45 AM

Topics I've Started

X3000 using enum in hlsl shader

14 December 2013 - 07:00 PM



I am building an Algorithm, which is suposed to run on both, the cpu and the gpu. It would be really usefull if I could just use my enum in the shaders header file and could just include it in both, my cpp and the hlsl shader itself (enum determines which function is going to be executed). But Unluckily I always get the error X3000 unexspected token when i try to use enums in my shader.


On the internet I read that enum is a reserved keyword for HLSL though I found very few examples of HLSL Code using enums, it should be possible or am I wrong?


Not even this hlsl file compiles:

enum { test1, test2};

[numthreads(8, 1, 1)]
void CS_NEW( uint ID : SV_DispatchThreadID  )

I am using the Inbuild HLSL Compiler from Visual Studio 2012, am I the only one having this Issue?

Exceptions using createcomputeshader

06 October 2013 - 05:48 PM

Hi guys,


I am working in Visual Studio 2012 and I need to create a computeshader, which is with its 250 lines quite big, so i decided to precreate it with the inbuild HLSL compiler by generating a header file. The compiler didnt complained at all and the binary Array just got created, but if i want to bind the data to a shader object uisng the line:

device->CreateComputeShader(backpropshader, sizeof(backpropshader), NULL, &berechner);

backpropshader being the binary buffer in the header file, the Application raises an acess violation Exception. Does anyone know how this Exception could arise?

I am also concerned about the binary array to be 23 thousand lines long, is that normal?


the Shader is suposed to use the Backpropagation algorithm on several neuronal systems parallely:

This is the Shader reduced to a few lines, I hope its understandable though many of the variablenames are german.


cbuffer konstanten : register(b0)
    uint systemcount;

    uint neuronencount;
    uint ausgaenge; 
    uint eingaenge;
    uint vorgabensegment;
    uint vorgabencount;

    uint aufgabencount;

    uint lernschritte;
    uint lernstaerke;
struct Verbindung
    uint start;
    uint ende;
    float gewicht;
struct neurondaten
    float eingabe;
    float ausgabe;
    uint transfermodus;
    float fehler;

StructuredBuffer<float> Vorgabentemplate : register(u0);

RWStructuredBuffer<Verbindung> Verbindungen : register(u1);
RWStructuredBuffer<neurondaten> Neurone : register(u2);

void PROPAGATE(uint basisvorgabe, uint basisneuron, uint basisverbindung, uint endverbindung) 
    Neurone[basisneuron].ausgabe = 1.0f; //Biasneuron gleich setzen
    for (unsigned int einschreib1 = 1; einschreib1 < eingaenge; einschreib1++)
        Neurone[basisneuron + einschreib1].ausgabe = Vorgabentemplate[basisvorgabe + einschreib1 - 1];
    for (unsigned int einschreib2 = 1 + eingaenge; einschreib2 < neuronencount; einschreib2++)
        Neurone[basisneuron + einschreib2].eingabe = 0;
        Neurone[basisneuron + einschreib2].fehler = 0;
    //Neurone sind im Grundzustand

    for (unsigned int i = basisverbindung; i < endverbindung; i++)
        if (Verbindungen[i].start == Verbindungen[i].ende)
            switch (Neurone[Verbindungen[i].start].transfermodus)

            Neurone[Verbindungen[i].ende].eingabe += Neurone[Verbindungen[i].start].ausgabe * Verbindungen[i].gewicht;

[numthreads(128, 1, 1)]
void BACKPROPAGATE( uint ID : SV_DispatchThreadID  )
    if (ID < systemcount)
        float restfehler = 0;
        uint basisneuron = neuronencount * ID;
        uint basisverbindung = aufgabencount * ID;
        uint endbasisverbindung = aufgabencount * (ID + 1);
        for (unsigned int durchgang = 0; durchgang < lernschritte; durchgang++)
            restfehler = 0;
            uint basisvorgabe = 0;
            for (unsigned int vorgabenzaehler = 0; vorgabenzaehler < vorgabencount; vorgabenzaehler++)
                PROPAGATE(basisvorgabe,basisneuron,basisverbindung, endbasisverbindung);
                for (unsigned int fehlerstart = neuronencount - ausgaenge; fehlerstart < neuronencount; fehlerstart++)
                    Neurone[basisneuron + fehlerstart].fehler = Neurone[basisneuron + fehlerstart].eingabe - Vorgabentemplate[basisvorgabe + eingaenge];
                    restfehler += Neurone[basisneuron + fehlerstart].fehler * Neurone[basisneuron + fehlerstart].fehler;
                for (unsigned int i = endbasisverbindung - 1; i-- > basisverbindung;)
                    if (Verbindungen[i].start == Verbindungen[i].ende) //refreshe den fehler
                        switch (Neurone[Verbindungen[i].start].transfermodus)

                        Verbindungen[i].gewicht -= lernschritte * Neurone[Verbindungen[i].ende].fehler * Neurone[Verbindungen[i].start].ausgabe;
                        Neurone[Verbindungen[i].start].fehler += Neurone[Verbindungen[i].ende].fehler * Verbindungen[i].gewicht;
                basisvorgabe += vorgabensegment;



adress mode issues

23 December 2012 - 10:10 AM


iam currently having some issues with my sample texture to wrap proper on my geodesic sphere, actually it seems like the address mode of my sampler doesnt works. I need the texture to repeat on the other side of the sphere, so i assigned a wrap samplerstate:
D3D11_SAMPLER_DESC samplerdesc = D3D11_SAMPLER_DESC();ZeroMemory( &samplerdesc, sizeof(samplerdesc) );samplerdesc.AddressU = D3D11_TEXTURE_ADDRESS_MODE::D3D11_TEXTURE_ADDRESS_WRAP;samplerdesc.AddressV = D3D11_TEXTURE_ADDRESS_MODE::D3D11_TEXTURE_ADDRESS_WRAP;samplerdesc.Filter = D3D11_FILTER::D3D11_FILTER_ANISOTROPIC;device->CreateSamplerState(&samplerdesc,&sampstate);////////////////////////////////immediatecontext->PSSetSamplers(0,1,&sampstate);immediatecontext->VSSetSamplers(0,1,&sampstate);
Texture2D<float4> Textur : register(t0);sampler sampler1 : register(s0);cbuffer Matrix : register( b0 ){	row_major matrix World;	row_major matrix View;	row_major matrix Projection;};struct VS_INPUT{    float4 Pos : POSITION;    float2 texcoord : TEXCOORD;};struct PS_INPUT{    float4 Pos : SV_POSITION;    float2 texcoord : TEXCOORD;};//--------------------------------------------------------------------------------------// Vertex Shader//--------------------------------------------------------------------------------------PS_INPUT VS( VS_INPUT input ){    PS_INPUT output = (PS_INPUT)0;    output.Pos = mul(input.Pos,World);    output.Pos = mul(output.Pos,View);    output.Pos = mul(output.Pos,Projection);    output.texcoord = input.texcoord;    return output;}//--------------------------------------------------------------------------------------// Pixel Shader//--------------------------------------------------------------------------------------float4 PS( PS_INPUT input) : SV_Target{	float4 ret = Textur.Sample(sampler1,input.texcoord);	return ret;}
Is there a problem with the shader itself?

The resolution of the image is 512X512, so there shouldnt be a problem with it?Attached File  merkurfail.PNG   238.36KB   34 downloads

the u coordinate ranges from 0 to 2, and the white strip has nothing to do with the texture, will replace it soon


Its working now, i missed to set the addressmode w parameter in the samplerstate, but i have another question anyway.
While searching in the internet i saw this code snippet:
Texture2D Textur : register(t0);
SamplerState sampler1 : register(s0);

while i used this one:
Texture2D<float4> Textur : register(t0);
sampler sampler1 : register(s0);

both is working but where is the difference, besides the float4 telling hlsl the format.

multiple objects performance issue

19 June 2012 - 10:04 AM

Hello, its me again.
After i sucessfully initialised my shader code i experienced some performance issues with drawing my scene which contains spherical geometrie. The issue seems to be the seperate drawing calls which tookes about 0,3ms. first i thought my graphics card would be too slow to give me the necessary performance to render 1800 spheres but then i increased the vertex-count of my "sphere" and the rendering tooked the same time.

Now i know that the Issue has to be the cpu-gpu-communication, but everything at all i cant imagine how a single draw call can cost that much time?... if i look at modern games having hundreds of models displayed at the same time.

A solution would be to use instancing but iam not really sure how i can use it at dx11, basicly all that should be done is telling the gpu to loop my indexbuffer content n times insteat of calling the drawing such often. The problem is i have absolutly no idea how to do that? Creating the whole indesbuffer containing 1800 times the same index-sequence seams to be very messy and unnecessary.

And if you use different models at the same time i guess a drawing call which takes 200 ms for 1000 different object buffers isnt really aceptable so how do YOU manage it?

problems with shader matrices

03 June 2012 - 06:59 AM

Hello Forum,

I have currently some issues with my shader code:

cbuffer matrizen : register( b0 )
	row_major matrix World;
	row_major matrix View;
	row_major matrix Projection;


struct VS_INPUT
	float4 Pos : POSITION;

struct PS_INPUT
	float4 Pos : SV_POSITION;

// Vertex Shader
	PS_INPUT output = (PS_INPUT)0;

	output.Pos = mul(input.Pos,World);
	output.Pos = mul(output.Pos,View);
	output.Pos = mul(output.Pos,Projection);

	return output;

// Pixel Shader
float4 PS( PS_INPUT input) : SV_Target
	return float4(1.0f,0.0f,0.0f,1.0f);

actually this code is mostly copied from the directx sample browser and should work as it also works in the sample project. The Problem is, it doesnt displays anything if i apply the matrix transformations inside the vertex shader. The Matrices are assigned over this code.

struct Mats
	XMMATRIX Projection;
Mats mat;
mat.World = XMMatrixIdentity(); //no geometry transformations
XMVECTOR Eye = XMVectorSet( 0.0f, 0.0f, 5.0f, 0.0f );
XMVECTOR At = XMVectorSet( 0.0f, 0.0f, 0.0f, 0.0f );
XMVECTOR Up = XMVectorSet( 0.0f, 1.0f, 0.0f, 0.0f );
mat.View = XMMatrixLookAtLH( Eye, At, Up ); //looking on the triangle
mat.Projection =  XMMatrixPerspectiveFovLH( XM_PIDIV4, (FLOAT)width / (FLOAT)height, 0.01f, 100.0f ); //calculating perspective matrix

immediatecontext->UpdateSubresource( Matrixbuffer 0, NULL, &mat, 0, 0 ); //updating matrices

drawing the content:

immediatecontext->VSSetShader( vertexshader, NULL, 0 ); //using the vertexshader
	immediatecontext->VSSetConstantBuffers( 0, 1, &Matrixbuffer ); //assign matrices to the vertexshader
	immediatecontext->PSSetShader( pixelshader, NULL, 0 ); //using the pixelshader
	immediatecontext->Draw( 3, 0 ); //drawing three vertices (one single triangle)
	swapchain->Present( 0, 0 );

I guess there is a really easy solution to this problem but currently i dont know how i could fix that.

EDIT: Leaving the matrices Identity matrices doesnt solves the problem so the problem has to be inside the matrix assignment (without doing the matrix transformations it display the triangle)