FleX icon indicating copy to clipboard operation
FleX copied to clipboard

Compile success, run failure in flex1.2

Open SmileHoudini opened this issue 7 years ago • 0 comments

This is the initial code of flex1.2 doc, I change the initial position, and I add gravity, set the initial speed, and activate 100 particles, which is a very simple start, running, the 970 graphics card does not respond to 5 seconds, the print particle position result is 0, I include cuda8.044, in win7,64.

#include <NvFlex.h> #include <NvFlexDevice.h> #include <NvFlexExt.h> #include #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <../core/types.h> #include <../core/maths.h> struct float4 { float a; float b; float c; float d; }; struct float3 { float a; float b; float c; };

void RenderParticles(float4* particles, float3* velocities, int* phases){ printf("%f,%f,%f\n..", particles[0].a, particles[0].b, particles[0].c); }

int main() { int n =100; NvFlexLibrary* library = NvFlexInit(); // create new solver NvFlexSolverDesc solverDesc; NvFlexSetSolverDescDefaults(&solverDesc); solverDesc.maxParticles = n; solverDesc.maxDiffuseParticles = 0;

NvFlexSolver* solver = NvFlexCreateSolver(library, &solverDesc);
NvFlexExtContainer* _cont = NvFlexExtCreateContainer(library, solver, n);
NvFlexBuffer* particleBuffer = NvFlexAllocBuffer(library, n, sizeof(float4), eNvFlexBufferHost);
NvFlexBuffer* velocityBuffer = NvFlexAllocBuffer(library, n, sizeof(float4), eNvFlexBufferHost);
NvFlexBuffer* phaseBuffer = NvFlexAllocBuffer(library, n, sizeof(int), eNvFlexBufferHost);
NvFlexBuffer* activeBuffer = NvFlexAllocBuffer(library, n, sizeof(int), eNvFlexBufferHost);

    NvFlexParams nvparams;
//// sim params
nvparams.gravity[0] = 0.0f;
nvparams.gravity[1] = -9.8f;
nvparams.gravity[2] = 0.0f;
nvparams.radius = 0.015f;
int done = 0;
int initcheck = 0;
while (done<100)
{
	done += 1;
	// map buffers for reading / writing
	float4* particles = (float4*)NvFlexMap(particleBuffer, eNvFlexMapWait);
	float3* velocities = (float3*)NvFlexMap(velocityBuffer, eNvFlexMapWait);
	int* phases = (int*)NvFlexMap(phaseBuffer, eNvFlexMapWait);
	int* activeIndices = (int*)NvFlexMap(activeBuffer, eNvFlexMapWait);

	// spawn (user method)
	//SpawnParticles(particles, velocities, phases);

	if (!initcheck)
	{
		for (int i = 0; i < n; ++i)
		{
			particles[i].a =0;
			particles[i].b = i;
			particles[i].c = 0;
			velocities[i] = vel;
			phases[i] = NvFlexMakePhase(0, eNvFlexPhaseSelfCollide);
			activeIndices[i] = i;
		}
		initcheck += 1;
	}

	for (int i = 0; i < n; ++i)
	{	
		phases[i] = NvFlexMakePhase(0, eNvFlexPhaseSelfCollide);
			
	}

	// render (user method)
	RenderParticles(particles, velocities, phases);
	// unmap buffers
	NvFlexUnmap(particleBuffer);
	NvFlexUnmap(velocityBuffer);
	NvFlexUnmap(phaseBuffer);
	NvFlexUnmap(activeBuffer);
	// write to device (async)
	NvFlexSetParticles(solver, particleBuffer, NULL);
	NvFlexSetVelocities(solver, velocityBuffer, NULL);
	NvFlexSetPhases(solver, phaseBuffer, NULL);
	NvFlexSetActive(solver, activeBuffer, NULL);
	NvFlexSetActiveCount(solver, n);
	// tick
	NvFlexSetParams(solver, &nvparams);

	NvFlexUpdateSolver(solver,0.025, 1, false);
	NvFlexGetParticles(solver, particleBuffer, NULL);
	NvFlexGetVelocities(solver, velocityBuffer, NULL);
	NvFlexGetPhases(solver, phaseBuffer, NULL);
	NvFlexGetActive(solver, activeBuffer, NULL);
}

NvFlexFreeBuffer(particleBuffer);
NvFlexFreeBuffer(velocityBuffer);
NvFlexFreeBuffer(phaseBuffer);
NvFlexFreeBuffer(activeBuffer);
NvFlexDestroySolver(solver);
NvFlexShutdown(library);

system("pause");
return 0;

}

adbt3 9dk12zk 5 1 qe

Thank you very much for your suggestions. I am looking forward to your any help. Pay tribute to the great NVIDIA thinks!

SmileHoudini avatar May 06 '18 13:05 SmileHoudini