Jump to content

  • Log In with Google      Sign In   
  • Create Account

[HLSL] inefficent function


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
17 replies to this topic

#1 tr0x   Members   -  Reputation: 100

Like
0Likes
Like

Posted 30 April 2011 - 04:49 AM

Hey,

in my shader I use a function that stores and returns the resulting data in two arrays. Everything works correctly but I assume it is very bad style of coding.
The result is correct, but the bigger those arrays are, the longer the program takes to start and the lower the framerate.

Here the relevant code snippets:

struct RET
{
	float a1[10][20];
	float a2[10][20];
};

static float array1[10][20];
static float array2[10][20];


RET foo1()
{
   RET r;

   // write and read array1 and array2

   r.a1 = array1;
   r.a2 = array2;

   return r;
}

Well, this is awkward but it was the only way to get it working.

I actually don't see any reason for array1 and array2 to be static and global. But if I try it like follows the program throws an error message at start:

RET foo2()
{
   RET r;

   // write and read r.a1 and r.a2

   return r;
}

1. Is this behaviour comprehensible?

2. How could I realize my task in a proper and more efficient way?

3. Do I maybe need to pack the data into float4s?

Sponsor:

#2 tr0x   Members   -  Reputation: 100

Like
0Likes
Like

Posted 01 May 2011 - 01:41 AM

So basically the question is: how to effectively write and read two-dimensional data in the shader.

#3 MJP   Moderators   -  Reputation: 11737

Like
0Likes
Like

Posted 01 May 2011 - 11:24 AM

Could you explain what you're actually trying to implement here?

#4 tr0x   Members   -  Reputation: 100

Like
0Likes
Like

Posted 02 May 2011 - 12:11 AM

Could you explain what you're actually trying to implement here?

I implemented an iterative solution of a recursion.

Based on two input variables and certain criteria the result is computed bottom up, so that the actual result is in the very first line of the array.
( i.e. it would be enough to return the first lines of both arrays, I just realized )

During the execution of the function both the arrays are written and read.

I have to add that the mentioned effects ( longer launch time, lower fps ) are not occuring because the function complexity increases with increasing array size.
Even if I only operate within [5][10], but declare the array as [10][20] the effects appear.

#5 tr0x   Members   -  Reputation: 100

Like
0Likes
Like

Posted 03 May 2011 - 09:07 AM

Anyone an idea maybe? Is the problem description still unclear?

As I said, everything works correctly but here an example how the performance is related to the array size:

static array1[5][7]
static array2[5][7]
launch time: 5 seconds
fps: 50

static array1[17][31]
static array2[17][31]
launch time: 80 seconds
fps: 12


The computational effort of the function is the same, as I use in both cases only the first 5 lines and the first 7 columns of the array.
So there's something terribly wrong with those arrays.

Any help would be much appreciated!

#6 MJP   Moderators   -  Reputation: 11737

Like
0Likes
Like

Posted 03 May 2011 - 11:51 AM

So if I understand you correctly, you just have a 2D array of temporary data that is written to and read by your shader program?

If that is the case, my guess would be that the array is consuming large numbers of registers, which is hurting performance. GPU's have a fixed-size register file per processor, and the (potentially thousands) of threads in flight on a single unit must share that register space. Consuming too many registers per thread means that the GPU can not keep as many threads in flight simultaneously, which can significantly hinder performance. However this is just a guess, and you would have to confirm using vendor-specific profiling/analysis tools.

#7 tr0x   Members   -  Reputation: 100

Like
0Likes
Like

Posted 03 May 2011 - 12:07 PM

So if I understand you correctly, you just have a 2D array of temporary data that is written to and read by your shader program?

Thank's for you answer!

That's right. The function uses internally two 2D arrays for computation and returns two 1D arrays (i've changed that now). Do you have any proposals how to circumvent the problem?

What I still don't get: why can't I just declare the arrays within the function? As they are only used by this function, I don't see any reason for them to be global and static.
Is there any reasonable explanation or might this be only a strange side-effect caused by something else?

#8 tr0x   Members   -  Reputation: 100

Like
0Likes
Like

Posted 04 May 2011 - 11:45 AM

I'd have a neat screenshot to offer if the problem gets solved! ;)

