File size: 2,911 Bytes
096584a
 
 
 
 
 
 
caa2240
 
c4b02b2
096584a
 
 
 
 
 
 
 
 
 
 
 
15081b3
 
 
 
 
 
 
 
 
 
 
 
caa2240
 
 
 
 
 
 
 
 
 
 
 
096584a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4b02b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
096584a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

import { RenderedScene, RenderRequest } from "../types.mts"

import { renderImage } from "./renderImage.mts"
import { renderVideo } from "./renderVideo.mts"
import { renderImageSegmentation } from "./renderImageSegmentation.mts"
import { renderVideoSegmentation } from "./renderVideoSegmentation.mts"
import { upscaleImage } from "../utils/upscaleImage.mts"
import { renderImageUpscaling } from "./renderImageUpscaling.mts"
import { saveRenderedSceneToCache } from "../utils/saveRenderedSceneToCache.mts"

export async function renderPipeline(request: RenderRequest, response: RenderedScene) {
  const isVideo = request?.nbFrames > 1

  const renderContent = isVideo ? renderVideo : renderImage
  const renderSegmentation  = isVideo ? renderVideoSegmentation : renderImageSegmentation 

  if (isVideo) {
    console.log(`rendering a video..`)
  } else {
    console.log(`rendering an image..`)
  }

  try {
    await renderContent(request, response)
  } catch (err) {
    console.log(`renderContent() failed, trying a 2nd time..`)
    try {
      await renderContent(request, response)
    } catch (err2) {
      console.log(`renderContent() failed, trying a 3th time..`)
      await renderContent(request, response)
    }
  }

  // we upscale images with esrgan
  // and for videos, well.. let's just skip this part,
  // but later we could use Zeroscope V2 XL maybe?
  const optionalUpscalingStep = isVideo
    ? Promise.resolve()
    : renderImageUpscaling(request, response)

  await Promise.all([
    renderSegmentation(request, response),
    optionalUpscalingStep
  ])

  /*
  this is the optimized pipeline
  However, right now it doesn't work because for some reason,
  asking to generate the same seed + prompt on different nb of steps
  doesn't generate the same image!

  // first we need to wait for the low quality pre-render
  await renderContent({
    ...request,

    // we are a bit more aggressive with the quality of the video preview
    nbSteps: isVideo ? 8 : 16
  }, response)

  // then we can run both the segmentation and the high-res render at the same time
  await Promise.all([
    renderSegmentation(request, response),
    renderContent(request, response)
  ])
  */

  response.status = "completed"
  response.error = ""

  if (!request.cache || request.cache === "ignore") {
    console.log("client asked to not use the cache in the rendering pipeline")
    return
  }

  console.log("client asked this for cache: "+request.cache)

  try {
    // since the request is now completed we cache it
    await saveRenderedSceneToCache(request, response)
    console.log("successfully saved to cache")

    // we don't really need to remove it from the in-memory cache 
    // (the cache queue in src/production/renderScene.mts)
    // since this cache queue has already an automatic pruning
  } catch (err) {
    console.error(`failed to save to cache, but no big deal: ${err}`)
  }
}