Пример #1
0
        /// <summary>
        /// Converts a RenderSettings received from Client to the internal class
        /// Merge?
        /// </summary>
        public static BlenderRenderSettings FromRenderSettings(RenderPacketModel settings)
        {
            var result = new BlenderRenderSettings()
            {
                TaskID      = (!string.IsNullOrEmpty(settings.TaskID)) ? settings.TaskID : Guid.NewGuid().ToString(),
                X           = settings.X,
                X2          = settings.X2,
                Y           = settings.Y,
                Y2          = settings.Y2,
                Cores       = settings.Cores,
                Frame       = settings.Frame,
                TileHeight  = settings.TileHeight,
                TileWidth   = settings.TileWidth,
                Width       = settings.Width,
                Height      = settings.Height,
                Samples     = settings.Samples,
                ComputeUnit = settings.RenderType,
                Crop        = settings.Crop,
                Denoiser    = settings.Denoiser,
                FPS         = settings.FPS,
                Workaround  = settings.Workaround,
            };

            return(result);
        }
        public RenderResponse Packet_Render(RenderRequest req)
        {
            if (!_blender.IsVersionAvailable(req.Version))
            {
                return new RenderResponse()
                       {
                           TaskID  = req.TaskID,
                           Success = false,
                           Message = "Version not prepared.."
                       }
            }
            ;

            try
            {
                //Validate Settings
                string filePath = SessionData.GetFilePath(req.SessionID);

                if (filePath == null)
                {
                    return new RenderResponse()
                           {
                               TaskID  = req.TaskID,
                               Success = false,
                               Message = "Blend file was not available"
                           }
                }
                ;

                if (req.Settings == null)
                {
                    req.Settings = new Shared.RenderPacketModel();
                }
                if (req.Settings.Cores <= 0)
                {
                    req.Settings.Cores = Environment.ProcessorCount;
                }

                req.Settings.Cores = Math.Min(Environment.ProcessorCount, req.Settings.Cores);

                DateTime lastUpdate = DateTime.Now;

                List <string> exceptions = new List <string>();

                //Render
                string file = _blender.Render(req.Version, filePath,
                                              BlenderRenderSettings.FromRenderSettings(req.Settings),
                                              (process) =>
                {
                    process.OnBlenderStatus += (state) =>
                    {
                        if (DateTime.Now.Subtract(lastUpdate).TotalMilliseconds > UPDATE_TIMING_MS)
                        {
                            lastUpdate = DateTime.Now;
                            SendPacket(new RenderInfoResponse()
                            {
                                TaskID        = req.TaskID,
                                TilesFinished = state.TilesFinish,
                                TilesTotal    = state.TilesTotal,
                                Time          = state.Time,
                                TimeRemaining = state.TimeRemaining
                            });
                        }
                    };
                    process.OnBlenderException += (excp) => exceptions.Add(excp);
                });

                //Handle Result
                if (file == null || !File.Exists(file))
                {
                    if (exceptions.Count == 0)
                    {
                        return new RenderResponse()
                               {
                                   TaskID  = req.TaskID,
                                   Success = false,
                                   Message = "Missing Files?"
                               }
                    }
                    ;
                    else
                    {
                        return new RenderResponse()
                               {
                                   TaskID  = req.TaskID,
                                   Success = false,
                                   Message = string.Join(", ", exceptions)
                               }
                    };
                }
                else
                {
                    byte[] data = File.ReadAllBytes(file);

                    File.Delete(file);
                    return(new RenderResponse()
                    {
                        Success = true,
                        TaskID = req.TaskID,
                        Data = data
                    });
                };
            }
            catch (Exception ex)
            {
                return(new RenderResponse()
                {
                    TaskID = req.TaskID,
                    Success = false,
                    Message = "Exception:" + ex.Message
                });
            }
        }
        public RenderBatchResponse Packet_RenderBatch(RenderBatchRequest req)
        {
            if (!_blender.IsVersionAvailable(req.Version))
            {
                return new RenderBatchResponse()
                       {
                           TaskID  = req.TaskID,
                           Success = false,
                           Message = "Version not prepared.."
                       }
            }
            ;

            try
            {
                //Validate Settings
                string filePath = SessionData.GetFilePath(req.SessionID);

                if (filePath == null)
                {
                    return new RenderBatchResponse()
                           {
                               TaskID  = req.TaskID,
                               Success = false,
                               Message = "Blend file was not available"
                           }
                }
                ;

                for (int i = 0; i < req.Settings.Count; i++)
                {
                    Shared.RenderPacketModel settings = req.Settings[i];

                    if (settings == null)
                    {
                        settings = new Shared.RenderPacketModel();
                    }
                    if (settings.Cores <= 0)
                    {
                        settings.Cores = Environment.ProcessorCount;
                    }

                    settings.Cores = Math.Min(Environment.ProcessorCount, settings.Cores);
                }

                BlenderRenderSettings[] batch = req.Settings.Select(x => BlenderRenderSettings.FromRenderSettings(x)).ToArray();

                DateTime lastUpdate = DateTime.Now;

                List <string> exceptions = new List <string>();

                //Render
                List <string> files = _blender.RenderBatch(req.Version, filePath, batch,
                                                           (process) =>
                {
                    process.OnBlenderStatus += (status) =>
                    {
                        if (DateTime.Now.Subtract(lastUpdate).TotalMilliseconds > UPDATE_TIMING_MS)
                        {
                            lastUpdate = DateTime.Now;
                            SendPacket(new RenderInfoResponse()
                            {
                                TaskID        = req.TaskID,
                                TilesFinished = status.TilesFinish,
                                TilesTotal    = status.TilesTotal,
                                Time          = status.Time,
                                TimeRemaining = status.TimeRemaining
                            });
                        }
                    };
                    process.OnBlenderCompleteTask += (taskID) =>
                    {
                        BlenderRenderSettings settings = batch.FirstOrDefault(x => x.TaskID == taskID);
                        if (settings != null)
                        {
                            SendPacket(new RenderBatchResult()
                            {
                                Data    = File.ReadAllBytes(settings.Output),
                                Success = true,
                                TaskID  = settings.TaskID
                            });
                        }
                    };
                    process.OnBlenderException += (excp) => exceptions.Add(excp);
                });

                //Handle Result
                if (files == null || files.Count != req.Settings.Count)
                {
                    if (exceptions.Count == 0)
                    {
                        return new RenderBatchResponse()
                               {
                                   TaskID  = req.TaskID,
                                   Success = false,
                                   Message = "Missing Files?"
                               }
                    }
                    ;
                    else
                    {
                        return new RenderBatchResponse()
                               {
                                   TaskID  = req.TaskID,
                                   Success = false,
                                   Message = string.Join(", ", exceptions)
                               }
                    };
                }
                else
                {
                    //Cleanup
                    //string data = Convert.ToBase64String(File.ReadAllBytes(file));
                    foreach (string file in files)
                    {
                        File.Delete(file);
                    }
                    if (exceptions.Count > 0)
                    {
                        return(new RenderBatchResponse()
                        {
                            Success = false,
                            TaskID = req.TaskID,
                            SubTaskIDs = req.Settings.Select(X => X.TaskID).ToList(),
                            Message = string.Join(", ", exceptions)
                        });
                    }
                    else
                    {
                        return(new RenderBatchResponse()
                        {
                            Success = true,
                            TaskID = req.TaskID,
                            SubTaskIDs = req.Settings.Select(X => X.TaskID).ToList()
                        });
                    }
                };
            }
            catch (Exception ex)
            {
                return(new RenderBatchResponse()
                {
                    TaskID = req.TaskID,
                    Success = false,
                    Message = "Exception:" + ex.Message
                });
            }
        }
 /// <summary>
 /// Render a single render settings (calls batch underneath with single entry)
 /// </summary>
 public string Render(string version, string file, BlenderRenderSettings settings, Action <BlenderProcess> beforeStart = null)
 {
     return(RenderBatch(version, file, new[] { settings }, beforeStart).FirstOrDefault());
 }
        /// <summary>
        /// Checks settings and fills in missing data
        /// </summary>
        /// <param name="batch"></param>
        private void FinalizeSettings(BlenderRenderSettings[] batch)
        {
            //Validate Settings
            for (int i = 0; i < batch.Length; i++)
            {
                BlenderRenderSettings settings = batch[i];

                //Finalize Settings
                if (settings == null)
                {
                    settings = new BlenderRenderSettings();
                    batch[i] = settings;
                }

                if (settings.Cores <= 0)
                {
                    settings.Cores = Environment.ProcessorCount;
                }

                //Check for valid Tile sizes, otherwise, replace with proper one for given device
                switch (settings.ComputeUnit)
                {
                //CPU tile size is optimally 8 for full scenes, but 16 better deals with quick tiles
                case RenderType.CPU:
                    if (settings.TileWidth <= 0)
                    {
                        settings.TileWidth = 16;
                    }
                    if (settings.TileHeight <= 0)
                    {
                        settings.TileHeight = 16;
                    }
                    break;

                //CPU/GPU tile size is optimally 64, untill gpu takeover is possible
                case RenderType.CUDA:
                case RenderType.OPENCL:
                    if (settings.TileWidth <= 0)
                    {
                        settings.TileWidth = 64;
                    }
                    if (settings.TileHeight <= 0)
                    {
                        settings.TileHeight = 64;
                    }
                    break;

                //GPU tile size is optimally 256
                case RenderType.CUDA_GPUONLY:
                case RenderType.OPENCL_GPUONLY:
                    if (settings.TileWidth <= 0)
                    {
                        settings.TileWidth = 256;
                    }
                    if (settings.TileHeight <= 0)
                    {
                        settings.TileHeight = 256;
                    }
                    break;
                }


                if (settings.TaskID == null)
                {
                    settings.TaskID = Guid.NewGuid().ToString();
                }

                string outputPath = settings.Output;


                if (string.IsNullOrEmpty(outputPath))
                {
                    string outputName = settings.TaskID;
                    outputPath = Path.Combine(RenderData, outputName);
                }

                if (settings.Output == null)
                {
                    settings.Output = outputPath;
                }

                settings.Output = Path.GetFullPath(outputPath);
                if (string.IsNullOrEmpty(Path.GetExtension(settings.Output)))
                {
                    settings.Output += ".png";
                }
            }
        }