Sorry for double posting all the time, but I don't have ideas anymore, to solve my issue and this is the only place I know, where I can anticipate help.

#9 MJP   Moderators   -  Reputation: 11737

Like
0Likes
Like

Posted 04 May 2011 - 03:54 PM

What I still don't get: why can't I just declare the arrays within the function? As they are only used by this function, I don't see any reason for them to be global and static.
Is there any reasonable explanation or might this be only a strange side-effect caused by something else?


What's the actual error that you get? Is it a compiler error?

#10 tr0x   Members   -  Reputation: 100

Like
0Likes
Like

Posted 05 May 2011 - 01:11 AM

What's the actual error that you get? Is it a compiler error?



Yeah, if I declare the arrays global and static it works - with the mentioned drawbacks.
If I declare the arrays within the function using the static keyword, the program has the same problems but only draws a blank screen. i.e. the result of the function is probably always zero.
If I don't use the static keyword within the function the program throws an error at startup. Same thing happens if I try to declare it globally and non static.

It's really frustrating.. all I need is a small temporal storage place, which I can read and write to. The function is admittedly critical and is called up to million times each frame. But obviously the pure allocation of memory and not the computational effort causes the performance to decrease dramatically.

#11 Hodgman   Moderators   -  Reputation: 31799

Like
0Likes
Like

Posted 05 May 2011 - 01:25 AM

the program throws an error at startup..

What's the actual error that you get?

BTW, 17*31 is 527 registers, which is a lot. Why do you need so many temp registers?

#12 tr0x   Members   -  Reputation: 100

Like
0Likes
Like

Posted 05 May 2011 - 01:50 AM

BTW, 17*31 is 527 registers, which is a lot. Why do you need so many temp registers?

As recursion is not supported in HLSL, this function solves a recursion formula using an iterative approach.

To accomplish that, I use the array as a binary tree, where the value of (x,y) is based on the values of (x,y-1) and (x+1, y-1).

As result the function returns two arrays.

#13 ankhd   Members   -  Reputation: 1357

Like
0Likes
Like

Posted 05 May 2011 - 09:56 PM

Hi there. Could you use texture to hold your data and not an array

#14 Hodgman   Moderators   -  Reputation: 31799

Like
0Likes
Like

Posted 05 May 2011 - 10:05 PM

What's the actual error that you get?



#15 tr0x   Members   -  Reputation: 100

Like
0Likes
Like

Posted 06 May 2011 - 12:14 AM

Hi there. Could you use texture to hold your data and not an array

It is important not only to read but also to write the data in the shader function. As far as I know that is not really possible with textures, is it?


What's the actual error that you get?

I get pretty much the the "standard" error messages, that always pop up, when there's something wrong with the shader code:

"The Direct3D device has a non-zero reference count, meaning some objects were not released."
"An error occurred in the device create callback function."
(from DXUT)

Since this error also appears if there's a missing semicolon or whatever, I don't think it really helps here.

I was able to increase the performance dramatically by using an array with just two rows, as the values of each hierarchy level are only dependent on its child-level.

Though I still think it is bad style of coding to do it statically.

#16 n3Xus   Members   -  Reputation: 720

Like
1Likes
Like

Posted 06 May 2011 - 04:32 AM

They mean the shader error, not the debug output.

When you call the compileFromFile function (not sure which one exactly you use) it returns an error blob on DX11 and 10 (not sure how you get it on DX9).

#17 forsandifs   Members   -  Reputation: 154

Like
1Likes
Like

Posted 06 May 2011 - 05:16 AM

They mean the shader error, not the debug output.

When you call the compileFromFile function (not sure which one exactly you use) it returns an error blob on DX11 and 10 (not sure how you get it on DX9).


Or you can use the fxc compiler directly from the command line window. That outputs error messages on the fly.

#18 tr0x   Members   -  Reputation: 100

Like
0Likes
Like

Posted 06 May 2011 - 06:17 AM

Oh dear.. I didn't even know about this feature. :unsure: Thanks for pointing it out, it should make life a lot easier!

The actual error code is x4580. It states that the shader that uses the concerned function does not return the values it should.

I guess that just means that there is an error during the execution of the shader, doesn't it?




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS