#include "glheader.h"
#include "imports.h"
#include "context.h"
#include "fbobject.h"
#include "mtypes.h"
#include "depthstencil.h"
#include "renderbuffer.h"
static void *
nop_get_pointer(GLcontext *ctx, struct gl_renderbuffer *rb, GLint x, GLint y)
{
(void) ctx;
(void) rb;
(void) x;
(void) y;
return NULL;
}
static void
delete_wrapper(struct gl_renderbuffer *rb)
{
struct gl_renderbuffer *dsrb = rb->Wrapped;
ASSERT(dsrb);
ASSERT(rb->_ActualFormat == GL_DEPTH_COMPONENT24 ||
rb->_ActualFormat == GL_STENCIL_INDEX8_EXT);
dsrb->RefCount--;
if (dsrb->RefCount <= 0) {
dsrb->Delete(dsrb);
}
_mesa_free(rb);
}
static GLboolean
alloc_wrapper_storage(GLcontext *ctx, struct gl_renderbuffer *rb,
GLenum internalFormat, GLuint width, GLuint height)
{
struct gl_renderbuffer *dsrb = rb->Wrapped;
GLboolean retVal;
(void) internalFormat;
ASSERT(dsrb->_ActualFormat == GL_DEPTH24_STENCIL8_EXT);
retVal = dsrb->AllocStorage(ctx, dsrb, dsrb->InternalFormat, width, height);
if (retVal) {
rb->Width = width;
rb->Height = height;
}
return retVal;
}
static void
get_row_z24(GLcontext *ctx, struct gl_renderbuffer *z24rb, GLuint count,
GLint x, GLint y, void *values)
{
struct gl_renderbuffer *dsrb = z24rb->Wrapped;
GLuint temp[MAX_WIDTH], i;
GLuint *dst = (GLuint *) values;
const GLuint *src = (const GLuint *) dsrb->GetPointer(ctx, dsrb, x, y);
ASSERT(z24rb->DataType == GL_UNSIGNED_INT);
ASSERT(dsrb->_ActualFormat == GL_DEPTH24_STENCIL8_EXT);
ASSERT(dsrb->DataType == GL_UNSIGNED_INT_24_8_EXT);
if (!src) {
dsrb->GetRow(ctx, dsrb, count, x, y, temp);
src = temp;
}
for (i = 0; i < count; i++) {
dst[i] = src[i] >> 8;
}
}
static void
get_values_z24(GLcontext *ctx, struct gl_renderbuffer *z24rb, GLuint count,
const GLint x[], const GLint y[], void *values)
{
struct gl_renderbuffer *dsrb = z24rb->Wrapped;
GLuint temp[MAX_WIDTH], i;
GLuint *dst = (GLuint *) values;
ASSERT(z24rb->DataType == GL_UNSIGNED_INT);
ASSERT(dsrb->_ActualFormat == GL_DEPTH24_STENCIL8_EXT);
ASSERT(dsrb->DataType == GL_UNSIGNED_INT_24_8_EXT);
ASSERT(count <= MAX_WIDTH);
dsrb->GetValues(ctx, dsrb, count, x, y, temp);
for (i = 0; i < count; i++) {
dst[i] = temp[i] >> 8;
}
}
static void
put_row_z24(GLcontext *ctx, struct gl_renderbuffer *z24rb, GLuint count,
GLint x, GLint y, const void *values, const GLubyte *mask)
{
struct gl_renderbuffer *dsrb = z24rb->Wrapped;
const GLuint *src = (const GLuint *) values;
GLuint *dst = (GLuint *) dsrb->GetPointer(ctx, dsrb, x, y);
ASSERT(z24rb->DataType == GL_UNSIGNED_INT);
ASSERT(dsrb->_ActualFormat == GL_DEPTH24_STENCIL8_EXT);
ASSERT(dsrb->DataType == GL_UNSIGNED_INT_24_8_EXT);
if (dst) {
GLuint i;
for (i = 0; i < count; i++) {
if (!mask || mask[i]) {
dst[i] = (src[i] << 8) | (dst[i] & 0xff);
}
}
}
else {
GLuint temp[MAX_WIDTH], i;
dsrb->GetRow(ctx, dsrb, count, x, y, temp);
for (i = 0; i < count; i++) {
if (!mask || mask[i]) {
temp[i] = (src[i] << 8) | (temp[i] & 0xff);
}
}
dsrb->PutRow(ctx, dsrb, count, x, y, temp, mask);
}
}
static void
put_mono_row_z24(GLcontext *ctx, struct gl_renderbuffer *z24rb, GLuint count,
GLint x, GLint y, const void *value, const GLubyte *mask)
{
struct gl_renderbuffer *dsrb = z24rb->Wrapped;
const GLuint shiftedVal = *((GLuint *) value) << 8;
GLuint *dst = (GLuint *) dsrb->GetPointer(ctx, dsrb, x, y);
ASSERT(z24rb->DataType == GL_UNSIGNED_INT);
ASSERT(dsrb->_ActualFormat == GL_DEPTH24_STENCIL8_EXT);
ASSERT(dsrb->DataType == GL_UNSIGNED_INT_24_8_EXT);
if (dst) {
GLuint i;
for (i = 0; i < count; i++) {
if (!mask || mask[i]) {
dst[i] = shiftedVal | (dst[i] & 0xff);
}
}
}
else {
GLuint temp[MAX_WIDTH], i;
dsrb->GetRow(ctx, dsrb, count, x, y, temp);
for (i = 0; i < count; i++) {
if (!mask || mask[i]) {
temp[i] = shiftedVal | (temp[i] & 0xff);
}
}
dsrb->PutRow(ctx, dsrb, count, x, y, temp, mask);
}
}
static void
put_values_z24(GLcontext *ctx, struct gl_renderbuffer *z24rb, GLuint count,
const GLint x[], const GLint y[],
const void *values, const GLubyte *mask)
{
struct gl_renderbuffer *dsrb = z24rb->Wrapped;
const GLuint *src = (const GLuint *) values;
ASSERT(z24rb->DataType == GL_UNSIGNED_INT);
ASSERT(dsrb->_ActualFormat == GL_DEPTH24_STENCIL8_EXT);
ASSERT(dsrb->DataType == GL_UNSIGNED_INT_24_8_EXT);
if (dsrb->GetPointer(ctx, dsrb, 0, 0)) {
GLuint i;
for (i = 0; i < count; i++) {
if (!mask || mask[i]) {
GLuint *dst = (GLuint *) dsrb->GetPointer(ctx, dsrb, x[i], y[i]);
*dst = (src[i] << 8) | (*dst & 0xff);
}
}
}
else {
GLuint temp[MAX_WIDTH], i;
dsrb->GetValues(ctx, dsrb, count, x, y, temp);
for (i = 0; i < count; i++) {
if (!mask || mask[i]) {
temp[i] = (src[i] << 8) | (temp[i] & 0xff);
}
}
dsrb->PutValues(ctx, dsrb, count, x, y, temp, mask);
}
}
static void
put_mono_values_z24(GLcontext *ctx, struct gl_renderbuffer *z24rb,
GLuint count, const GLint x[], const GLint y[],
const void *value, const GLubyte *mask)
{
struct gl_renderbuffer *dsrb = z24rb->Wrapped;
GLuint temp[MAX_WIDTH], i;
const GLuint shiftedVal = *((GLuint *) value) << 8;
dsrb->GetValues(ctx, dsrb, count, x, y, temp);
for (i = 0; i < count; i++) {
if (!mask || mask[i]) {
temp[i] = shiftedVal | (temp[i] & 0xff);
}
}
dsrb->PutValues(ctx, dsrb, count, x, y, temp, mask);
}
struct gl_renderbuffer *
_mesa_new_z24_renderbuffer_wrapper(GLcontext *ctx,
struct gl_renderbuffer *dsrb)
{
struct gl_renderbuffer *z24rb;
ASSERT(dsrb->_ActualFormat == GL_DEPTH24_STENCIL8_EXT);
ASSERT(dsrb->DataType == GL_UNSIGNED_INT_24_8_EXT);
z24rb = _mesa_new_renderbuffer(ctx, 0);
if (!z24rb)
return NULL;
z24rb->Wrapped = dsrb;
z24rb->Name = dsrb->Name;
z24rb->RefCount = 1;
z24rb->Width = dsrb->Width;
z24rb->Height = dsrb->Height;
z24rb->InternalFormat = GL_DEPTH_COMPONENT24_ARB;
z24rb->_ActualFormat = GL_DEPTH_COMPONENT24_ARB;
z24rb->_BaseFormat = GL_DEPTH_COMPONENT;
z24rb->DataType = GL_UNSIGNED_INT;
z24rb->DepthBits = 24;
z24rb->Data = NULL;
z24rb->Delete = delete_wrapper;
z24rb->AllocStorage = alloc_wrapper_storage;
z24rb->GetPointer = nop_get_pointer;
z24rb->GetRow = get_row_z24;
z24rb->GetValues = get_values_z24;
z24rb->PutRow = put_row_z24;
z24rb->PutRowRGB = NULL;
z24rb->PutMonoRow = put_mono_row_z24;
z24rb->PutValues = put_values_z24;
z24rb->PutMonoValues = put_mono_values_z24;
return z24rb;
}
static void
get_row_s8(GLcontext *ctx, struct gl_renderbuffer *s8rb, GLuint count,
GLint x, GLint y, void *values)
{
struct gl_renderbuffer *dsrb = s8rb->Wrapped;
GLuint temp[MAX_WIDTH], i;
GLubyte *dst = (GLubyte *) values;
const GLuint *src = (const GLuint *) dsrb->GetPointer(ctx, dsrb, x, y);
ASSERT(s8rb->DataType == GL_UNSIGNED_BYTE);
ASSERT(dsrb->_ActualFormat == GL_DEPTH24_STENCIL8_EXT);
ASSERT(dsrb->DataType == GL_UNSIGNED_INT_24_8_EXT);
if (!src) {
dsrb->GetRow(ctx, dsrb, count, x, y, temp);
src = temp;
}
for (i = 0; i < count; i++) {
dst[i] = src[i] & 0xff;
}
}
static void
get_values_s8(GLcontext *ctx, struct gl_renderbuffer *s8rb, GLuint count,
const GLint x[], const GLint y[], void *values)
{
struct gl_renderbuffer *dsrb = s8rb->Wrapped;
GLuint temp[MAX_WIDTH], i;
GLubyte *dst = (GLubyte *) values;
ASSERT(s8rb->DataType == GL_UNSIGNED_BYTE);
ASSERT(dsrb->_ActualFormat == GL_DEPTH24_STENCIL8_EXT);
ASSERT(dsrb->DataType == GL_UNSIGNED_INT_24_8_EXT);
ASSERT(count <= MAX_WIDTH);
dsrb->GetValues(ctx, dsrb, count, x, y, temp);
for (i = 0; i < count; i++) {
dst[i] = temp[i] & 0xff;
}
}
static void
put_row_s8(GLcontext *ctx, struct gl_renderbuffer *s8rb, GLuint count,
GLint x, GLint y, const void *values, const GLubyte *mask)
{
struct gl_renderbuffer *dsrb = s8rb->Wrapped;
const GLubyte *src = (const GLubyte *) values;
GLuint *dst = (GLuint *) dsrb->GetPointer(ctx, dsrb, x, y);
ASSERT(s8rb->DataType == GL_UNSIGNED_BYTE);
ASSERT(dsrb->_ActualFormat == GL_DEPTH24_STENCIL8_EXT);
ASSERT(dsrb->DataType == GL_UNSIGNED_INT_24_8_EXT);
if (dst) {
GLuint i;
for (i = 0; i < count; i++) {
if (!mask || mask[i]) {
dst[i] = (dst[i] & 0xffffff00) | src[i];
}
}
}
else {
GLuint temp[MAX_WIDTH], i;
dsrb->GetRow(ctx, dsrb, count, x, y, temp);
for (i = 0; i < count; i++) {
if (!mask || mask[i]) {
temp[i] = (temp[i] & 0xffffff00) | src[i];
}
}
dsrb->PutRow(ctx, dsrb, count, x, y, temp, mask);
}
}
static void
put_mono_row_s8(GLcontext *ctx, struct gl_renderbuffer *s8rb, GLuint count,
GLint x, GLint y, const void *value, const GLubyte *mask)
{
struct gl_renderbuffer *dsrb = s8rb->Wrapped;
const GLubyte val = *((GLubyte *) value);
GLuint *dst = (GLuint *) dsrb->GetPointer(ctx, dsrb, x, y);
ASSERT(s8rb->DataType == GL_UNSIGNED_BYTE);
ASSERT(dsrb->_ActualFormat == GL_DEPTH24_STENCIL8_EXT);
ASSERT(dsrb->DataType == GL_UNSIGNED_INT_24_8_EXT);
if (dst) {
GLuint i;
for (i = 0; i < count; i++) {
if (!mask || mask[i]) {
dst[i] = (dst[i] & 0xffffff00) | val;
}
}
}
else {
GLuint temp[MAX_WIDTH], i;
dsrb->GetRow(ctx, dsrb, count, x, y, temp);
for (i = 0; i < count; i++) {
if (!mask || mask[i]) {
temp[i] = (temp[i] & 0xffffff00) | val;
}
}
dsrb->PutRow(ctx, dsrb, count, x, y, temp, mask);
}
}
static void
put_values_s8(GLcontext *ctx, struct gl_renderbuffer *s8rb, GLuint count,
const GLint x[], const GLint y[],
const void *values, const GLubyte *mask)
{
struct gl_renderbuffer *dsrb = s8rb->Wrapped;
const GLubyte *src = (const GLubyte *) values;
ASSERT(s8rb->DataType == GL_UNSIGNED_BYTE);
ASSERT(dsrb->_ActualFormat == GL_DEPTH24_STENCIL8_EXT);
ASSERT(dsrb->DataType == GL_UNSIGNED_INT_24_8_EXT);
if (dsrb->GetPointer(ctx, dsrb, 0, 0)) {
GLuint i;
for (i = 0; i < count; i++) {
if (!mask || mask[i]) {
GLuint *dst = (GLuint *) dsrb->GetPointer(ctx, dsrb, x[i], y[i]);
*dst = (*dst & 0xffffff00) | src[i];
}
}
}
else {
GLuint temp[MAX_WIDTH], i;
dsrb->GetValues(ctx, dsrb, count, x, y, temp);
for (i = 0; i < count; i++) {
if (!mask || mask[i]) {
temp[i] = (temp[i] & 0xffffff00) | src[i];
}
}
dsrb->PutValues(ctx, dsrb, count, x, y, temp, mask);
}
}
static void
put_mono_values_s8(GLcontext *ctx, struct gl_renderbuffer *s8rb, GLuint count,
const GLint x[], const GLint y[],
const void *value, const GLubyte *mask)
{
struct gl_renderbuffer *dsrb = s8rb->Wrapped;
GLuint temp[MAX_WIDTH], i;
const GLubyte val = *((GLubyte *) value);
dsrb->GetValues(ctx, dsrb, count, x, y, temp);
for (i = 0; i < count; i++) {
if (!mask || mask[i]) {
temp[i] = (temp[i] & 0xffffff) | val;
}
}
dsrb->PutValues(ctx, dsrb, count, x, y, temp, mask);
}
struct gl_renderbuffer *
_mesa_new_s8_renderbuffer_wrapper(GLcontext *ctx, struct gl_renderbuffer *dsrb)
{
struct gl_renderbuffer *s8rb;
ASSERT(dsrb->_ActualFormat == GL_DEPTH24_STENCIL8_EXT);
ASSERT(dsrb->DataType == GL_UNSIGNED_INT_24_8_EXT);
s8rb = _mesa_new_renderbuffer(ctx, 0);
if (!s8rb)
return NULL;
s8rb->Wrapped = dsrb;
s8rb->Name = dsrb->Name;
s8rb->RefCount = 1;
s8rb->Width = dsrb->Width;
s8rb->Height = dsrb->Height;
s8rb->InternalFormat = GL_STENCIL_INDEX8_EXT;
s8rb->_ActualFormat = GL_STENCIL_INDEX8_EXT;
s8rb->_BaseFormat = GL_STENCIL_INDEX;
s8rb->DataType = GL_UNSIGNED_BYTE;
s8rb->StencilBits = 8;
s8rb->Data = NULL;
s8rb->Delete = delete_wrapper;
s8rb->AllocStorage = alloc_wrapper_storage;
s8rb->GetPointer = nop_get_pointer;
s8rb->GetRow = get_row_s8;
s8rb->GetValues = get_values_s8;
s8rb->PutRow = put_row_s8;
s8rb->PutRowRGB = NULL;
s8rb->PutMonoRow = put_mono_row_s8;
s8rb->PutValues = put_values_s8;
s8rb->PutMonoValues = put_mono_values_s8;
return s8rb;
}
void
_mesa_extract_stencil(GLcontext *ctx,
struct gl_renderbuffer *dsRb,
struct gl_renderbuffer *stencilRb)
{
GLuint row, width, height;
ASSERT(dsRb);
ASSERT(stencilRb);
ASSERT(dsRb->_ActualFormat == GL_DEPTH24_STENCIL8_EXT);
ASSERT(dsRb->DataType == GL_UNSIGNED_INT_24_8_EXT);
ASSERT(stencilRb->_ActualFormat == GL_DEPTH24_STENCIL8_EXT ||
stencilRb->_ActualFormat == GL_STENCIL_INDEX8_EXT);
ASSERT(dsRb->Width == stencilRb->Width);
ASSERT(dsRb->Height == stencilRb->Height);
width = dsRb->Width;
height = dsRb->Height;
for (row = 0; row < height; row++) {
GLuint depthStencil[MAX_WIDTH];
dsRb->GetRow(ctx, dsRb, width, 0, row, depthStencil);
if (stencilRb->_ActualFormat == GL_STENCIL_INDEX8_EXT) {
GLubyte stencil[MAX_WIDTH];
GLuint i;
for (i = 0; i < width; i++) {
stencil[i] = depthStencil[i] & 0xff;
}
stencilRb->PutRow(ctx, stencilRb, width, 0, row, stencil, NULL);
}
else {
ASSERT(stencilRb->_ActualFormat == GL_DEPTH24_STENCIL8_EXT);
ASSERT(stencilRb->DataType == GL_UNSIGNED_INT_24_8_EXT);
stencilRb->PutRow(ctx, stencilRb, width, 0, row, depthStencil, NULL);
}
}
}
void
_mesa_insert_stencil(GLcontext *ctx,
struct gl_renderbuffer *dsRb,
struct gl_renderbuffer *stencilRb)
{
GLuint row, width, height;
ASSERT(dsRb);
ASSERT(stencilRb);
ASSERT(dsRb->_ActualFormat == GL_DEPTH24_STENCIL8_EXT);
ASSERT(dsRb->DataType == GL_UNSIGNED_INT_24_8_EXT);
ASSERT(stencilRb->_ActualFormat == GL_DEPTH24_STENCIL8_EXT ||
stencilRb->_ActualFormat == GL_STENCIL_INDEX8_EXT);
ASSERT(dsRb->Width == stencilRb->Width);
ASSERT(dsRb->Height == stencilRb->Height);
width = dsRb->Width;
height = dsRb->Height;
for (row = 0; row < height; row++) {
GLuint depthStencil[MAX_WIDTH];
dsRb->GetRow(ctx, dsRb, width, 0, row, depthStencil);
if (stencilRb->_ActualFormat == GL_STENCIL_INDEX8_EXT) {
GLubyte stencil[MAX_WIDTH];
GLuint i;
stencilRb->GetRow(ctx, stencilRb, width, 0, row, stencil);
for (i = 0; i < width; i++) {
depthStencil[i] = (depthStencil[i] & 0xffffff00) | stencil[i];
}
}
else {
GLuint stencil[MAX_WIDTH], i;
ASSERT(stencilRb->_ActualFormat == GL_DEPTH24_STENCIL8_EXT);
ASSERT(stencilRb->DataType == GL_UNSIGNED_INT_24_8_EXT);
stencilRb->GetRow(ctx, stencilRb, width, 0, row, stencil);
for (i = 0; i < width; i++) {
depthStencil[i]
= (depthStencil[i] & 0xffffff00) | (stencil[i] & 0xff);
}
}
dsRb->PutRow(ctx, dsRb, width, 0, row, depthStencil, NULL);
}
}
void
_mesa_promote_stencil(GLcontext *ctx, struct gl_renderbuffer *stencilRb)
{
const GLsizei width = stencilRb->Width;
const GLsizei height = stencilRb->Height;
GLubyte *data;
GLint i, j, k;
ASSERT(stencilRb->_ActualFormat == GL_STENCIL_INDEX8_EXT);
ASSERT(stencilRb->Data);
data = (GLubyte *) stencilRb->Data;
stencilRb->Data = NULL;
stencilRb->AllocStorage(ctx, stencilRb, GL_DEPTH24_STENCIL8_EXT,
width, height);
ASSERT(stencilRb->DataType == GL_UNSIGNED_INT_24_8_EXT);
k = 0;
for (i = 0; i < height; i++) {
GLuint depthStencil[MAX_WIDTH];
for (j = 0; j < width; j++) {
depthStencil[j] = data[k++];
}
stencilRb->PutRow(ctx, stencilRb, width, 0, i, depthStencil, NULL);
}
_mesa_free(data);
stencilRb->_BaseFormat = GL_DEPTH_STENCIL_EXT;
}