Closed doukasd closed 5 years ago
Hi, i am a bit familiar with compute shaders, this is due to the computebuffer size that was containing xxx amount of particles when initilialzed earlier, and then the flex source actor added more particles, but the buffer size wasnt updated. im having the same issue in unity and the flex plugin. i think i can debug this, did some compute shader coding last month. if i manage to fix that ill post the solution there.
For example, if you set the fluid source actor particle life to a high number like : 999, when the source actor spawns its particles, it has to wait 999 seconds before adding anymore particles to the simulation, and having to update the compute buffer size, so it runs fine as long as no new particles are added.
But i need this to be fixed so ill take a look and see if i can resize the compute buffer according to the new particles count. This shouldnt require any PHD in Astrophysics, because i got none.
:-)
here for example in _auxFlexDrawFluid.cs at the line 247 :
if you replace m_indexBuffer.SetData(indices);
by
if(indices.Length != m_indexBuffer.count)
{
m_indexBuffer = new ComputeBuffer(indices.Length, sizeof(int));
}
else
{
m_indexBuffer.SetData(indices);
}
Now the error is gone, but because we recreate the buffer every single time we add a particle, this creates a flickering. Ill see if i can fix that, with my poor understanding of computer graphics.
I guess they use some kind of pre-allocation system for x number of particles, and update that buffer only once in a while.
Wow i managed to fix this :
//ORIGINAL
//m_indexBuffer.SetData(indices);
//ORIGINAL
//NEW
if(indices.Length != m_indexBuffer.count)
{
m_indexBuffer = new ComputeBuffer(indices.Length, sizeof(int));
m_indexBuffer.SetData(indices);
}
else
{
m_indexBuffer.SetData(indices);
}
//NEW
The flicker happened because the SetData needs to be called in all cases, the code can be simplified by removing the else, and writing only one SetData at the end.
Now for me this works, i dont really know if this doesnt create side effects, i've read somewhere on unity forums that recreating ComputeBuffer every frame is not a good idea. Thats as far as my knowledge goes on this topic.
I hope this helps.
@rt974 Thanks for the solution! that helps me solve the problem!
After a lil' bit investigation I think your solution can be better by adding another line
m_indexBuffer.Release();
before:
m_indexBuffer = new ComputeBuffer(indices.Length, sizeof(int));
Which releases the buffer created at line 242 before recreating it, otherwise it will throw back some warnings by Unity Garbage Collection
The code will then be:
if (indices.Length != m_indexBuffer.count)
{
m_indexBuffer.Release();
m_indexBuffer = new ComputeBuffer(indices.Length, sizeof(int));
}
m_indexBuffer.SetData(indices);
Thanks for the help!
The solution by @reforia keeps the number of drawn particles at the maximum. This becomes a problem when deactivating the fluid source, as the fluids will not despawn until every single one of them has reached the end of its lifetime. Instead I propose this workaround.
Start with a fresh (unmodified) _auxFlexDrawFluid.cs, and simply replace:
if (m_indexBuffer != null)
{
m_indexBuffer.SetData(indices);
}
at line 245 with:
if (m_indexBuffer != null)
{
m_indexBuffer.SetData(indices, 0, 0, m_indexBuffer.count);
}
The crash happened because indices
doesn't appear to dynamically change size. So if our max particles is 4000, then indices
will have a length of 4000 as long as there're any particles around.
indexCount
on the other hand directly reflects how many particles are alive, which may for instance be 3672. So when copying the indices we need to limit what we're copying by the size of m_indexBuffer
. Creating a new m_indexBuffer
to match the maximum amount of particles on the other hand causes particles to visually stay in the scene.
Hi, i am a bit familiar with compute shaders, this is due to the computebuffer size that was containing xxx amount of particles when initilialzed earlier, and then the flex source actor added more particles, but the buffer size wasnt updated. im having the same issue in unity and the flex plugin. i think i can debug this, did some compute shader coding last month. if i manage to fix that ill post the solution there.
For example, if you set the fluid source actor particle life to a high number like : 999, when the source actor spawns its particles, it has to wait 999 seconds before adding anymore particles to the simulation, and having to update the compute buffer size, so it runs fine as long as no new particles are added.
But i need this to be fixed so ill take a look and see if i can resize the compute buffer according to the new particles count. This shouldnt require any PHD in Astrophysics, because i got none.
:-)
its perfectly worked for me thank you
I am trying to use the SteamVR Plugin with the nvidia Flex fluid renderer. On their own, they all work fine. But when combined, there are runtime errors.
Setup:
Steps to reproduce:
Camera
in that scene and drop in the[CameraRig]
from the SteamVR prefabsI understand that it seems to be Flex "crashing" here and not SteamVR, which is why I first posted this issue on the nvidia forums, however as SteamVR is implicated (Flex works fine without it) I am here to see if SteamVR Plugin could be improved in some way to not affect other such systems.