예제 #1
0
        private void Controller3DDraw(Controller3D c3d, ulong time)
        {
            long t1 = hptimer.ElapsedTicks;
            //TBD
            //    GL.Finish();      // use GL finish to ensure last frame is done - if we are operating above sys tick rate, this will be small time. If we are rendering too much, it will stall
            long t2 = hptimer.ElapsedTicks;

            GLMatrixCalcUniformBlock mcb = ((GLMatrixCalcUniformBlock)items.UB("MCUB"));

            mcb.SetFull(gl3dcontroller.MatrixCalc);        // set the matrix unform block to the controller 3d matrix calc.

            // set up the grid shader size

            if (gridrenderable != null)
            {
                gridrenderable.InstanceCount = gridvertshader.ComputeGridSize(gl3dcontroller.MatrixCalc.EyeDistance, out lastgridwidth);
                lasteyedistance = gl3dcontroller.MatrixCalc.EyeDistance;

                gridvertshader.SetUniforms(gl3dcontroller.MatrixCalc.LookAt, lastgridwidth, gridrenderable.InstanceCount);
            }

            if (gridbitmapvertshader != null)
            {
                float coordfade = lastgridwidth == 10000 ? (0.7f - (c3d.MatrixCalc.EyeDistance / 20000).Clamp(0.0f, 0.7f)) : 0.7f;
                Color coordscol = Color.FromArgb(coordfade < 0.05 ? 0 : 150, Color.Cyan);
                gridbitmapvertshader.ComputeUniforms(lastgridwidth, gl3dcontroller.MatrixCalc, gl3dcontroller.PosCamera.CameraDirection, coordscol, Color.Transparent);
            }

            if (edsmgalmapregions != null)
            {
                edsmgalmapregions.SetY(gl3dcontroller.PosCamera.LookAt.Y);
            }

            if (elitemapregions != null)
            {
                elitemapregions.SetY(gl3dcontroller.PosCamera.LookAt.Y);
            }

            // set the coords fader


            // set the galaxy volumetric block

            if (galaxyrenderable != null)
            {
                galaxyrenderable.InstanceCount = volumetricblock.Set(gl3dcontroller.MatrixCalc, volumetricboundingbox, gl3dcontroller.MatrixCalc.InPerspectiveMode ? 50.0f  : 0);        // set up the volumentric uniform
                //System.Diagnostics.Debug.WriteLine("GI {0}", galaxyrendererable.InstanceCount);
                galaxyshader.SetDistance(gl3dcontroller.MatrixCalc.InPerspectiveMode ? c3d.MatrixCalc.EyeDistance : -1f);
            }

            long t3 = hptimer.ElapsedTicks;

            if (travelpath != null)
            {
                travelpath.Update(time, gl3dcontroller.MatrixCalc.EyeDistance);
            }

            if (galmapobjects != null)
            {
                galmapobjects.Update(time, gl3dcontroller.MatrixCalc.EyeDistance);
            }

            if (galaxystars != null)
            {
                galaxystars.Update(time, gl3dcontroller.MatrixCalc.EyeDistance);
            }

            if (galaxystars != null && gl3dcontroller.MatrixCalc.EyeDistance < 400)
            {
                galaxystars.Request9BoxConditional(gl3dcontroller.PosCamera.LookAt);
            }


            long t4 = hptimer.ElapsedTicks;

            //int[] queryID = new int[2];
            //GL.GenQueries(2, queryID);
            //GL.QueryCounter(queryID[0], QueryCounterTarget.Timestamp);

            var tmr1 = new GLOperationQueryTimeStamp();
            var tmr2 = new GLOperationQueryTimeStamp();

            tmr1.Execute(null);

            rObjects.Render(glwfc.RenderState, gl3dcontroller.MatrixCalc, verbose: false);
            tmr2.Execute(null);

            // GL.QueryCounter(queryID[1], QueryCounterTarget.Timestamp);

            //   GL.Flush(); // ensure everything is in the grapghics pipeline

            //while (tmr2.IsAvailable() == false)
            //    ;

            long a = tmr1.GetCounter();
            long b = tmr2.GetCounter();

            //int done = 0;
            //while (done == 0)
            //{
            //    GL.GetQueryObject(queryID[1], GetQueryObjectParam.QueryResultAvailable, out done);
            //}

            //GL.GetQueryObject(queryID[0], GetQueryObjectParam.QueryResult, out long a);
            //GL.GetQueryObject(queryID[1], GetQueryObjectParam.QueryResult, out long b);
            //System.Diagnostics.Debug.WriteLine($"timer {a} {b} {b-a}" );

            long t5 = hptimer.ElapsedTicks;

            if (debugbuffer != null)
            {
                GLMemoryBarrier.All();
                Vector4[] debugout = debugbuffer.ReadVector4s(0, 4);
                System.Diagnostics.Debug.WriteLine("{0},{1},{2},{3}", debugout[0], debugout[1], debugout[2], debugout[3]);
            }

            long t    = hptimer.ElapsedMilliseconds;
            long diff = t - lastms;

            for (int i = frametimes.Length - 1; i > 0; i--)
            {
                frametimes[i] = frametimes[i - 1];
            }

            frametimes[0] = diff;

            lastms = t;
            double fps = (1000.0 / diff);

            if (fpsavg <= 1)
            {
                fpsavg = fps;
            }
            else
            {
                fpsavg = (fpsavg * 0.95) + fps * 0.05;
            }

            tmr1.Dispose();
            tmr2.Dispose();
            if (diff > 0)
            {
//                System.Diagnostics.Debug.Write($"Frame {hptimer.ElapsedMilliseconds,6} {diff,3} fps {fpsavg:#.0} frames {frametimes[0],3} {frametimes[1],3} {frametimes[2],3} {frametimes[3],3} {frametimes[4],3} {frametimes[5],3} sec {galaxystars.Sectors,3}");
//                System.Diagnostics.Debug.WriteLine($" finish {(t2 - t1) * 1000000 / Stopwatch.Frequency,5} t3 {(t3 - t2) * 1000000 / Stopwatch.Frequency,4} t4 {(t4 - t3) * 1000000 / Stopwatch.Frequency,4} render {(t5 - t4) * 1000000 / Stopwatch.Frequency,5} tot {(t5 - t1) * 1000000 / Stopwatch.Frequency,5}");
            }
            //            this.Text = "FPS " + fpsavg.ToString("N0") + " Looking at " + gl3dcontroller.MatrixCalc.TargetPosition + " eye@ " + gl3dcontroller.MatrixCalc.EyePosition + " dir " + gl3dcontroller.Pos.CameraDirection + " Dist " + gl3dcontroller.MatrixCalc.EyeDistance + " Zoom " + gl3dcontroller.Pos.ZoomFactor;
        }