Ejemplo n.º 1
0
	// tick: renders one frame
	public void Tick()
	{
		// initialize timer
		if (firstFrame)
		{
			timer.Reset();
			timer.Start();
			firstFrame = false;
		}
		// handle keys, only when running time set to -1 (infinite)
		if (runningTime == -1) if (camera.HandleInput())
		{
			// camera moved; restart
			ClearAccumulator();
		}
		// render
		if (false) // if (useGPU)
		{
			// add your CPU + OpenCL path here
			// mind the gpuPlatform parameter! This allows us to specify the platform on our
			// test system.
			// note: it is possible that the automated test tool provides you with a different
			// platform number than required by your hardware. In that case, you can hardcode
			// the platform during testing (ignoring gpuPlatform); do not forget to put back
			// gpuPlatform before submitting!
		}
		else
		{
			// this is your CPU only path
			float scale = 1.0f / (float)++spp;
			for( int y = 0; y < screen.height; y++ )
			{
				for( int x = 0; x < screen.width; x++ )
				{
					// generate primary ray
					Ray ray = camera.Generate( RTTools.GetRNG(), x, y );
					// trace path
					int pixelIdx = x + y * screen.width;
					accumulator[pixelIdx] += Sample( ray, 0 );
					// plot final color
					screen.pixels[pixelIdx] = RTTools.Vector3ToIntegerRGB( scale * accumulator[pixelIdx] );
				}
			}
		}
		// stop and report when max render time elapsed
		int elapsedSeconds = (int)(timer.ElapsedMilliseconds / 1000);
		if (runningTime != -1) if (elapsedSeconds >= runningTime)
		{
			OpenTKApp.Report( (int)timer.ElapsedMilliseconds, spp, screen );
		}
	}
Ejemplo n.º 2
0
        // tick: renders one frame
        public void Tick()
        {
            // initialize timer
            if (firstFrame)
            {
                timer.Reset();
                timer.Start();
                firstFrame = false;
            }
            // handle keys, only when running time set to -1 (infinite)
            if (runningTime == -1)
            {
                if (camera.HandleInput())
                {
                    // camera moved; restart
                    ClearAccumulator();
                }
            }
            // render
            if (false)     // if (useGPU)
            {
                // add your CPU + OpenCL path here
                // mind the gpuPlatform parameter! This allows us to specify the platform on our
                // test system.
                // note: it is possible that the automated test tool provides you with a different
                // platform number than required by your hardware. In that case, you can hardcode
                // the platform during testing (ignoring gpuPlatform); do not forget to put back
                // gpuPlatform before submitting!
                long[] workSize  = { screen.width, screen.height };
                long[] localSize = { 16, 2 };
                queue.Execute(kernel, null, workSize, null, null);
                queue.Finish();

                queue.ReadFromBuffer(outputBuffer, ref screen.pixels, true, null);
                Console.WriteLine(screen.pixels[0]);

                Random r = RTTools.GetRNG();
                for (int i = 0; i < 1000; i++)
                {
                    randoms[i] = (float)r.NextDouble();
                }
                rndBuffer    = new ComputeBuffer <float>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.UseHostPointer, randoms);
                cameraBuffer = new ComputeBuffer <Vector3>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.UseHostPointer, camera.toCL());
                kernel.SetMemoryArgument(6, cameraBuffer);
                kernel.SetMemoryArgument(7, rndBuffer);
            }
            else
            {
                // this is your CPU only path
                float scale = 1.0f / (float)++spp;

                Parallel.For(0, screen.height, y => {
                    for (int x = 0; x < screen.width; x++)
                    {
                        // generate primary ray
                        Ray ray = camera.Generate(RTTools.GetRNG(), x, y);
                        // trace path
                        int pixelIdx           = x + y * screen.width;
                        accumulator[pixelIdx] += Sample(ray, 0, x, y);
                        // plot final color
                        screen.pixels[pixelIdx] = RTTools.Vector3ToIntegerRGB(scale * accumulator[pixelIdx]);
                    }
                });
            }
            // stop and report when max render time elapsed
            int elapsedSeconds = (int)(timer.ElapsedMilliseconds / 1000);

            if (runningTime != -1)
            {
                if (elapsedSeconds >= runningTime)
                {
                    OpenTKApp.Report((int)timer.ElapsedMilliseconds, spp, screen);
                }
            }
        }
