public void Render(IRenderResult renderResult, RenderJobCallBackDelegate progressCallBackAction = null) { int total = m_Works.Count; Task[] tasks = new Task[m_Jobs.Length]; for (int i = 0; i < m_Jobs.Length; i++) { tasks[i] = Task.Run(new Action(m_Jobs[i].Render)); } while (m_Works.Count > 0) { m_ResetEvent.WaitOne(); m_ResetEvent.Reset(); //if (m_Results.Count > 0) //{ // T result = default(T); // if (!m_Results.TryDequeue(out result)) // break; // progressCallBackAction?.Invoke(total - m_Works.Count, total, result); //} progressCallBackAction?.Invoke(total - m_Works.Count, total); } //等待所有任务结束 Task.WaitAll(tasks); if (renderResult != null) { while (m_Results.Count > 0) { T result = default(T); if (!m_Results.TryDequeue(out result)) { return; } if (result != null) { result.ApplyToRenderResult(renderResult); } } } }
//public void SetPixel(int x, int y, Color color) //{ // int i = x - m_X; // int j = y - m_Y; // m_Colors[j * m_TileWidth + i] = color; //} //public Color GetPixel(int i, int j) //{ // return m_Colors[j * m_TileWidth + i]; //} public bool ApplyToRenderResult(IRenderResult result) { Texture texture = result as Texture; if (texture != null) { //for(int i=0;i< m_TileWidth; i++) //{ // for(int j=0;j< m_TileHeight; j++) // { // Color color = m_Colors[j * m_TileWidth + i]; // texture.SetPixel(m_X + i, m_Y + j, color); // } //} texture.SetPixel(m_X, m_Y, m_Color); return(true); } return(false); }
private void renderResultBox_MouseClick(object sender, MouseEventArgs e) { #if DEBUG if (this.pixelDebugCheckBox.Checked) { if (MessageBox.Show("开启单像素调试后点击画布将对单个像素渲染,这会清空之前的渲染结果,是否继续?", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes) { int traceTimes = string.IsNullOrEmpty(this.bounceInputBox.Text) ? 0 : int.Parse(this.bounceInputBox.Text); int numSamples = string.IsNullOrEmpty(this.numSampleInputBox.Text) ? 0 : int.Parse(this.numSampleInputBox.Text); var sampleType = (SamplerType)this.samplerTypeCombo.SelectedIndex; uint width = string.IsNullOrEmpty(this.widthInputBox.Text) ? 0 : uint.Parse(this.widthInputBox.Text); uint height = string.IsNullOrEmpty(this.heightInputBox.Text) ? 0 : uint.Parse(this.heightInputBox.Text); if (traceTimes <= 0) { MessageBox.Show("不允许反弹次数小于等于0!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (numSamples <= 0) { MessageBox.Show("不允许采样次数小于等于0!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (width <= 0 || height <= 0) { MessageBox.Show("请输入宽高合法的宽高!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } int w = ((PictureBox)sender).Width; int h = ((PictureBox)sender).Height; Log.Info($"点击了坐标:({w - 1 - e.Location.X},{h - 1 - e.Location.Y}),开始单像素渲染"); int x = (int)(((float)(w - 1 - e.Location.X)) / w * width); int y = (int)(((float)(h - 1 - e.Location.Y)) / h * height); Log.Info($"渲染目标像素:({x},{y})"); var pt = new ASL.PathTracer.PathTracer(m_Scene); RenderConfig config = new RenderConfig() { traceTimes = traceTimes, samplerType = sampleType, numSamples = numSamples, numSets = 83, width = width, height = height, }; m_Result = pt.RenderDebugSinglePixel(x, y, config); if (m_Result != null) { Log.CompleteInfo("渲染完成"); //this.renderResultBox.BackgroundImage = result.SaveToImage(); } else { Log.Err("渲染失败!"); } } } #endif }
//private void fastPreviewButton_Click(object sender, EventArgs e) //{ // if (m_Scene == null) // return; // uint width = string.IsNullOrEmpty(this.widthInputBox.Text) ? 0 : uint.Parse(this.widthInputBox.Text); // uint height = string.IsNullOrEmpty(this.heightInputBox.Text) ? 0 : uint.Parse(this.heightInputBox.Text); // if (width <= 0 || height <= 0) // { // MessageBox.Show("请输入宽高合法的宽高!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); // return; // } // Log.Info("开始快速预览"); // Stopwatch stopWatch = new Stopwatch(); // stopWatch.Start(); // this.progressBar.Value = 0; // this.progressBar.Maximum = 100; // m_Result = m_Scene.FastRender(width, height, this.ProgressCallBack); // stopWatch.Stop(); // if (m_Result != null) // { // Log.CompleteInfo($"渲染完成,总计用时:{stopWatch.ElapsedMilliseconds}"); // if (m_Bitmap != null) // { // m_Bitmap.Dispose(); // m_Bitmap = null; // } // m_Bitmap = m_Result.TransferToBMP(m_Bitmap, 0.45f); // this.renderResultBox.BackgroundImage = m_Bitmap; // this.progressBar.Value = 0; // } // else // Log.Err("渲染预览!"); //} private void renderButton_Click(object sender, EventArgs e) { if (m_Scene == null) { return; } int traceTimes = string.IsNullOrEmpty(this.bounceInputBox.Text)?0: int.Parse(this.bounceInputBox.Text); int numSamples = string.IsNullOrEmpty(this.numSampleInputBox.Text) ? 0 : int.Parse(this.numSampleInputBox.Text); var sampleType = (SamplerType)this.samplerTypeCombo.SelectedIndex; var renderChannel = (RenderChannel)this.renderChannelCombo.SelectedIndex; uint width = string.IsNullOrEmpty(this.widthInputBox.Text) ? 0 : uint.Parse(this.widthInputBox.Text); uint height = string.IsNullOrEmpty(this.heightInputBox.Text) ? 0 : uint.Parse(this.heightInputBox.Text); float exposure = -1.0f; if (this.tonemappingCheckBox.Checked && !string.IsNullOrEmpty(this.exposureInputBox.Text)) { exposure = float.Parse(this.exposureInputBox.Text); } //if (renderChannel == RenderChannel.Full && traceTimes <= 0) //{ // MessageBox.Show("不允许反弹次数小于等于0!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); // return; //} if (traceTimes < 0) { traceTimes = 0; } //if (renderChannel == RenderChannel.Full && numSamples <= 0) //{ // MessageBox.Show("不允许采样次数小于等于0!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); // return; //} if (numSamples <= 0) { numSamples = 1; } if (width <= 0 || height <= 0) { MessageBox.Show("请输入宽高合法的宽高!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } Log.Info("开始渲染"); Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); this.progressBar.Value = 0; this.progressBar.Maximum = 100; var pt = new ASL.PathTracer.PathTracer(m_Scene); //var pt = new ASL.PathTracer.VolumeTextureRenderer(m_Scene); ///var pt = new ASL.PathTracer.PRTRenderer(m_Scene); RenderConfig config = new RenderConfig() { traceTimes = traceTimes, samplerType = sampleType, numSamples = numSamples, numSets = 83, width = width, height = height, }; m_Result = pt.Render(config, this.ProgressCallBack); stopWatch.Stop(); if (m_Result != null) { Log.CompleteInfo($"渲染完成,总计用时:{stopWatch.ElapsedMilliseconds}"); if (m_Result is Texture) { if (m_Bitmap != null) { m_Bitmap.Dispose(); m_Bitmap = null; } var tex = m_Result as Texture; m_Bitmap = tex.TransferToBMP(m_Bitmap, 0.45f, exposure); this.renderResultBox.BackgroundImage = m_Bitmap; if (this.tonemappingCheckBox.Checked) { this.retonemappingButton.Enabled = true; } } else { this.renderResultBox.BackgroundImage = null; this.retonemappingButton.Enabled = false; } this.progressBar.Value = 0; } else { Log.Err("渲染失败!"); } }