Наследование: UnityEngine.Object
Пример #1
0
 public BoundsChecker(ComputeShader compute, LifeService l, PositionService p)
 {
     _kernel = compute.FindKernel(ShaderConst.KERNEL_CHECK_BOUNDS);
     _compute = compute;
     _lifes = l;
     _positions = p;
 }
Пример #2
0
 public void SetConstants(ComputeShader compute, float dt)
 {
     compute.SetFloat(ShaderConst.PROP_DELTA_TIME, dt);
     compute.SetFloat(ShaderConst.PROP_ELASTICS, _data.elastics);
     compute.SetFloat(ShaderConst.PROP_PARTICLE_RADIUS, _data.radius);
     compute.SetVector(ShaderConst.PROP_BOUNDS, _data.bounds);
 }
Пример #3
0
    public void Init()
    {
        _numParticlesX = _particleGroupsX * kNumThreadsX;
        _numParticlesY = _particleGroupsY * kNumThreadsY;
        _numParticles = _numParticlesX * _numParticlesY;

        _currentCopiedVertices = 0;

        _particleMaterial = Resources.Load<Material>("GPUParticleMat");

        _computeShader = Resources.Load<ComputeShader>("ComputeShaders/GPUParticleUpdater");
        _kernelMoveParticles = _computeShader.FindKernel(kKernelMoveParticles);

        _particlesData = new Particle[_numParticles];
        InitBuffer(_particlesData);

        for (int i = 0; i < _particlesData.Length; ++i)
        {
            float id = ((float)i) / 10000.1f;
            CreateParticle(id);
        }

        // Set ComputeShader Parameters
        _particlesBuffer = new ComputeBuffer(_particlesData.Length, System.Runtime.InteropServices.Marshal.SizeOf(typeof(GPUParticleSystem.Particle)));
        _particlesBuffer.SetData(_particlesData);

        _computeShader.SetBuffer(_kernelMoveParticles, "_ParticlesBuffer", _particlesBuffer);

        _computeShader.SetFloat("_Width", _numParticlesX);
        _computeShader.SetFloat("_Height", _numParticlesY);

        // Set Shader Parameters
        _particleMaterial.SetBuffer("_ParticlesBuffer", _particlesBuffer);
    }
 public BitonicMergeSort(ComputeShader compute)
 {
     _compute = compute;
     _kernelInit = compute.FindKernel(KERNEL_INIT);
     _kernelSort = compute.FindKernel(KERNEL_SORT);
     _kernelSortInt = compute.FindKernel(KERNEL_SORT_INT);
 }
 static public int SetMatrixArray(IntPtr l)
 {
     try {
         int argc = LuaDLL.lua_gettop(l);
         if (matchType(l, "SetMatrixArray__Int32__Arr_Matrix4x4", argc, 2, typeof(int), typeof(UnityEngine.Matrix4x4[])))
         {
             UnityEngine.ComputeShader self = (UnityEngine.ComputeShader)checkSelf(l);
             System.Int32 a1;
             checkType(l, 3, out a1);
             UnityEngine.Matrix4x4[] a2;
             checkArray(l, 4, out a2);
             self.SetMatrixArray(a1, a2);
             pushValue(l, true);
             return(1);
         }
         else if (matchType(l, "SetMatrixArray__String__Arr_Matrix4x4", argc, 2, typeof(string), typeof(UnityEngine.Matrix4x4[])))
         {
             UnityEngine.ComputeShader self = (UnityEngine.ComputeShader)checkSelf(l);
             System.String             a1;
             checkType(l, 3, out a1);
             UnityEngine.Matrix4x4[] a2;
             checkArray(l, 4, out a2);
             self.SetMatrixArray(a1, a2);
             pushValue(l, true);
             return(1);
         }
         pushValue(l, false);
         LuaDLL.lua_pushstring(l, "No matched override function to call");
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
Пример #6
0
    /// <summary>
    /// Uses the GPU to generate a RenderTexture where the pixels in the texture represent noise.
    /// Set the octaves variable before calling this to a desired value.
    /// </summary>
    /// <returns>RenderTexture</returns>
    /// <param name="width"> Width of the texture to generate. </param>
    /// <param name="height"> Height of the texture to generate. </param>
    /// <param name="noiseOffsetX"> X Coordinate of the offset for the noise on the texture. </param>
    /// <param name="noiseOffsetY"> Y Coordinate of the offset for the noise on the texture. </param>
    /// <param name="noiseScale"> Value to scale the noise coordinates by. </param>
    /// <param name="normalize"> Whether or not to remap the noise from (-1, 1) to (0, 1). </param>
    public static UnityEngine.RenderTexture GetNoiseRenderTexture(int width, int height, float noiseOffsetX = 0, float noiseOffsetY = 0, float noiseScale = 0.01f, bool normalize = true)
    {
        UnityEngine.RenderTexture retTex = new UnityEngine.RenderTexture(width, height, 0);
        retTex.enableRandomWrite = true;
        retTex.Create();

        UnityEngine.ComputeShader shader = UnityEngine.Resources.Load(shaderPath) as UnityEngine.ComputeShader;
        if (shader == null)
        {
            UnityEngine.Debug.LogError(noShaderMsg);
            return(null);
        }

        int[] resInts = { width, height };

        int kernel = shader.FindKernel("ComputeNoise");

        shader.SetTexture(kernel, "Result", retTex);
        SetShaderVars(shader, new UnityEngine.Vector2(noiseOffsetX, noiseOffsetY), normalize, noiseScale);
        shader.SetInts("reses", resInts);

        UnityEngine.ComputeBuffer permBuffer = new UnityEngine.ComputeBuffer(perm.Length, 4);
        permBuffer.SetData(perm);
        shader.SetBuffer(kernel, "perm", permBuffer);

        shader.Dispatch(kernel, width / 14, height / 15, 1);

        permBuffer.Release();

        return(retTex);
    }
Пример #7
0
 public PositionSimulation(ComputeShader compute, VelocityService v, PositionService p)
 {
     _kernelSimulate = compute.FindKernel(ShaderConst.KERNEL_SIMULATE_POSITION);
     _compute = compute;
     _velocities = v;
     _positions = p;
 }
Пример #8
0
 static public int constructor(IntPtr l)
 {
     UnityEngine.ComputeShader o;
     o = new UnityEngine.ComputeShader();
     pushObject(l, o);
     return(1);
 }
 private void ShowShaderErrors(ComputeShader s)
 {
     if (ShaderUtil.GetComputeShaderErrorCount(s) >= 1)
     {
         ShaderInspector.ShaderErrorListUI(s, ShaderUtil.GetComputeShaderErrors(s), ref this.m_ScrollPosition);
     }
 }
 static public int constructor(IntPtr l)
 {
     try {
                     #if DEBUG
         var    method     = System.Reflection.MethodBase.GetCurrentMethod();
         string methodName = GetMethodName(method);
                     #if UNITY_5_5_OR_NEWER
         UnityEngine.Profiling.Profiler.BeginSample(methodName);
                     #else
         Profiler.BeginSample(methodName);
                     #endif
                     #endif
         UnityEngine.ComputeShader o;
         o = new UnityEngine.ComputeShader();
         pushValue(l, true);
         pushValue(l, o);
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
             #if DEBUG
     finally {
                     #if UNITY_5_5_OR_NEWER
         UnityEngine.Profiling.Profiler.EndSample();
                     #else
         Profiler.EndSample();
                     #endif
     }
             #endif
 }
 static public int HasKernel(IntPtr l)
 {
     try {
                     #if DEBUG
         var    method     = System.Reflection.MethodBase.GetCurrentMethod();
         string methodName = GetMethodName(method);
                     #if UNITY_5_5_OR_NEWER
         UnityEngine.Profiling.Profiler.BeginSample(methodName);
                     #else
         Profiler.BeginSample(methodName);
                     #endif
                     #endif
         UnityEngine.ComputeShader self = (UnityEngine.ComputeShader)checkSelf(l);
         System.String             a1;
         checkType(l, 2, out a1);
         var ret = self.HasKernel(a1);
         pushValue(l, true);
         pushValue(l, ret);
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
             #if DEBUG
     finally {
                     #if UNITY_5_5_OR_NEWER
         UnityEngine.Profiling.Profiler.EndSample();
                     #else
         Profiler.EndSample();
                     #endif
     }
             #endif
 }
Пример #12
0
 public VelocitySimulation(ComputeShader compute, VelocityService v, ConstantService c)
 {
     _kernelSimulate = compute.FindKernel(ShaderConst.KERNEL_SIMULATE_VELOCITY);
     _compute = compute;
     _velocities = v;
     _constants = c;
 }
Пример #13
0
 static public int SetFloat(IntPtr l)
 {
     try {
         int argc = LuaDLL.lua_gettop(l);
         if (matchType(l, argc, 2, typeof(int), typeof(float)))
         {
             UnityEngine.ComputeShader self = (UnityEngine.ComputeShader)checkSelf(l);
             System.Int32 a1;
             checkType(l, 2, out a1);
             System.Single a2;
             checkType(l, 3, out a2);
             self.SetFloat(a1, a2);
             pushValue(l, true);
             return(1);
         }
         else if (matchType(l, argc, 2, typeof(string), typeof(float)))
         {
             UnityEngine.ComputeShader self = (UnityEngine.ComputeShader)checkSelf(l);
             System.String             a1;
             checkType(l, 2, out a1);
             System.Single a2;
             checkType(l, 3, out a2);
             self.SetFloat(a1, a2);
             pushValue(l, true);
             return(1);
         }
         pushValue(l, false);
         LuaDLL.lua_pushstring(l, "No matched override function SetFloat to call");
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
 public WallCollisionSolver(ComputeShader compute, VelocityService v, PositionService p, WallService w)
 {
     _kernel = compute.FindKernel(ShaderConst.KERNEL_SOLVE_WALL_COLLISION);
     _compute = compute;
     _velocities = v;
     _positions = p;
     _walls = w;
 }
Пример #15
0
 public void Initialize(ComputeShader sh_bitonic_sort)
 {
     m_cs_bitonic_sort = sh_bitonic_sort;
     m_buf_consts[0] = new ComputeBuffer(1, 16);
     m_buf_consts[1] = new ComputeBuffer(1, 16);
     m_buf_dummies[0] = new ComputeBuffer(1, 16);
     m_buf_dummies[1] = new ComputeBuffer(1, 16);
 }
Пример #16
0
    //
    // GPU STUFF
    //

    private static void SetShaderVars(UnityEngine.ComputeShader shader, UnityEngine.Vector2 noiseOffset, bool normalize, float noiseScale)
    {
        shader.SetInt("octaves", octaves);
        shader.SetFloat("falloff", falloff);

        shader.SetInt("normalize", System.Convert.ToInt32(normalize));
        shader.SetFloat("noiseScale", noiseScale);
        shader.SetVector("offset", noiseOffset);
    }
 public ParticleCollisionSolver(ComputeShader compute, VelocityService v, PositionService p, LifeService l, BroadPhase b)
 {
     _kernel = compute.FindKernel(ShaderConst.KERNEL_SOLVE_PARTICLE_COLLISION);
     _compute = compute;
     _velocities = v;
     _positions = p;
     _lifes = l;
     _broadphase = b;
 }
 public PolygonCollisionSolver(ComputeShader c, VelocityService v, PositionService p, LifeService l, PolygonColliderService poly)
 {
     _compute = c;
     _kernel = c.FindKernel(ShaderConst.KERNEL_SOLVE_POLYGON_COLLISION);
     _velocities = v;
     _positions = p;
     _lifes = l;
     _polygons = poly;
 }
Пример #19
0
    public static void ReadFromRenderTexture(RenderTexture tex, int channels, ComputeBuffer buffer, ComputeShader readData)
    {
        if(tex == null)
        {
            Debug.Log("CBUtility::ReadFromRenderTexture - RenderTexture is null");
            return;
        }

        if(buffer == null)
        {
            Debug.Log("CBUtility::ReadFromRenderTexture - buffer is null");
            return;
        }

        if(readData == null)
        {
            Debug.Log("CBUtility::ReadFromRenderTexture - Computer shader is null");
            return;
        }

        if(channels < 1 || channels > 4)
        {
            Debug.Log("CBUtility::ReadFromRenderTexture - Channels must be 1, 2, 3, or 4");
            return;
        }

        int kernel = -1;
        int depth = 1;
        string D = "2D";
        string C = "C" + channels.ToString();

        if(tex.isVolume)
        {
            depth = tex.volumeDepth;
            D = "3D";
        }

        kernel = readData.FindKernel("read"+D+C);

        if(kernel == -1)
        {
            Debug.Log("CBUtility::ReadFromRenderTexture - could not find kernel " + "read"+D+C);
            return;
        }

        int width = tex.width;
        int height = tex.height;

        //set the compute shader uniforms
        readData.SetTexture(kernel, "_Tex"+D, tex);
        readData.SetInt("_Width", width);
        readData.SetInt("_Height", height);
        readData.SetBuffer(kernel, "_Buffer"+D+C, buffer); //tex will be write into this
        //run the  compute shader
        readData.Dispatch(kernel, Mathf.Max(1, width/8), Mathf.Max(1, height/8), Mathf.Max(1, depth/8));
    }
 static public int SetBuffer(IntPtr l)
 {
     try {
                     #if DEBUG
         var    method     = System.Reflection.MethodBase.GetCurrentMethod();
         string methodName = GetMethodName(method);
                     #if UNITY_5_5_OR_NEWER
         UnityEngine.Profiling.Profiler.BeginSample(methodName);
                     #else
         Profiler.BeginSample(methodName);
                     #endif
                     #endif
         int argc = LuaDLL.lua_gettop(l);
         if (matchType(l, argc, 2, typeof(int), typeof(int), typeof(UnityEngine.ComputeBuffer)))
         {
             UnityEngine.ComputeShader self = (UnityEngine.ComputeShader)checkSelf(l);
             System.Int32 a1;
             checkType(l, 2, out a1);
             System.Int32 a2;
             checkType(l, 3, out a2);
             UnityEngine.ComputeBuffer a3;
             checkType(l, 4, out a3);
             self.SetBuffer(a1, a2, a3);
             pushValue(l, true);
             return(1);
         }
         else if (matchType(l, argc, 2, typeof(int), typeof(string), typeof(UnityEngine.ComputeBuffer)))
         {
             UnityEngine.ComputeShader self = (UnityEngine.ComputeShader)checkSelf(l);
             System.Int32 a1;
             checkType(l, 2, out a1);
             System.String a2;
             checkType(l, 3, out a2);
             UnityEngine.ComputeBuffer a3;
             checkType(l, 4, out a3);
             self.SetBuffer(a1, a2, a3);
             pushValue(l, true);
             return(1);
         }
         pushValue(l, false);
         LuaDLL.lua_pushstring(l, "No matched override function SetBuffer to call");
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
             #if DEBUG
     finally {
                     #if UNITY_5_5_OR_NEWER
         UnityEngine.Profiling.Profiler.EndSample();
                     #else
         Profiler.EndSample();
                     #endif
     }
             #endif
 }
Пример #21
0
 public HashService(ComputeShader compute, LifeService l, PositionService p)
 {
     _compute = compute;
     _lifes = l;
     _positions = p;
     _kernelInitHashes = compute.FindKernel(ShaderConst.KERNEL_INIT_HASHES);
     HashData = new int[l.Lifes.count];
     Hashes = new ComputeBuffer(l.Lifes.count, Marshal.SizeOf(typeof(int)));
     Hashes.SetData (HashData);
 }
Пример #22
0
 public void SetConstants(ComputeShader compute, float dt)
 {
     CheckDragCoeffs ();
     compute.SetFloat(ShaderConst.PROP_DELTA_TIME, dt);
     compute.SetFloat(ShaderConst.PROP_ELASTICS, _data.elastics);
     compute.SetFloat(ShaderConst.PROP_FRICTION, _data.friction);
     compute.SetFloat(ShaderConst.PROP_PARTICLE_RADIUS, _data.radius);
     compute.SetFloat(ShaderConst.PROP_PENETRATION_BIAS, _data.penetrationBias);
     compute.SetVector(ShaderConst.PROP_BOUNDS, _data.bounds);
 }
Пример #23
0
 public PositionService(ComputeShader compute, int capacity)
 {
     _kernelUpload = compute.FindKernel(ShaderConst.KERNEL_UPLOAD_POSITION);
     _compute = compute;
     _positions = new Vector2[capacity];
     P0 = new ComputeBuffer(capacity, Marshal.SizeOf(_positions[0]));
     P0.SetData(_positions);
     _uploader = new ComputeBuffer(INITIAL_CAP, Marshal.SizeOf(_positions[0]));
     ShaderUtil.CalcWorkSize(capacity, out SimSizeX, out SimSizeY, out SimSizeZ);
 }
	static public int constructor(IntPtr l) {
		try {
			UnityEngine.ComputeShader o;
			o=new UnityEngine.ComputeShader();
			pushValue(l,true);
			pushValue(l,o);
			return 2;
		}
		catch(Exception e) {
			return error(l,e);
		}
	}
Пример #25
0
        public LifeService(ComputeShader compute, int capacity)
        {
            _compute = compute;
            _kernelSimulate = compute.FindKernel(ShaderConst.KERNEL_SIMULATE_LIFE);
            _kernelUpload = compute.FindKernel(ShaderConst.KERNEL_UPLOAD_LIFE);
            _lifes = new float[capacity];
            Lifes = new ComputeBuffer(capacity, Marshal.SizeOf(typeof(float)));
            Lifes.SetData(_lifes);
            _uploader = new ComputeBuffer(INIT_CAPACITY, Marshal.SizeOf(typeof(float)));

            ShaderUtil.CalcWorkSize(capacity, out SimSizeX, out SimSizeY, out SimSizeZ);
        }
Пример #26
0
 public VelocityService(ComputeShader compute, int capacity)
 {
     _kernelUpload = compute.FindKernel(ShaderConst.KERNEL_UPLOAD_VELOCITY);
     _kernelClampVelocity = compute.FindKernel(ShaderConst.KERNEL_CLAMP_VELOCITY);
     _compute = compute;
     _velocities = new Vector2[capacity];
     V0 = new ComputeBuffer(capacity, Marshal.SizeOf(typeof(Vector2)));
     V1 = new ComputeBuffer(capacity, Marshal.SizeOf(typeof(Vector2)));
     V0.SetData(_velocities);
     V1.SetData(_velocities);
     _uploader = new ComputeBuffer(INITIAL_CAP, Marshal.SizeOf(_velocities[0]));
     ShaderUtil.CalcWorkSize(capacity, out SimSizeX, out SimSizeY, out SimSizeZ);
 }
Пример #27
0
 public BroadPhase(ComputeShader compute, ComputeShader computeSort, LifeService l, PositionService p)
 {
     var capacity = l.Lifes.count;
     _lifes = l;
     _positions = p;
     _kernelInitYs = compute.FindKernel(ShaderConst.KERNEL_INIT_BROADPHASE);
     _kernelSolve = compute.FindKernel (ShaderConst.KERNEL_SOVLE_BROADPHASE);
     _compute = compute;
     _sort = new BitonicMergeSort(computeSort);
     Keys = new ComputeBuffer(capacity, Marshal.SizeOf(typeof(uint)));
     _ys = new ComputeBuffer(capacity, Marshal.SizeOf(typeof(float)));
     Bands = new ComputeBuffer(capacity, Marshal.SizeOf(typeof(Band)));
 }
 static public int constructor(IntPtr l)
 {
     LuaDLL.lua_remove(l, 1);
     UnityEngine.ComputeShader o;
     if (matchType(l, 1))
     {
         o = new UnityEngine.ComputeShader();
         pushObject(l, o);
         return(1);
     }
     LuaDLL.luaL_error(l, "New object failed.");
     return(0);
 }
Пример #29
0
    void CreateAssets()
    {
        _material = Resources.Load<Material>("ParticleInstancingMat");

        _shaderCompute = Resources.Load<ComputeShader>("ParticleMeshCompute");

        _initKernelId = _shaderCompute.FindKernel(kInitKernel);
        _updateKernelId = _shaderCompute.FindKernel(kUpdateKernel);

        _instancesCount = _mesh.vertices.Length/3;
        _numThreadGroupsX = Mathf.CeilToInt(((float)_instancesCount) / ((float)kNumThreadsX));
        Debug.Log("_numThreadGroupsX " + _numThreadGroupsX);
    }
Пример #30
0
 static public int constructor(IntPtr l)
 {
     try {
         UnityEngine.ComputeShader o;
         o = new UnityEngine.ComputeShader();
         pushValue(l, o);
         return(1);
     }
     catch (Exception e) {
         LuaDLL.luaL_error(l, e.ToString());
         return(0);
     }
 }
 private void ShowDebuggingData(ComputeShader cs)
 {
   GUILayout.Space(5f);
   GUILayout.Label("Compiled code:", EditorStyles.boldLabel, new GUILayoutOption[0]);
   EditorGUILayout.BeginHorizontal();
   EditorGUILayout.PrefixLabel("All variants", EditorStyles.miniButton);
   if (GUILayout.Button(ComputeShaderInspector.Styles.showAll, EditorStyles.miniButton, new GUILayoutOption[1]{ GUILayout.ExpandWidth(false) }))
   {
     ShaderUtil.OpenCompiledComputeShader(cs, true);
     GUIUtility.ExitGUI();
   }
   EditorGUILayout.EndHorizontal();
 }
Пример #32
0
 static public int constructor(IntPtr l)
 {
     try {
         UnityEngine.ComputeShader o;
         o = new UnityEngine.ComputeShader();
         pushValue(l, true);
         pushValue(l, o);
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
 public static int constructor(IntPtr l)
 {
     try {
         UnityEngine.ComputeShader o;
         o=new UnityEngine.ComputeShader();
         pushValue(l,o);
         return 1;
     }
     catch(Exception e) {
         LuaDLL.luaL_error(l, e.ToString());
         return 0;
     }
 }
Пример #34
0
    void CreateAssets()
    {
        _material = Resources.Load<Material>("ParticleImageMat");

        _shaderCompute = Resources.Load<ComputeShader>("ParticleImageCompute");
        _initImageKernelId = _shaderCompute.FindKernel(kInitParticlesKernel);
        _updateParticlesKernel = _shaderCompute.FindKernel(kUpdateParticlesKernel);

        _numThreadGroupsX = Mathf.CeilToInt(_image.width / kNumThreadsX);
        _numThreadGroupsY = Mathf.CeilToInt(_image.height / kNumThreadsY);
        _numThreadGroupsZ = Mathf.Max(Mathf.CeilToInt(_imageLayers / kNumThreadsX), 1);

        _totalNumParticles = _image.width * _image.height * _imageLayers;
    }
Пример #35
0
 static public int FindKernel(IntPtr l)
 {
     try {
         UnityEngine.ComputeShader self = (UnityEngine.ComputeShader)checkSelf(l);
         System.String             a1;
         checkType(l, 2, out a1);
         var ret = self.FindKernel(a1);
         pushValue(l, ret);
         return(1);
     }
     catch (Exception e) {
         LuaDLL.luaL_error(l, e.ToString());
         return(0);
     }
 }
Пример #36
0
 static public int HasKernel(IntPtr l)
 {
     try {
         UnityEngine.ComputeShader self = (UnityEngine.ComputeShader)checkSelf(l);
         System.String             a1;
         checkType(l, 2, out a1);
         var ret = self.HasKernel(a1);
         pushValue(l, true);
         pushValue(l, ret);
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
Пример #37
0
        public HashGrid(ComputeShader compute, ComputeShader computeSort, LifeService l, PositionService p, GridService.Grid g)
        {
            var capacity = l.Lifes.count;
            _compute = compute;
            _lifes = l;
            _positions = p;

            _kernelSolve = compute.FindKernel (ShaderConst.KERNEL_SOVLE_COLLISION_DETECTION);
            _sort = new BitonicMergeSort(computeSort);
            _keys = new ComputeBuffer(capacity, Marshal.SizeOf(typeof(uint)));
            CollisionData = new Collision[capacity];
            Collisions = new ComputeBuffer(capacity, Marshal.SizeOf(typeof(Collision)));
            _hashes = new HashService(compute, l, p);
            _grid = new GridService(compute, g, _hashes);
        }
Пример #38
0
        public GridService(ComputeShader compute, Grid g, HashService h)
        {
            _compute = compute;
            _grid = g;
            _hashes = h;
            _kernelInit = compute.FindKernel(ShaderConst.KERNEL_INIT_GRID);
            _kernelConstruct = compute.FindKernel(ShaderConst.KERNEL_CONSTRUCT_GRID);
            var gridCellCount = g.nx * g.ny;

            StartData = new uint[gridCellCount];
            EndData = new uint[gridCellCount];
            _hashGridStart = new ComputeBuffer(gridCellCount, Marshal.SizeOf(typeof(uint)));
            _hashGridEnd = new ComputeBuffer(gridCellCount, Marshal.SizeOf(typeof(uint)));
            _hashGridStart.SetData (StartData);
            _hashGridEnd.SetData (EndData);
        }
Пример #39
0
 static public int SetFloats(IntPtr l)
 {
     try {
         UnityEngine.ComputeShader self = (UnityEngine.ComputeShader)checkSelf(l);
         System.String             a1;
         checkType(l, 2, out a1);
         System.Single[] a2;
         checkParams(l, 3, out a2);
         self.SetFloats(a1, a2);
         return(0);
     }
     catch (Exception e) {
         LuaDLL.luaL_error(l, e.ToString());
         return(0);
     }
 }
Пример #40
0
 static public int SetInt(IntPtr l)
 {
     try {
         UnityEngine.ComputeShader self = (UnityEngine.ComputeShader)checkSelf(l);
         System.String             a1;
         checkType(l, 2, out a1);
         System.Int32 a2;
         checkType(l, 3, out a2);
         self.SetInt(a1, a2);
         pushValue(l, true);
         return(1);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
Пример #41
0
 public static void ReadFromRenderTexture(RenderTexture tex, int channels, ComputeBuffer buffer, ComputeShader readData)
 {
     if (tex == null)
     {
         Debug.Log("CBUtility::ReadFromRenderTexture - RenderTexture is null");
         return;
     }
     if (buffer == null)
     {
         Debug.Log("CBUtility::ReadFromRenderTexture - buffer is null");
         return;
     }
     if (readData == null)
     {
         Debug.Log("CBUtility::ReadFromRenderTexture - Computer shader is null");
         return;
     }
     if (channels < 1 || channels > 4)
     {
         Debug.Log("CBUtility::ReadFromRenderTexture - Channels must be 1, 2, 3, or 4");
         return;
     }
     if (!tex.IsCreated())
     {
         Debug.Log("CBUtility::ReadFromRenderTexture - tex has not been created (Call Create() on tex)");
         return;
     }
     int num = 1;
     int num2 = readData.FindKernel(CBUtility.readNames2D[channels - 1, 0]);
     readData.SetTexture(num2, CBUtility.readNames2D[channels - 1, 1], tex);
     readData.SetBuffer(num2, CBUtility.readNames2D[channels - 1, 2], buffer);
     if (num2 == -1)
     {
         Debug.Log("CBUtility::ReadFromRenderTexture - could not find kernels");
         return;
     }
     int width = tex.width;
     int height = tex.height;
     readData.SetInt("_Width", width);
     readData.SetInt("_Height", height);
     readData.SetInt("_Depth", num);
     int num3 = (width % 8 != 0) ? 1 : 0;
     int num4 = (height % 8 != 0) ? 1 : 0;
     int num5 = (num % 8 != 0) ? 1 : 0;
     readData.Dispatch(num2, Mathf.Max(1, width / 8 + num3), Mathf.Max(1, height / 8 + num4), Mathf.Max(1, num / 8 + num5));
 }
Пример #42
0
 static public int SetBuffer(IntPtr l)
 {
     try {
         UnityEngine.ComputeShader self = (UnityEngine.ComputeShader)checkSelf(l);
         System.Int32 a1;
         checkType(l, 2, out a1);
         System.String a2;
         checkType(l, 3, out a2);
         UnityEngine.ComputeBuffer a3;
         checkType(l, 4, out a3);
         self.SetBuffer(a1, a2, a3);
         return(0);
     }
     catch (Exception e) {
         LuaDLL.luaL_error(l, e.ToString());
         return(0);
     }
 }
Пример #43
0
    public RotationService(ComputeShader c, VelocityService v, LifeService l, float radius, Vector2 angularSpeedRange)
    {
        Count = v.V0.count;
        _compute = c;
        _velocities = v;
        _lifes = l;
        _kernelVelocityBasedRotate = c.FindKernel(KERNEL_VELOCITY_BASED_ROTATE);
        _data = new Vector4[Count];
        _rotations = new ComputeBuffer(Count, Marshal.SizeOf(typeof(Vector4)));

        for (var i = 0; i < Count; i++)
            _data[i] = QuaternionExtension.IDENTITY;
        Upload();
        ShaderUtil.CalcWorkSize(Count, out SimSizeX, out SimSizeY, out SimSizeZ);

        _angularSpeedData = new float[ShaderConst.WARP_SIZE];
        _angularSpeed = new ComputeBuffer(_angularSpeedData.Length, Marshal.SizeOf(typeof(float)));
        UpdateAngularSpeed(angularSpeedRange);
    }
Пример #44
0
    /*
    public bool CreateChunk(int octreeSize, Vector3 position)
    {
        min = position - new Vector3(octreeSize / 2, octreeSize / 2, octreeSize / 2);
        Vector3 octreePos = new Vector3(position.x - (octreeSize / 2), position.y - (octreeSize / 2), position.z - (octreeSize / 2));
        root = tree.BuildOctree(octreePos, octreeSize);

        if (root == null && meshObject != null)
        {
            DestroyMesh();
        }

        return root != null;
    }
    */
    public bool CreateChunk(ComputeShader computeShader, int lod, Vector3 position)
    {
        LOD = lod;
        if (LOD < 1)
            LOD = 1;
        else if (LOD > 6)
            LOD = 6;
        int octreeSize = (int) Mathf.Pow(2, LOD);

        min = position - new Vector3(octreeSize / 2, octreeSize / 2, octreeSize / 2);
        Vector3 octreePos = new Vector3(position.x - (octreeSize / 2), position.y - (octreeSize / 2), position.z - (octreeSize / 2));
        root = tree.BuildOctree(computeShader, octreePos, octreeSize);

        if (root == null && meshObject != null)
        {
            DestroyMesh();
        }

        return root != null;
    }
Пример #45
0
 static public int GetKernelThreadGroupSizes(IntPtr l)
 {
     try {
         UnityEngine.ComputeShader self = (UnityEngine.ComputeShader)checkSelf(l);
         System.Int32 a1;
         checkType(l, 2, out a1);
         System.UInt32 a2;
         System.UInt32 a3;
         System.UInt32 a4;
         self.GetKernelThreadGroupSizes(a1, out a2, out a3, out a4);
         pushValue(l, true);
         pushValue(l, a2);
         pushValue(l, a3);
         pushValue(l, a4);
         return(4);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
Пример #46
0
 static public int Dispatch(IntPtr l)
 {
     try {
         UnityEngine.ComputeShader self = (UnityEngine.ComputeShader)checkSelf(l);
         System.Int32 a1;
         checkType(l, 2, out a1);
         System.Int32 a2;
         checkType(l, 3, out a2);
         System.Int32 a3;
         checkType(l, 4, out a3);
         System.Int32 a4;
         checkType(l, 5, out a4);
         self.Dispatch(a1, a2, a3, a4);
         pushValue(l, true);
         return(1);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
Пример #47
0
 static public int GetKernelThreadGroupSizes(IntPtr l)
 {
     try {
                     #if DEBUG
         var    method     = System.Reflection.MethodBase.GetCurrentMethod();
         string methodName = GetMethodName(method);
                     #if UNITY_5_5_OR_NEWER
         UnityEngine.Profiling.Profiler.BeginSample(methodName);
                     #else
         Profiler.BeginSample(methodName);
                     #endif
                     #endif
         UnityEngine.ComputeShader self = (UnityEngine.ComputeShader)checkSelf(l);
         System.Int32 a1;
         checkType(l, 2, out a1);
         System.UInt32 a2;
         checkType(l, 3, out a2);
         System.UInt32 a3;
         checkType(l, 4, out a3);
         System.UInt32 a4;
         checkType(l, 5, out a4);
         self.GetKernelThreadGroupSizes(a1, out a2, out a3, out a4);
         pushValue(l, true);
         pushValue(l, a2);
         pushValue(l, a3);
         pushValue(l, a4);
         return(4);
     }
     catch (Exception e) {
         return(error(l, e));
     }
             #if DEBUG
     finally {
                     #if UNITY_5_5_OR_NEWER
         UnityEngine.Profiling.Profiler.EndSample();
                     #else
         Profiler.EndSample();
                     #endif
     }
             #endif
 }
Пример #48
0
    public void Init()
    {
        _grassShader = Resources.Load<Shader>("Shaders/GrassGeneratorShader");
        _grassMaterial = Resources.Load<Material>("GrassGeneratorMat");
        _noiseTex = Resources.Load<Texture>("Noise");
        if(_noiseTex == null)
        {
            Debug.LogError("Not found noise");
        }

        _grassComputeShader = Resources.Load<ComputeShader>("ComputeShaders/GrassComputeShader");
        _initGrassKernelId = _grassComputeShader.FindKernel(kInitGrassKernel);
        _updateGrassKernelId = _grassComputeShader.FindKernel(kUpdateGrassKernel);

        _numGrassItems = _numGroupGrassX*_numGroupGrassY*kThreadsX*kThreadsY;
        _grassBuffer = new ComputeBuffer(_numGrassItems, System.Runtime.InteropServices.Marshal.SizeOf(typeof(GrassData)));
        _obstaclesBuffer = new ComputeBuffer(kMaxObstacles, System.Runtime.InteropServices.Marshal.SizeOf(typeof(ObstacleData)));

        _grassComputeShader.SetFloat("_Width", _numGroupGrassX*kThreadsX);
        _grassComputeShader.SetFloat("_Height", _numGroupGrassY*kThreadsY);
        _grassComputeShader.SetTexture(_initGrassKernelId, "_NoiseTex", _noiseTex);

        _grassMaterial.SetTexture("_NoiseTex", _noiseTex);
        _grassMaterial.SetFloat("_Width", _numGroupGrassX*kThreadsX);
        _grassMaterial.SetFloat("_Height", _numGroupGrassY*kThreadsY);

        _grassComputeShader.SetBuffer(_initGrassKernelId, "_GrassBuffer", _grassBuffer);
        _grassComputeShader.SetBuffer(_updateGrassKernelId, "_GrassBuffer", _grassBuffer);
        _grassComputeShader.SetBuffer(_updateGrassKernelId, "_ObstaclesBuffer", _obstaclesBuffer);
        _grassComputeShader.SetInt("_NumObstacles", 0);
        _grassMaterial.SetBuffer("_GrassBuffer", _grassBuffer);

        _grassComputeShader.Dispatch(_initGrassKernelId, _numGroupGrassX, _numGroupGrassY, 1);
        #if GRASS_CPU
        _grassDataTestCPU = new GrassData[_numGrassItems];
        _grassBuffer.GetData(_grassDataTestCPU);
        #endif
        _isInit = true;
    }
 static public int SetTextureFromGlobal(IntPtr l)
 {
     try {
         int argc = LuaDLL.lua_gettop(l);
         if (matchType(l, "SetTextureFromGlobal__Int32__Int32__Int32", argc, 2, typeof(int), typeof(int), typeof(int)))
         {
             UnityEngine.ComputeShader self = (UnityEngine.ComputeShader)checkSelf(l);
             System.Int32 a1;
             checkType(l, 3, out a1);
             System.Int32 a2;
             checkType(l, 4, out a2);
             System.Int32 a3;
             checkType(l, 5, out a3);
             self.SetTextureFromGlobal(a1, a2, a3);
             pushValue(l, true);
             return(1);
         }
         else if (matchType(l, "SetTextureFromGlobal__Int32__String__String", argc, 2, typeof(int), typeof(string), typeof(string)))
         {
             UnityEngine.ComputeShader self = (UnityEngine.ComputeShader)checkSelf(l);
             System.Int32 a1;
             checkType(l, 3, out a1);
             System.String a2;
             checkType(l, 4, out a2);
             System.String a3;
             checkType(l, 5, out a3);
             self.SetTextureFromGlobal(a1, a2, a3);
             pushValue(l, true);
             return(1);
         }
         pushValue(l, false);
         LuaDLL.lua_pushstring(l, "No matched override function to call");
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
Пример #50
0
    /// <summary>
    /// Uses the GPU to process an array of 4D coordinates for noise and return an array with the noise at the specified coordinates.
    /// </summary>
    /// <returns>Float array</returns>
    /// <param name="positions"> Array of coordinates to process. </param>
    /// <param name="noiseScale"> Value to scale the noise coordinates by. </param>
    /// <param name="normalize"> Whether or not to remap the noise from (-1, 1) to (0, 1). </param>
    public static float[] NoiseArrayGPU(UnityEngine.Vector4[] positions, float noiseScale = 0.01f, bool normalize = true)
    {
        UnityEngine.ComputeShader shader = UnityEngine.Resources.Load(shaderPath) as UnityEngine.ComputeShader;
        if (shader == null)
        {
            UnityEngine.Debug.LogError(noShaderMsg);
            return(null);
        }

        int kernel = shader.FindKernel("ComputeNoiseArray");

        SetShaderVars(shader, UnityEngine.Vector2.zero, normalize, noiseScale);
        shader.SetInt("dimension", 4);

        UnityEngine.ComputeBuffer permBuffer = new UnityEngine.ComputeBuffer(perm.Length, 4);
        permBuffer.SetData(perm);
        shader.SetBuffer(kernel, "perm", permBuffer);

        UnityEngine.ComputeBuffer posBuffer = new UnityEngine.ComputeBuffer(positions.Length, 16);
        posBuffer.SetData(positions);
        shader.SetBuffer(kernel, "float4Array", posBuffer);

        UnityEngine.ComputeBuffer outputBuffer = new UnityEngine.ComputeBuffer(positions.Length, 4);
        shader.SetBuffer(kernel, "outputArray", outputBuffer);

        shader.Dispatch(kernel, positions.Length / 14, 1, 1);

        float[] outputData = new float[positions.Length];
        outputBuffer.GetData(outputData);

        permBuffer.Release();
        posBuffer.Release();
        outputBuffer.Release();

        return(outputData);
    }
Пример #51
0
 static public int DispatchIndirect(IntPtr l)
 {
     try {
         int argc = LuaDLL.lua_gettop(l);
         if (argc == 3)
         {
             UnityEngine.ComputeShader self = (UnityEngine.ComputeShader)checkSelf(l);
             System.Int32 a1;
             checkType(l, 2, out a1);
             UnityEngine.ComputeBuffer a2;
             checkType(l, 3, out a2);
             self.DispatchIndirect(a1, a2);
             pushValue(l, true);
             return(1);
         }
         else if (argc == 4)
         {
             UnityEngine.ComputeShader self = (UnityEngine.ComputeShader)checkSelf(l);
             System.Int32 a1;
             checkType(l, 2, out a1);
             UnityEngine.ComputeBuffer a2;
             checkType(l, 3, out a2);
             System.UInt32 a3;
             checkType(l, 4, out a3);
             self.DispatchIndirect(a1, a2, a3);
             pushValue(l, true);
             return(1);
         }
         pushValue(l, false);
         LuaDLL.lua_pushstring(l, "No matched override function DispatchIndirect to call");
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
Пример #52
0
 private static extern void INTERNAL_CALL_SetMatrix(ComputeShader self, int nameID, ref Matrix4x4 val);
Пример #53
0
 private static extern void INTERNAL_CALL_SetVector(ComputeShader self, int nameID, ref Vector4 val);
Пример #54
0
 public void SetVector(int nameID, Vector4 val)
 {
     ComputeShader.INTERNAL_CALL_SetVector(this, nameID, ref val);
 }
Пример #55
0
 private static extern void INTERNAL_CALL_SetVector(ComputeShader self, int nameID, ref Vector4 val);
Пример #56
0
 /// <summary>
 ///   <para>Set a vector parameter.</para>
 /// </summary>
 /// <param name="name">Variable name in shader code.</param>
 /// <param name="val">Value to set.</param>
 public void SetVector(string name, Vector4 val)
 {
     ComputeShader.INTERNAL_CALL_SetVector(this, name, ref val);
 }
Пример #57
0
 private static extern void INTERNAL_CALL_SetVector(ComputeShader self, string name, ref Vector4 val);
Пример #58
0
 public void SetMatrix(int nameID, Matrix4x4 val)
 {
     ComputeShader.INTERNAL_CALL_SetMatrix(this, nameID, ref val);
 }