Esempio n. 1
0
        void CopyTransform(Transform ubo, UniformData uniform)
        {
            var map = device.MapMemory(uniform.Memory, 0, uniform.AllocSize);

            var    size  = Marshal.SizeOf(typeof(Transform));
            var    bytes = new byte[size];
            IntPtr ptr   = Marshal.AllocHGlobal(size);

            Marshal.StructureToPtr(ubo, ptr, true);
            Marshal.Copy(ptr, bytes, 0, size);
            Marshal.FreeHGlobal(ptr);

            Marshal.Copy(bytes, 0, map, size);

            device.UnmapMemory(uniform.Memory);
        }
Esempio n. 2
0
        UniformData CreateUniformBuffer(Type targetType)
        {
            var data = new UniformData();
            var size = Marshal.SizeOf(targetType);

            data.Buffer = CreateBuffer((ulong)size, BufferUsageFlags.UniformBuffer);

            var memRequirements = device.GetBufferMemoryRequirements(data.Buffer);
            var memoryIndex     = FindMemoryIndex(MemoryPropertyFlags.HostVisible);
            var memAlloc        = new MemoryAllocateInfo(memRequirements.Size, memoryIndex);

            data.Memory = BindBuffer(data.Buffer, memAlloc);

            data.Descriptor = new DescriptorBufferInfo(data.Buffer, 0, (ulong)size);

            data.AllocSize = (uint)memAlloc.AllocationSize;

            return(data);
        }
Esempio n. 3
0
        //static int i;

        void OnRenderCallback(Queue queue, CommandBuffer[] cmdBuffer, SwapchainData swapchainData, UniformData uniformData, Transform transform)
        {
            //transform.model *= OpenTK.Matrix4.CreateRotationX(i++);
            //CopyUBO(transform, uniformData);

            var semaphoreCreateInfo = new SemaphoreCreateInfo();
            var presentSemaphore    = device.CreateSemaphore(semaphoreCreateInfo);

            semaphoreCreateInfo.Dispose();

            var currentBufferIndex = (int)device.AcquireNextImageKHR(swapchainData.Swapchain, ulong.MaxValue, presentSemaphore, null);

            SubmitForExecution(queue, presentSemaphore, cmdBuffer[currentBufferIndex]);

            var presentInfo = new PresentInfoKHR(new[] { swapchainData.Swapchain }, new[] { (uint)currentBufferIndex });

            queue.PresentKHR(presentInfo);
            presentInfo.Dispose();

            queue.WaitIdle();

            device.DestroySemaphore(presentSemaphore);
        }
Esempio n. 4
0
        DescriptorSet CreateDescriptorSet(DescriptorPool pool, DescriptorSetLayout layout, ImageData imageData, UniformData uniformData)
        {
            var allocInfo           = new DescriptorSetAllocateInfo(pool, new[] { layout });
            var sets                = device.AllocateDescriptorSets(allocInfo);
            var descriptorSet       = sets.First();
            var texDescriptor       = new DescriptorImageInfo(imageData.Sampler, imageData.View, ImageLayout.General);
            var writeDescriptorSets = new[]
            {
                new WriteDescriptorSet(descriptorSet, 0, 0, DescriptorType.UniformBuffer, null, null, null),
                new WriteDescriptorSet(descriptorSet, 1, 0, DescriptorType.CombinedImageSampler, null, null, null),
            };

            writeDescriptorSets[0].BufferInfo = new[] { uniformData.Descriptor };
            writeDescriptorSets[1].ImageInfo  = new[] { texDescriptor };
            device.UpdateDescriptorSets(writeDescriptorSets, null);
            return(descriptorSet);
        }
