Younes Manton wrote:
On Fri, Apr 17, 2009 at 2:22 PM, Thomas Hellstrom <[email protected]> wrote:
Ideally this flushing would've been done in the various map operations within
the pipe driver as you say, but
unfortunately these map operations are screen operations, so the pipe driver
doesn't know which context to flush in a multi-threaded environment.
Usually drivers wait for fence in a map operation but there is no fence if the
draw commands affecting the buffer have not been flushed => Rendering errors.
For each buffer we keep track of which command buf it has been
referenced in and which context that command buf belongs to. So when
we map we check if the buffer has been referenced in a command buf and
flush the context that owns it and associate the resulting fences with
the buffer and wait on them. If the buffer isn't referenced but is
fenced then we just wait on them as usual.
Not sure how we keep track if a buffer is referenced in more than one
unflushed context though, the current code doesn't seem to handle
that, based on my quick look. I'll have to ask Ben.
So if the driver is handling all this correctly, then it should be safe
to return PIPE_UNREFERENCED in the
hooks, and you will get back the old behavior. Still, there might be odd
situations when the state tracker intended a st_no_flush_xx operation,
and the driver flushes anyway.
Also if a buffer is referenced by another context than the current,
(another thread), you will effectively flush that other thread's
rendering context from the thread doing a map operation. While that's
not illegal at the driver level, it requires very careful locking.
Also the semantics for this will vary for different state-trackers. For OpenGL
it's sufficient to flush the current context of the
thread doing the map operation, since there can only be one context with
unflushed data per thread.
Other threads are required to synchronize in the application to avoid clashes.
For other state trackers the situation will be different, although I'm no
expert in this field we have a couple of them in-house.
So to summarize:
1) We need to know what context to flush before mapping. Only the state-tracker
knows that.
2) Different state-trackers have different requirements.
I'm attaching a program "flush_teximage", which will expose the problem.
Run with flush_teximage -subimage and you'll probably see the rendering error
without this fix.
You forgot to attach it. I'd like to give it a try if you still have
it around somewhere.
Oops. Sorry.
Attached two files, flush_teximage.c and flush_pbo.c,
The latter is always working with gallium because we don't do HW
accelerated pbos yet, but
flush_teximage doesn't work unless you've set up map flushing correctly.
./flush_teximage and
./flush_teximage -subimage
shoudl give the same output.
/Thomas
/*
* Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee, provided
* that (i) the above copyright notices and this permission notice appear in
* all copies of the software and related documentation, and (ii) the name of
* Silicon Graphics may not be used in any advertising or
* publicity relating to the software without the specific, prior written
* permission of Silicon Graphics.
*
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
* ANY KIND,
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <GL/glut.h>
#define CI_OFFSET_1 16
#define CI_OFFSET_2 32
GLenum doubleBuffer;
GLboolean doSub = GL_FALSE;
static void Init(void)
{
fprintf(stderr, "GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
fprintf(stderr, "GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
fprintf(stderr, "GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
glClearColor(0.0, 0.0, 1.0, 0.0);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
#define SIZE 32
static void setTexImage(GLubyte grayscale, GLboolean sub)
{
GLubyte tex2d[SIZE*SIZE*3];
memset(tex2d, grayscale, SIZE*SIZE*3);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
if (sub) {
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, SIZE, SIZE,
GL_RGB, GL_UNSIGNED_BYTE, tex2d);
} else {
glTexImage2D(GL_TEXTURE_2D, 0, 3, SIZE, SIZE, 0,
GL_RGB, GL_UNSIGNED_BYTE, tex2d);
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
}
static void Reshape(int width, int height)
{
glViewport(0, 0, (GLint)width, (GLint)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
glMatrixMode(GL_MODELVIEW);
}
static void Key(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(1);
default:
return;
}
glutPostRedisplay();
}
static void Draw(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
setTexImage(0, 0);
glBegin(GL_QUADS);
glTexCoord2f(1,0);
glVertex3f( 0.9, -0.9, -30.0);
glTexCoord2f(1,1);
glVertex3f( 0.9, 0.9, -30.0);
glTexCoord2f(0,1);
glVertex3f(-0.9, 0.9, -30.0);
glTexCoord2f(0,0);
glVertex3f(-0.9, -0.9, -30.0);
glEnd();
setTexImage(128, doSub);
glBegin(GL_QUADS);
glTexCoord2f(1,0);
glVertex3f( 0.6, -0.6, -30.0);
glTexCoord2f(1,1);
glVertex3f( 0.6, 0.6, -30.0);
glTexCoord2f(0,1);
glVertex3f(-0.6, 0.6, -30.0);
glTexCoord2f(0,0);
glVertex3f(-0.6, -0.6, -30.0);
glEnd();
setTexImage(255, doSub);
glBegin(GL_QUADS);
glTexCoord2f(1,0);
glVertex3f( 0.3, -0.3, -30.0);
glTexCoord2f(1,1);
glVertex3f( 0.3, 0.3, -30.0);
glTexCoord2f(0,1);
glVertex3f(-0.3, 0.3, -30.0);
glTexCoord2f(0,0);
glVertex3f(-0.3, -0.3, -30.0);
glEnd();
glDisable(GL_TEXTURE_2D);
glFlush();
if (doubleBuffer) {
glutSwapBuffers();
}
}
static GLenum Args(int argc, char **argv)
{
GLint i;
doubleBuffer = GL_FALSE;
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-sb") == 0) {
doubleBuffer = GL_FALSE;
} else if (strcmp(argv[i], "-db") == 0) {
doubleBuffer = GL_TRUE;
} else if (strcmp(argv[i], "-subimage") == 0) {
doSub = GL_TRUE;
} else {
fprintf(stderr, "%s (Bad option).\n", argv[i]);
return GL_FALSE;
}
}
return GL_TRUE;
}
int main(int argc, char **argv)
{
GLenum type;
glutInit(&argc, argv);
if (Args(argc, argv) == GL_FALSE) {
exit(1);
}
glutInitWindowPosition(0, 0); glutInitWindowSize( 250, 250);
type = GLUT_RGB;
type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
glutInitDisplayMode(type);
if (glutCreateWindow("Black-Grey-White image") == GL_FALSE) {
exit(1);
}
Init();
glutReshapeFunc(Reshape);
glutKeyboardFunc(Key);
glutDisplayFunc(Draw);
glutMainLoop();
return 0;
}
/*
* Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee, provided
* that (i) the above copyright notices and this permission notice appear in
* all copies of the software and related documentation, and (ii) the name of
* Silicon Graphics may not be used in any advertising or
* publicity relating to the software without the specific, prior written
* permission of Silicon Graphics.
*
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
* ANY KIND,
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <GL/glext.h>
extern void * glMapBufferARB(GLenum target,
GLenum access);
#define CI_OFFSET_1 16
#define CI_OFFSET_2 32
GLenum doubleBuffer;
static int doSub = 0;
static GLuint pbo;
static void Init(void)
{
fprintf(stderr, "GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
fprintf(stderr, "GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
fprintf(stderr, "GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
glClearColor(0.0, 0.0, 1.0, 0.0);
glGenBuffers(1, &pbo);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo);
}
#define SIZE 200
static void setPbo(GLubyte grayscale, int sub)
{
static GLubyte *pixels;
int size = SIZE*SIZE*4;
pixels = malloc(size);
memset(pixels, grayscale, size);
if (sub == 2) {
static void *buf;
buf = glMapBufferARB(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
memcpy(buf, pixels, size);
glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER);
} else if (sub == 1) {
glBufferSubData(GL_PIXEL_UNPACK_BUFFER, 0, size, pixels);
} else {
glBufferData(GL_PIXEL_UNPACK_BUFFER, size, pixels, GL_STREAM_DRAW);
}
}
static void Reshape(int width, int height)
{
glViewport(0, 0, (GLint)width, (GLint)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -0.5, 1000.0);
glMatrixMode(GL_MODELVIEW);
}
static void Key(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(1);
default:
return;
}
glutPostRedisplay();
}
static void Draw(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 200);
setPbo(0, 0);
glRasterPos2i(25, 25);
glDrawPixels(SIZE, SIZE, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
setPbo(128, doSub);
glRasterPos2i(50, 50);
glDrawPixels(150, 150, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
setPbo(255, doSub);
glRasterPos2i(75, 75);
glDrawPixels(100, 100, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glFlush();
if (doubleBuffer) {
glutSwapBuffers();
}
}
static GLenum Args(int argc, char **argv)
{
GLint i;
doubleBuffer = GL_FALSE;
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-sb") == 0) {
doubleBuffer = GL_FALSE;
} else if (strcmp(argv[i], "-db") == 0) {
doubleBuffer = GL_TRUE;
} else if (strcmp(argv[i], "-subimage") == 0) {
doSub = 1;
} else if (strcmp(argv[i], "-map") == 0) {
doSub = 2;
} else {
fprintf(stderr, "%s (Bad option).\n", argv[i]);
return GL_FALSE;
}
}
return GL_TRUE;
}
int main(int argc, char **argv)
{
GLenum type;
glutInit(&argc, argv);
if (Args(argc, argv) == GL_FALSE) {
exit(1);
}
glutInitWindowPosition(0, 0); glutInitWindowSize( 250, 250);
type = GLUT_RGB;
type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
glutInitDisplayMode(type);
if (glutCreateWindow("Black-Grey-White image") == GL_FALSE) {
exit(1);
}
Init();
glutReshapeFunc(Reshape);
glutKeyboardFunc(Key);
glutDisplayFunc(Draw);
glutMainLoop();
return 0;
}
------------------------------------------------------------------------------
Stay on top of everything new and different, both inside and
around Java (TM) technology - register by April 22, and save
$200 on the JavaOne (SM) conference, June 2-5, 2009, San Francisco.
300 plus technical and hands-on sessions. Register today.
Use priority code J9JMT32. http://p.sf.net/sfu/p
_______________________________________________
Mesa3d-dev mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mesa3d-dev