Ejemplo n.º 3
0
        // tick: renders one frame
        public void Tick()
        {
            //float t = 21.5f;
            // initialize timer
            if (firstFrame)
            {
                timer.Reset();
                timer.Start();
                firstFrame = false;
            }
            // handle keys, only when running time set to -1 (infinite)
            if (runningTime == -1) 
                if (camera.HandleInput())
                {
                    // camera moved; restart
                    ClearAccumulator();
                }
            // render
            if (useGPU)
            {
                
                // add your CPU + OpenCL path here
                // mind the gpuPlatform parameter! This allows us to specify the platform on our
                // test system.
                // note: it is possible that the automated test tool provides you with a different
                // platform number than required by your hardware. In that case, you can hardcode
                // the platform during testing (ignoring gpuPlatform); do not forget to put back
                // gpuPlatform before submitting!
                GL.Finish();
                // clear the screen
                screen.Clear(0);

                // do opencl stuff
                if (GLInterop)
                {
                    kernel.SetMemoryArgument(0, texBuffer);
                }
                else
                {
                    kernel.SetMemoryArgument(0, buffer);
                }
                
                // execute kernel
                //long[] workSize = { screen.width, screen.height };
                long[] localSize = { 8, 12 };
                long [] workSize = { screen.width , screen.height };
                if (GLInterop)
                {
                    List<ComputeMemory> c = new List<ComputeMemory>() { texBuffer };
                    queue.AcquireGLObjects(c, null);
                    queue.Execute(kernel, null, workSize, localSize, null);
                    queue.Finish();
                    queue.ReleaseGLObjects(c, null);
                }
                else
                {
                    camera.Update();
                    gpuCamera = new GPUCamera(camera.pos, camera.p1, camera.p2, camera.p3, camera.up, camera.right, screen.width, screen.height, camera.lensSize);
                    float scale = 1.0f / (float)++spp;
                    kernel.SetValueArgument(2, gpuCamera);
                    kernel.SetValueArgument(3, scale);
                    queue.Execute(kernel, null, workSize, localSize, null);
                    queue.Finish();
                    // fetch results
                    if (!GLInterop)
                    {
                        queue.ReadFromBuffer(buffer, ref data, true, null);

                        // visualize result
                        for (int y = 0; y < screen.height; y++) 
                            for (int x = 0; x < screen.width; x++)
                            {
                                int temp = x + y * screen.width;
                                screen.pixels[temp] = data[temp];
                            }
                    }
                }
            }
            else
            {
                // this is your CPU only path
                float scale = 1.0f / (float)++spp;
                Parallel.For(0, dataArray.Length, parallelOptions, (id) =>
                {
                    for (int j = dataArray[id].y1; j < dataArray[id].y2 && j < screen.height; j++)
                    {
                        for (int i = dataArray[id].x1; i < dataArray[id].x2 && i < screen.width; i++)
                        {
                            // generate primary ray
                            Ray ray = camera.Generate(RTTools.GetRNG(), i, j);
                            // trace path
                            int pixelIdx = i + j * screen.width;
                            accumulator[pixelIdx] += Sample(ray, 0);
                            // plot final color
                            screen.pixels[pixelIdx] = RTTools.Vector3ToIntegerRGB(scale * accumulator[pixelIdx]);
                        }
                    }
                });
            }

            // stop and report when max render time elapsed
            int elapsedSeconds = (int)(timer.ElapsedMilliseconds / 1000);
            if (runningTime != -1) if (elapsedSeconds >= runningTime)
                {
                    OpenTKApp.Report((int)timer.ElapsedMilliseconds, spp, screen);
                }
        }