Esempio n. 5
0
        static void Main(string[] args)
        {
            if (!SharpDevice.IsDirectX11Supported())
            {
                System.Windows.Forms.MessageBox.Show("DirectX11 Not Supported");
                return;
            }

            //render form
            RenderForm form = new RenderForm();

            form.Text = "Tutorial 4: Primitives";

            //Help to count Frame Per Seconds
            SharpFPS fpsCounter = new SharpFPS();

            using (SharpDevice device = new SharpDevice(form)) {
                //Init Mesh
                Water water = new Water(device);

                //Create Shader From File and Create Input Layout
                SharpShader shader = new SharpShader(device, "../../HLSL.txt",
                                                     new SharpShaderDescription()
                {
                    VertexShaderFunction = "VS", PixelShaderFunction = "PS"
                },
                                                     new InputElement[] {
                    new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0),
                    new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 12, 0)
                });

                //create constant buffer
                Buffer11 buffer      = shader.CreateBuffer <UniformData>();
                Buffer11 arrayBuffer = shader.CreateBuffer <ArrayData>();


                fpsCounter.Reset();

                form.KeyDown += (sender, e) =>
                {
                    switch (e.KeyCode)
                    {
                    case Keys.W:
                        device.SetWireframeRasterState();
                        device.SetDefaultBlendState();
                        break;

                    case Keys.S:
                        device.SetDefaultRasterState();
                        break;

                    case Keys.D1:
                        device.SetDefaultBlendState();
                        break;

                    case Keys.D2:
                        device.SetBlend(BlendOperation.Add, BlendOption.InverseSourceAlpha, BlendOption.SourceAlpha);
                        break;

                    case Keys.D3:
                        device.SetBlend(BlendOperation.Add, BlendOption.SourceAlpha, BlendOption.InverseSourceAlpha);
                        break;

                    case Keys.D4:
                        device.SetBlend(BlendOperation.Add, BlendOption.SourceColor, BlendOption.InverseSourceColor);
                        break;

                    case Keys.D5:
                        device.SetBlend(BlendOperation.Add, BlendOption.SourceColor, BlendOption.DestinationColor);
                        break;
                    }
                };

                //main loop
                RenderLoop.Run(form, () => {
                    //Resizing
                    if (device.MustResize)
                    {
                        device.Resize();
                    }

                    //apply states
                    device.UpdateAllStates();

                    //clear color
                    device.Clear(Color.CornflowerBlue);

                    //apply shader
                    shader.Apply();

                    //Set matrices
                    float ratio       = (float)form.ClientRectangle.Width / (float)form.ClientRectangle.Height;
                    Matrix projection = Matrix.PerspectiveFovLH(3.14F / 3.0F, ratio, 1, 1000);
                    Matrix view       = Matrix.LookAtLH(new Vector3(0, 12, -12), new Vector3(), Vector3.UnitY);
                    Matrix world      = Matrix.RotationY(Environment.TickCount / 1000.0F * 0.1f);
                    world             = Matrix.Identity;


                    float time          = Environment.TickCount / 1000.0F;
                    Vector4[] waveStats = new Vector4[count] {
                        new Vector4(water.wave.speed * 1.0f, water.wave.wavelength * 1.0f, water.wave.amplitude * 1.0f, 0),
                        new Vector4(water.wave.speed * 1.0f, water.wave.wavelength * 1.0f, water.wave.amplitude * 1.0f, 0),
                        new Vector4(water.wave.speed * 1.0f, water.wave.wavelength * 1.0f, water.wave.amplitude * 1.0f, 0),
                    };

                    Vector4 tmp = new Vector4(2.0f, 0.0f, -2.0f, 0.0f);
                    tmp.Normalize();

                    Vector4[] waveDir = new Vector4[count] {
                        water.wave.waveDir,
                        new Vector4(water.wave.waveDir.Z, 0, -water.wave.waveDir.X, 0),
                        tmp,
                    };

                    var array = new Vector4[count] {
                        new Vector4(1.0f, 0.0f, 0.0f, 0.0f),
                        new Vector4(1.0f, 1.0f, 1.0f, 0.0f),
                        new Vector4(1.0f, 1.0f, 1.0f, 0.0f),
                    };

                    water.wave.waveDir = new Vector4((float)Math.Sin(time / 5.0), 0, (float)Math.Cos(time / 5.0), 0);

                    UniformData sceneInfo = new UniformData()
                    {
                        worldViewProj = world * view * projection,
                        worldView     = world * view,
                        time          = time,
                    };

                    ArrayData arrayInfo = new ArrayData()
                    {
                        array     = array,
                        waveDir   = waveDir,
                        waveStats = waveStats,
                    };


                    using (DataStream ds = new DataStream(Utilities.SizeOf <ArrayData>(), true, true)) {
                        //device.UpdateDataWithDataStream(arrayBuffer,ds);
                        ds.WriteRange(getBytes(arrayInfo));
                        ds.Position = 0;
                        arrayBuffer.Dispose();
                        arrayBuffer = shader.CreateBuffer <ArrayData>(ds);
                        //device.UnmapDataStream(arrayBuffer);
                    }

                    //update constant buffer
                    device.UpdateData <UniformData>(buffer, sceneInfo);

                    //pass constant buffer to shader
                    device.DeviceContext.VertexShader.SetConstantBuffer(0, buffer);
                    device.DeviceContext.PixelShader.SetConstantBuffer(0, buffer);
                    device.DeviceContext.VertexShader.SetConstantBuffer(1, arrayBuffer);


                    //draw mesh
                    water.Draw();

                    //begin drawing text
                    device.Font.Begin();

                    //draw string
                    fpsCounter.Update();
                    device.Font.DrawString("FPS: " + fpsCounter.FPS, 0, 0);

                    //flush text to view
                    device.Font.End();
                    //present
                    device.Present();
                });

                //release resources
                water.Dispose();
                buffer.Dispose();
            }
        }