Rendering is now working

This commit is contained in:
Cameron Gutman 2016-02-13 14:49:27 -05:00
parent e67a841a3e
commit b7032a8957
4 changed files with 94 additions and 84 deletions

View File

@ -7,7 +7,7 @@
<script type="text/javascript" src="index.js"></script> <script type="text/javascript" src="index.js"></script>
<script type="text/javascript" src="common.js"></script> <script type="text/javascript" src="common.js"></script>
</head> </head>
<body data-name="moonlight-chrome" data-width="500" data-height="500" style="height: 0px;" data-tools="pnacl" data-configs="Debug Release" data-path="{tc}/{config}"> <body data-name="moonlight-chrome" data-width="1280" data-height="720" data-tools="pnacl" data-configs="Debug Release" data-path="{tc}/{config}">
<h1>Moonlight</h1> <h1>Moonlight</h1>
<h2>Status: <code id="statusField">NO-STATUS</code></h2> <h2>Status: <code id="statusField">NO-STATUS</code></h2>

View File

@ -73,8 +73,8 @@ void MoonlightInstance::HandleMessage(const pp::Var& var_message) {
LiInitializeStreamConfiguration(&s_StreamConfig); LiInitializeStreamConfiguration(&s_StreamConfig);
s_StreamConfig.width = 1280; s_StreamConfig.width = 1280;
s_StreamConfig.height = 720; s_StreamConfig.height = 720;
s_StreamConfig.fps = 30; s_StreamConfig.fps = 60;
s_StreamConfig.bitrate = 10; // megabits per second s_StreamConfig.bitrate = 15000; // kilobits per second
s_StreamConfig.packetSize = 1024; s_StreamConfig.packetSize = 1024;
s_StreamConfig.streamingRemotely = 0; s_StreamConfig.streamingRemotely = 0;
s_StreamConfig.audioConfiguration = AUDIO_CONFIGURATION_STEREO; s_StreamConfig.audioConfiguration = AUDIO_CONFIGURATION_STEREO;

View File

@ -12,6 +12,9 @@
#include "ppapi/utility/completion_callback_factory.h" #include "ppapi/utility/completion_callback_factory.h"
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include "nacl_io/nacl_io.h" #include "nacl_io/nacl_io.h"
#include <Limelight.h> #include <Limelight.h>
@ -24,19 +27,17 @@ struct Shader {
GLint texcoord_scale_location; GLint texcoord_scale_location;
}; };
class MoonlightInstance : public pp::Instance, public pp::MouseLock, public pp::Graphics3DClient { class MoonlightInstance : public pp::Instance, public pp::MouseLock {
public: public:
MoonlightInstance(PP_Instance instance) : explicit MoonlightInstance(PP_Instance instance) :
pp::Instance(instance), pp::Instance(instance),
pp::MouseLock(this), pp::MouseLock(this),
pp::Graphics3DClient(this),
m_CallbackFactory(this), m_CallbackFactory(this),
m_MouseLocked(false) { m_MouseLocked(false) {
// This function MUST be used otherwise sockets don't work (nacl_io_init() doesn't work!) // This function MUST be used otherwise sockets don't work (nacl_io_init() doesn't work!)
nacl_io_init_ppapi(pp_instance(), pp::Module::Get()->get_browser_interface()); nacl_io_init_ppapi(pp_instance(), pp::Module::Get()->get_browser_interface());
m_GamepadApi = static_cast<const PPB_Gamepad*>(pp::Module::Get()->GetBrowserInterface(PPB_GAMEPAD_INTERFACE)); m_GamepadApi = static_cast<const PPB_Gamepad*>(pp::Module::Get()->GetBrowserInterface(PPB_GAMEPAD_INTERFACE));
m_GlesApi = static_cast<const PPB_OpenGLES2*>(pp::Module::Get()->GetBrowserInterface(PPB_OPENGLES2_INTERFACE));
} }
virtual ~MoonlightInstance(); virtual ~MoonlightInstance();
@ -67,12 +68,11 @@ class MoonlightInstance : public pp::Instance, public pp::MouseLock, public pp::
static void ClDisplayMessage(char* message); static void ClDisplayMessage(char* message);
static void ClDisplayTransientMessage(char* message); static void ClDisplayTransientMessage(char* message);
virtual void Graphics3DContextLost() {}
static Shader CreateProgram(const char* vertexShader, const char* fragmentShader); static Shader CreateProgram(const char* vertexShader, const char* fragmentShader);
static void CreateShader(GLuint program, GLenum type, const char* source, int size); static void CreateShader(GLuint program, GLenum type, const char* source, int size);
static void PaintPicture(PP_VideoPicture picture); static void PaintPicture(PP_VideoPicture picture);
void DispatchRendering(int32_t unused);
void DispatchGetPicture(uint32_t unused); void DispatchGetPicture(uint32_t unused);
void PictureReady(int32_t result, PP_VideoPicture picture); void PictureReady(int32_t result, PP_VideoPicture picture);
@ -84,13 +84,13 @@ class MoonlightInstance : public pp::Instance, public pp::MouseLock, public pp::
static CONNECTION_LISTENER_CALLBACKS s_ClCallbacks; static CONNECTION_LISTENER_CALLBACKS s_ClCallbacks;
static DECODER_RENDERER_CALLBACKS s_DrCallbacks; static DECODER_RENDERER_CALLBACKS s_DrCallbacks;
pp::Graphics3D* m_Graphics3D; pp::Graphics3D m_Graphics3D;
pp::VideoDecoder* m_VideoDecoder; pp::VideoDecoder* m_VideoDecoder;
pp::Size m_ViewSize; pp::Size m_ViewSize;
const PPB_OpenGLES2* m_GlesApi;
Shader m_Texture2DShader; Shader m_Texture2DShader;
Shader m_RectangleArbShader; Shader m_RectangleArbShader;
Shader m_ExternalOesShader; Shader m_ExternalOesShader;
PP_VideoPicture m_LastPicture;
double m_LastPadTimestamps[4]; double m_LastPadTimestamps[4];
const PPB_Gamepad* m_GamepadApi; const PPB_Gamepad* m_GamepadApi;

View File

@ -3,6 +3,8 @@
#include <GLES2/gl2.h> #include <GLES2/gl2.h>
#include <GLES2/gl2ext.h> #include <GLES2/gl2ext.h>
#include "ppapi/lib/gl/gles2/gl2ext_ppapi.h"
#define INITIAL_DECODE_BUFFER_LEN 128 * 1024 #define INITIAL_DECODE_BUFFER_LEN 128 * 1024
static unsigned char* s_DecodeBuffer; static unsigned char* s_DecodeBuffer;
@ -64,28 +66,30 @@ void MoonlightInstance::DidChangeView(const pp::Rect& position,
m_ViewSize = position.size(); m_ViewSize = position.size();
printf("View size: %dx%d\n", m_ViewSize.width(), m_ViewSize.height()); printf("View size: %dx%d\n", m_ViewSize.width(), m_ViewSize.height());
if (!glInitializePPAPI(pp::Module::Get()->get_browser_interface())) {
return;
}
int32_t contextAttributes[] = { int32_t contextAttributes[] = {
PP_GRAPHICS3DATTRIB_ALPHA_SIZE, 8, PP_GRAPHICS3DATTRIB_ALPHA_SIZE, 8,
PP_GRAPHICS3DATTRIB_BLUE_SIZE, 8, PP_GRAPHICS3DATTRIB_DEPTH_SIZE, 24,
PP_GRAPHICS3DATTRIB_GREEN_SIZE, 8, PP_GRAPHICS3DATTRIB_WIDTH, position.size().width(),
PP_GRAPHICS3DATTRIB_RED_SIZE, 8, PP_GRAPHICS3DATTRIB_HEIGHT, position.size().height(),
PP_GRAPHICS3DATTRIB_DEPTH_SIZE, 0, PP_GRAPHICS3DATTRIB_NONE
PP_GRAPHICS3DATTRIB_STENCIL_SIZE, 0,
PP_GRAPHICS3DATTRIB_SAMPLES, 0,
PP_GRAPHICS3DATTRIB_SAMPLE_BUFFERS, 0,
PP_GRAPHICS3DATTRIB_WIDTH, g_Instance->m_ViewSize.width(),
PP_GRAPHICS3DATTRIB_HEIGHT, g_Instance->m_ViewSize.height(),
PP_GRAPHICS3DATTRIB_NONE,
}; };
g_Instance->m_Graphics3D = new pp::Graphics3D(g_Instance, contextAttributes); g_Instance->m_Graphics3D = pp::Graphics3D(this, contextAttributes);
assert(!g_Instance->m_Graphics3D->is_null());
assert(BindGraphics(*m_Graphics3D)); if (!BindGraphics(m_Graphics3D)) {
fprintf(stderr, "Unable to bind 3d context!\n");
m_Graphics3D = pp::Graphics3D();
glSetCurrentContextPPAPI(0);
return;
}
PP_Resource graphics3D = g_Instance->m_Graphics3D->pp_resource(); glSetCurrentContextPPAPI(m_Graphics3D.pp_resource());
g_Instance->m_GlesApi->ClearColor(graphics3D, 1, 0, 0, 1); glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
g_Instance->m_GlesApi->Clear(graphics3D, GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
assertNoGLError(); assertNoGLError();
@ -95,15 +99,16 @@ void MoonlightInstance::DidChangeView(const pp::Rect& position,
}; };
GLuint buffer; GLuint buffer;
g_Instance->m_GlesApi->GenBuffers(graphics3D, 1, &buffer); glGenBuffers(1, &buffer);
g_Instance->m_GlesApi->BindBuffer(graphics3D, GL_ARRAY_BUFFER, buffer); glBindBuffer(GL_ARRAY_BUFFER, buffer);
g_Instance->m_GlesApi->BufferData(graphics3D, glBufferData(GL_ARRAY_BUFFER,
GL_ARRAY_BUFFER,
sizeof(k_Vertices), sizeof(k_Vertices),
k_Vertices, k_Vertices,
GL_STATIC_DRAW); GL_STATIC_DRAW);
assertNoGLError(); assertNoGLError();
g_Instance->m_Graphics3D.SwapBuffers(g_Instance->m_CallbackFactory.NewCallback(&MoonlightInstance::DispatchRendering));
} }
void MoonlightInstance::VidDecSetup(int width, int height, int redrawRate, void* context, int drFlags) { void MoonlightInstance::VidDecSetup(int width, int height, int redrawRate, void* context, int drFlags) {
@ -112,7 +117,7 @@ void MoonlightInstance::VidDecSetup(int width, int height, int redrawRate, void*
s_DecodeBufferLength = INITIAL_DECODE_BUFFER_LEN; s_DecodeBufferLength = INITIAL_DECODE_BUFFER_LEN;
s_DecodeBuffer = (unsigned char *)malloc(s_DecodeBufferLength); s_DecodeBuffer = (unsigned char *)malloc(s_DecodeBufferLength);
g_Instance->m_VideoDecoder->Initialize(*g_Instance->m_Graphics3D, g_Instance->m_VideoDecoder->Initialize(g_Instance->m_Graphics3D,
PP_VIDEOPROFILE_H264HIGH, PP_VIDEOPROFILE_H264HIGH,
PP_HARDWAREACCELERATION_ONLY, PP_HARDWAREACCELERATION_ONLY,
0, 0,
@ -123,6 +128,12 @@ void MoonlightInstance::VidDecSetup(int width, int height, int redrawRate, void*
} }
void MoonlightInstance::DispatchGetPicture(uint32_t unused) { void MoonlightInstance::DispatchGetPicture(uint32_t unused) {
/*glClearColor(0.5, 0.5, 0.5, 1);
glClear(GL_COLOR_BUFFER_BIT);
m_Graphics3D.SwapBuffers(
m_CallbackFactory.NewCallback(&MoonlightInstance::DispatchGetPicture));*/
// Queue the initial GetPicture callback on the main thread // Queue the initial GetPicture callback on the main thread
g_Instance->m_VideoDecoder->GetPicture( g_Instance->m_VideoDecoder->GetPicture(
g_Instance->m_CallbackFactory.NewCallbackWithOutput(&MoonlightInstance::PictureReady)); g_Instance->m_CallbackFactory.NewCallbackWithOutput(&MoonlightInstance::PictureReady));
@ -132,18 +143,15 @@ void MoonlightInstance::VidDecCleanup(void) {
free(s_DecodeBuffer); free(s_DecodeBuffer);
delete g_Instance->m_VideoDecoder; delete g_Instance->m_VideoDecoder;
PP_Resource graphics3D = g_Instance->m_Graphics3D->pp_resource();
if (g_Instance->m_Texture2DShader.program) { if (g_Instance->m_Texture2DShader.program) {
g_Instance->m_GlesApi->DeleteProgram(graphics3D, g_Instance->m_Texture2DShader.program); glDeleteProgram(g_Instance->m_Texture2DShader.program);
} }
if (g_Instance->m_RectangleArbShader.program) { if (g_Instance->m_RectangleArbShader.program) {
g_Instance->m_GlesApi->DeleteProgram(graphics3D, g_Instance->m_RectangleArbShader.program); glDeleteProgram(g_Instance->m_RectangleArbShader.program);
} }
if (g_Instance->m_ExternalOesShader.program) { if (g_Instance->m_ExternalOesShader.program) {
g_Instance->m_GlesApi->DeleteProgram(graphics3D, g_Instance->m_ExternalOesShader.program); glDeleteProgram(g_Instance->m_ExternalOesShader.program);
} }
delete g_Instance->m_Graphics3D;
} }
int MoonlightInstance::VidDecSubmitDecodeUnit(PDECODE_UNIT decodeUnit) { int MoonlightInstance::VidDecSubmitDecodeUnit(PDECODE_UNIT decodeUnit) {
@ -174,79 +182,79 @@ int MoonlightInstance::VidDecSubmitDecodeUnit(PDECODE_UNIT decodeUnit) {
void MoonlightInstance::CreateShader(GLuint program, GLenum type, void MoonlightInstance::CreateShader(GLuint program, GLenum type,
const char* source, int size) { const char* source, int size) {
PP_Resource graphics3D = g_Instance->m_Graphics3D->pp_resource(); GLuint shader = glCreateShader(type);
GLuint shader = g_Instance->m_GlesApi->CreateShader(graphics3D, type); glShaderSource(shader, 1, &source, &size);
g_Instance->m_GlesApi->ShaderSource(graphics3D, shader, 1, &source, &size); glCompileShader(shader);
g_Instance->m_GlesApi->CompileShader(graphics3D, shader); glAttachShader(program, shader);
g_Instance->m_GlesApi->AttachShader(graphics3D, program, shader); glDeleteShader(shader);
g_Instance->m_GlesApi->DeleteShader(graphics3D, shader);
} }
Shader MoonlightInstance::CreateProgram(const char* vertexShader, const char* fragmentShader) { Shader MoonlightInstance::CreateProgram(const char* vertexShader, const char* fragmentShader) {
Shader shader; Shader shader;
PP_Resource graphics3D = g_Instance->m_Graphics3D->pp_resource();
shader.program = g_Instance->m_GlesApi->CreateProgram(graphics3D); shader.program = glCreateProgram();
CreateShader(shader.program, GL_VERTEX_SHADER, vertexShader, strlen(vertexShader)); CreateShader(shader.program, GL_VERTEX_SHADER, vertexShader, strlen(vertexShader));
CreateShader(shader.program, GL_FRAGMENT_SHADER, fragmentShader, strlen(fragmentShader)); CreateShader(shader.program, GL_FRAGMENT_SHADER, fragmentShader, strlen(fragmentShader));
g_Instance->m_GlesApi->LinkProgram(graphics3D, shader.program); glLinkProgram(shader.program);
g_Instance->m_GlesApi->UseProgram(graphics3D, shader.program); glUseProgram(shader.program);
g_Instance->m_GlesApi->Uniform1i(graphics3D, glUniform1i(glGetUniformLocation(shader.program, "s_texture"), 0);
g_Instance->m_GlesApi->GetUniformLocation(graphics3D, shader.program, "s_texture"), 0);
assertNoGLError(); assertNoGLError();
shader.texcoord_scale_location = g_Instance->m_GlesApi->GetUniformLocation(graphics3D, shader.program, "v_scale"); shader.texcoord_scale_location = glGetUniformLocation(shader.program, "v_scale");
GLint pos_location = g_Instance->m_GlesApi->GetAttribLocation(graphics3D, shader.program, "a_position"); GLint pos_location = glGetAttribLocation(shader.program, "a_position");
GLint tc_location = g_Instance->m_GlesApi->GetAttribLocation(graphics3D, shader.program, "a_texCoord"); GLint tc_location = glGetAttribLocation(shader.program, "a_texCoord");
assertNoGLError(); assertNoGLError();
g_Instance->m_GlesApi->EnableVertexAttribArray(graphics3D, pos_location); glEnableVertexAttribArray( pos_location);
g_Instance->m_GlesApi->VertexAttribPointer(graphics3D, pos_location, 2, GL_FLOAT, GL_FALSE, 0, 0); glVertexAttribPointer(pos_location, 2, GL_FLOAT, GL_FALSE, 0, 0);
g_Instance->m_GlesApi->EnableVertexAttribArray(graphics3D, tc_location); glEnableVertexAttribArray(tc_location);
g_Instance->m_GlesApi->VertexAttribPointer(graphics3D, tc_location, 2, GL_FLOAT, GL_FALSE, 0, static_cast<float*>(0) + 8); glVertexAttribPointer(tc_location, 2, GL_FLOAT, GL_FALSE, 0, static_cast<float*>(0) + 8);
g_Instance->m_GlesApi->UseProgram(graphics3D, 0); glUseProgram(0);
assertNoGLError(); assertNoGLError();
return shader; return shader;
} }
void MoonlightInstance::PaintPicture(PP_VideoPicture picture) { void MoonlightInstance::PaintPicture(PP_VideoPicture picture) {
PP_Resource graphics3D = g_Instance->m_Graphics3D->pp_resource();
if (picture.texture_target == GL_TEXTURE_2D) { if (picture.texture_target == GL_TEXTURE_2D) {
if (!g_Instance->m_Texture2DShader.program) { if (!g_Instance->m_Texture2DShader.program) {
g_Instance->m_Texture2DShader = CreateProgram(k_VertexShader, k_FragmentShader2D); g_Instance->m_Texture2DShader = CreateProgram(k_VertexShader, k_FragmentShader2D);
} }
g_Instance->m_GlesApi->UseProgram(graphics3D, g_Instance->m_Texture2DShader.program); glUseProgram(g_Instance->m_Texture2DShader.program);
g_Instance->m_GlesApi->Uniform2f(graphics3D, g_Instance->m_Texture2DShader.texcoord_scale_location, glUniform2f(g_Instance->m_Texture2DShader.texcoord_scale_location, 1.0, 1.0);
1.0, 1.0);
} }
else if (picture.texture_target == GL_TEXTURE_RECTANGLE_ARB) { else if (picture.texture_target == GL_TEXTURE_RECTANGLE_ARB) {
if (!g_Instance->m_RectangleArbShader.program) { if (!g_Instance->m_RectangleArbShader.program) {
g_Instance->m_RectangleArbShader = CreateProgram(k_VertexShader, k_FragmentShaderRectangle); g_Instance->m_RectangleArbShader = CreateProgram(k_VertexShader, k_FragmentShaderRectangle);
} }
g_Instance->m_GlesApi->UseProgram(graphics3D, g_Instance->m_RectangleArbShader.program); glUseProgram(g_Instance->m_RectangleArbShader.program);
g_Instance->m_GlesApi->Uniform2f(graphics3D, g_Instance->m_RectangleArbShader.texcoord_scale_location, glUniform2f(g_Instance->m_RectangleArbShader.texcoord_scale_location,
picture.texture_size.width, picture.texture_size.height); picture.texture_size.width, picture.texture_size.height);
} }
else { else if (picture.texture_target == GL_TEXTURE_EXTERNAL_OES){
if (!g_Instance->m_ExternalOesShader.program) { if (!g_Instance->m_ExternalOesShader.program) {
g_Instance->m_ExternalOesShader = CreateProgram(k_VertexShader, k_FragmentShaderExternal); g_Instance->m_ExternalOesShader = CreateProgram(k_VertexShader, k_FragmentShaderExternal);
} }
g_Instance->m_GlesApi->UseProgram(graphics3D, g_Instance->m_ExternalOesShader.program); glUseProgram(g_Instance->m_ExternalOesShader.program);
g_Instance->m_GlesApi->Uniform2f(graphics3D, g_Instance->m_ExternalOesShader.texcoord_scale_location, glUniform2f(g_Instance->m_ExternalOesShader.texcoord_scale_location, 1.0, 1.0);
1.0, 1.0);
} }
g_Instance->m_GlesApi->Viewport(graphics3D, 0, 0, g_Instance->m_ViewSize.width(), g_Instance->m_ViewSize.height()); if (picture.texture_target != 0) {
g_Instance->m_GlesApi->ActiveTexture(graphics3D, GL_TEXTURE0); glViewport(0, 0, g_Instance->m_ViewSize.width(), g_Instance->m_ViewSize.height());
g_Instance->m_GlesApi->BindTexture(graphics3D, picture.texture_target, picture.texture_id); glActiveTexture(GL_TEXTURE0);
g_Instance->m_GlesApi->DrawArrays(graphics3D, GL_TRIANGLE_STRIP, 0, 4); glBindTexture(picture.texture_target, picture.texture_id);
g_Instance->m_GlesApi->UseProgram(graphics3D, 0); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glUseProgram(0);
}
g_Instance->m_Graphics3D->SwapBuffers(pp::BlockUntilComplete()); g_Instance->m_Graphics3D.SwapBuffers(g_Instance->m_CallbackFactory.NewCallback(&MoonlightInstance::DispatchRendering));
}
void MoonlightInstance::DispatchRendering(int32_t unused) {
// Paint the image on screen
PaintPicture(m_LastPicture);
} }
void MoonlightInstance::PictureReady(int32_t result, PP_VideoPicture picture) { void MoonlightInstance::PictureReady(int32_t result, PP_VideoPicture picture) {
@ -254,10 +262,12 @@ void MoonlightInstance::PictureReady(int32_t result, PP_VideoPicture picture) {
return; return;
} }
// Paint the image on screen // Replace the last picture with this one and free the old one
PaintPicture(picture); PP_VideoPicture oldPic = m_LastPicture;
m_LastPicture = picture;
g_Instance->m_VideoDecoder->RecyclePicture(picture); if (oldPic.texture_target) {
g_Instance->m_VideoDecoder->RecyclePicture(oldPic);
}
// Queue another callback // Queue another callback
g_Instance->m_VideoDecoder->GetPicture( g_Instance->m_VideoDecoder->GetPicture(